-
Notifications
You must be signed in to change notification settings - Fork 105
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
Add initial support for unsized MaybeUninit
wrapper type
#2055
Conversation
271b768
to
96b54d1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haven't looked at most of zerocopy-derive yet, but looked at all of zerocopy.
9844c1f
to
9751180
Compare
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## v0.8.x #2055 +/- ##
==========================================
- Coverage 88.04% 87.42% -0.63%
==========================================
Files 16 16
Lines 5983 6115 +132
==========================================
+ Hits 5268 5346 +78
- Misses 715 769 +54 ☔ View full report in Codecov by Sentry. 🚨 Try these New Features:
|
235989d
to
bb140d0
Compare
The remaining CI failures are from
|
We should report this upstream. In particular, given that |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few more nits, but otherwise LGTM!
src/util/mod.rs
Outdated
// fields both start at the safe offset and the types of those fields are | ||
// transparent wrappers around `Src` and `Dst` [1]. Consequently, | ||
// initializng `Transmute` with with `src` and then reading out `dst` is | ||
// equivalent to transmuting from `Src` to `Dst` [2]. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also need to prove that such a transmute is sound (guaranteed by the caller).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Also note this change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that latter change semantically meaningful? Or do you just prefer that wording?
bb140d0
to
b573d83
Compare
Filed: obi1kenobi/cargo-semver-checks#997 In the meantime, I think we should permit |
b573d83
to
9f151e8
Compare
d94bda0
to
944a13e
Compare
944a13e
to
167b002
Compare
This is achieved by adding a `MaybeUninit` associated type to `KnownLayout`, whose layout is identical to `Self` except that it admits uninitialized bytes in all positions. For sized types, this is bound to `mem::MaybeUninit<Self>`. For potentially unsized structs, we synthesize a doppelganger with the same `repr`, whose leading fields are wrapped in `mem::MaybeUninit` and whose trailing field is the `MaybeUninit` associated type of struct's original trailing field type. This type-level recursion bottoms out at `[T]`, whose `MaybeUninit` associated type is bound to `[mem::MaybeUninit<T>]`. Makes progress towards #1797 SKIP_CARGO_SEMVER_CHECKS=1
167b002
to
60f0a43
Compare
This is achieved by adding a
MaybeUninit
associated type toKnownLayout
, whose layout is identical toSelf
except that it admits uninitialized bytes in all positions.For sized types, this is bound to
mem::MaybeUninit<Self>
. For potentially unsized structs, we synthesize a doppelganger with the samerepr
, whose leading fields are wrapped inmem::MaybeUninit
and whose trailing field is theMaybeUninit
associated type of struct's original trailing field type. This type-level recursion bottoms out at[T]
, whoseMaybeUninit
associated type is bound to[mem::MaybeUninit<T>]
.Next Steps and Future Possibilities
MaybeUninit<T: ?Sized>
In the near term, we may remove
doc(hidden)
from ourMaybeUninit
wrapper. In doing so, we'd be quick-to-ship a gadget that extends the present capabilities of Rust. We might, then, be able to use this feature as a demonstration of our approach, potentially suitable for upstreaming into rustc.UnalignUnsized<T: ?Sized>
Presently, our
Unalign
wrapper requiresT: Sized
, because we could not figure out how to drop unsized values. For sized values, we copy them onto the stack, then run their destructor. WithMaybeUninit<T: ?Sized>
, we can extend unalign support to unsized values, by first copying them into aBox<MaybeUninit<T>>
, casting the box toBox<T>
, and dropping it. This process would be skipped for types with trivial drops, keeping the common case simple and efficient.Value<T, I> where I: Invariants<Validity = Any>
Combining the above two approaches, we could create an invariant-parameterized
Ptr
analogue for values that generalizes over initialization and alignment.