-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why don't slices use dedicated metadata structs? #125517
Comments
Related to #81513 |
I was thinking about this myself, and I agree that the metadata for slices should be a distinct type. Moreover, when there is a separate metadata type for each different kind of DST, then it becomes possible to distinguish DSTs by their type. You could specify that a type must be a slice DST, or a trait object DST, or whatever new DSTs people might add in the future. |
If "multiply-unsized" types ever become a thing, e.g.
I don't think |
@zachs18 To be clear:
|
Note that that's only precisely true for references to slices. "Thanks" to https://doc.rust-lang.org/std/ptr/fn.slice_from_raw_parts.html, a |
@scottmcm There's also 32-bit x86 with PAE, which extends the address space beyond what a simple pointer can address. It is possible in kernel land to access those beyond-max-pointer offsets. |
@dead-claudia Not with |
Should the So what about slice-based custom DSTs? It would definitely be valuable to have a EDIT: Another thought: since we already have #69835, it may even be useful for the |
@Rua This issue is focused on slices in particular, and ergonomics surrounding them. That's why everything here is centered around slices. What you're discussing here, I spelled out a bit in #123353 (closed as I thought I had more collected than I did, and I suspect you're in a similar boat). I suggest filing a new issue, linking to it from the main tracking issue #81513, and seeing where it takes you. |
Slices and slice-based DSTs use the same metadata. That means that if you call |
A method that could be useful to add for pub fn is_valid(&self) -> bool It would check if the metadata follows the requirements for functions like |
It is possible. There isn't really a difference between If Note that struct A(u16, [u8]);
let a: &A = ...;
assert_eq!(a.1.len(), 2); // given length of the slice = 2
assert_eq!(metadata(a).size_of(), 2); // the byte size of the slice
assert_eq!(size_of_val(a), 4); // size of the whole structure (this is the same what currently happens with |
Oh, I see. I misunderstood the proposal, I thought I think this looks good then. Will you be implementing it? |
passer-by remarks: I'm not sure what the overall benefit of the newtype is. Isn't slice transmutation better solved with Also IMO the marker shouldn't be |
Yes, for slices you already have and don't own. This targets two cases:
You could replace |
Curious question: why don't slices also use dedicated wrapper structs? Why just
&dyn Trait
?I'd think
std::ptr::metadata(slice).len()
would be clearer thanstd::ptr::metadata(slice)
, for example. You could also add other helper methods, to simplify common tasks that might use it, and the added type safety would make it safer to use. (Safe enough, in fact, for you to entirely replace slice construction APIs with it.)Concretely, I'm thinking of the following, for
&str
and&[T]
:Also, part of the idea here is to replace
slice::from_raw_parts{,_mut}
andptr::slice_from_raw_parts{,_mut}
with a less error-prone alternative similarly to howmem::MaybeUninit
replacedmem::uninit
. So, instead of this:You'd do this instead:
The text was updated successfully, but these errors were encountered: