-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
The ptr::Pointee
design must incorporate layout info
#123353
Comments
I don't think I get it. If I have an Your proposal lacks a motivation statement for what users of the standard library should be able to do that they currently can't do. You are suggesting to re-implement the way
I think you are incorporating some kind of hypothetical future version of Again, all that is something that should be explained in the first paragraph of your proposal. Don't make us guess. :)
I don't know what you mean by that. Please give an example of a bug Miri currently cannot find, but would be able to find with your proposal. |
You're essentially co-opting
This definition has the usual issue for It's also impossible to access the field without first knowing its alignment, as that impacts its offset in the structure. Along the same lines, The operation really just wants to be possible from just |
Right, what would make a lot of sense is fn size_of_meta<T: Pointee>(meta: T::Metadata) -> usize That corresponds pretty well to the underlying primitive in the compiler. Do we have an issue tracking something like that? |
I glossed over a lot here. My apologies.
It's kinda like an RFC for an RFC. Because of this, I wasn't sure whether to post this here, in https://internals.rust-lang.org, or as a proper RFC/MCP. If you know of a better way to raise such a major question around the pointer metadata RFC/feature, please let me know and I'll move this accordingly. I'll be the first to admit I was somewhat lost on where to post this. And my being lost is admittedly part of how this turned out to be a bit of a stream of consciousness.
That's true from the average user's perspective. However, there's use in making it pluggable.
I'll defer to rust-lang/rfcs#3536 on rationale for why Some degree of alignment pluggability is also needed so FFI types like I went with a
Yeah, I should've clarified the hypothetical nature of it. I almost added a note about that dozens of times and simply forgot. All those implementations in that code block are hypothetical.
The idea is to check that stuff like |
@CAD97 I'll bring those up in rust-lang/rfcs#3536 and try to come to a solution there. This was admittedly very premature, despite me spending most of a day on it. |
#81513 introduces a
ptr::Pointee
type. This interface lacks a couple key bits of info, two key bits critical for allocators and stuff likeThinBox
.Every pointee in practice has to have some sort of defined layout, and as pointers exist in a finite space, layout is also guaranteed to be finite. rust-lang/rfcs#3536 offers a partial solution for sizes, but doesn't address the two cases where alignment isn't statically known: the dynamic alignment of
dyn Trait
and the undefined alignment of opaque types.So,
ptr::Pointee
needs to expose a way to get this information, as it was initially intended to. There's a few constraints:The simplest way I can think of is unfortunately still somewhat involved, and I'm not sure I can reduce it any further:
Add a required
fn layout(&self) -> alloc::Layout
method toptr::Pointee
.Add a
marker::Aligned
trait similar tomarker::Sized
, representing that the type has a statically defined alignment. Almost everything auto-implements this, butdyn Trait
and opaque types will not.Sized
will be modified to subtype this. The only way to declare an unknown alignment is to use anextern
type, a type of DST, and so it's not possible to both make the size statically determinable and leave the alignment unknown.Like its counterpart
Sized
,Aligned
must be implemented to read something by value, and generic parameters implicitly add anAligned
constraint unless you explicitly add?Aligned
. Unlike it,Aligned
may be manually implemented forextern
types via a#[repr(align(N))]
attribute.Make
Pointee
anunsafe
trait. The lack of safety is because it's defining the memory safety boundaries.The
layout
method is safe to call, but the method itself must ensure that:T
.&self
pointer.Auto-implement
Pointee
for every type that either implementsAligned
or has a final field that implementsPointee
.If the final field does not implement
Aligned
but does implementPointee
, the auto-generated implementation returns essentiallystatic_prefix.extend(final_field.layout()).unwrap().0
.Generics will assume
Pointee
to be implemented by default. It'll be likeSized
rather thanUnpin
in that regard.Here's how it'd look for the bulk of this at the type level:
There's a couple other benefits this provides:
size_of_val
andalign_of_val
. Their implementations are of course in the above code block.layout
s against the struct's backing memory on construct and on every pointer cast to the type, ensuring the referenced memory region is always valid and that the pointer in question is correctly aligned.The text was updated successfully, but these errors were encountered: