-
Notifications
You must be signed in to change notification settings - Fork 107
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 try_transmute!, try_transmute_{ref,mut}! #359
Conversation
src/lib.rs
Outdated
// TODO: What's the correct drop behavior on `None`? Does this just | ||
// behave like `mem::forget` in that case? | ||
let m = $crate::__RealManuallyDrop::new(e); | ||
$crate::TryFromBytes::try_read_from($crate::AsBytes::as_bytes(&m)) |
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.
@jswrenn Thoughts on this TODO? I could see an argument that the most reasonable behavior would be to drop on failure - you always have something that gets dropped, either now or later (if try_transmute!
succeeds and you have a U
that gets dropped later).
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.
Discussed with @jswrenn; he advocates for, on failure, returning e
as Result::Error(e)
.
df988fb
to
6c7af14
Compare
e22c6d0
to
b62e2ec
Compare
6c7af14
to
d7c16fa
Compare
59e8401
to
4891c4f
Compare
d7c16fa
to
7a78746
Compare
4891c4f
to
0f315c7
Compare
7a78746
to
d1921e2
Compare
bfb5bf5
to
1b18c53
Compare
d1921e2
to
0adefe6
Compare
1b18c53
to
87754b2
Compare
/// assert_eq!(try_transmute_ref!(&0u32.as_bytes()[1..]), None::<&u16>); | ||
/// ``` | ||
#[macro_export] | ||
macro_rules! try_transmute_ref { |
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.
This can - and thus should - validate size equality at compile time.
0adefe6
to
76b3532
Compare
87754b2
to
2402ec6
Compare
76b3532
to
5d4515a
Compare
c26336c
to
b9b29a0
Compare
5d4515a
to
06a52df
Compare
b9b29a0
to
34016f9
Compare
06a52df
to
68ab76d
Compare
34016f9
to
1a0b1bb
Compare
05e5775
to
77ad3c3
Compare
1a0b1bb
to
b8781ee
Compare
77ad3c3
to
0b55a6d
Compare
b8781ee
to
dc8e0fc
Compare
0b55a6d
to
1eff6f2
Compare
02895f3
to
51e8bcc
Compare
43b24a2
to
94130a9
Compare
51e8bcc
to
528d07d
Compare
94130a9
to
c79a650
Compare
528d07d
to
83e66cc
Compare
c79a650
to
778533a
Compare
83e66cc
to
91c9b2b
Compare
778533a
to
24f0c25
Compare
91c9b2b
to
7c32984
Compare
24f0c25
to
51cf9f9
Compare
7c32984
to
dde3280
Compare
51cf9f9
to
eb970d2
Compare
dde3280
to
91057f7
Compare
eb970d2
to
baa448f
Compare
91057f7
to
3008eb5
Compare
baa448f
to
73961ac
Compare
3008eb5
to
1e309ca
Compare
`Ptr` is like `NonNull`, but has many restrictions which make it so that using a `Ptr` in unsafe code requires much simpler soundness proofs. In particular, in a future commit, we will add support for a `try_cast_into<U>` method where `U: ?Sized + KnownLayout`, which is a building block of `TryFromBytes`. Makes progress on #29
`try_cast_into` attempts to cast a `Ptr<[u8]>` into a `Ptr<U>` where `U: ?Sized + KnownLayout`, and will be built upon in future commits as a building block of `TryFromBytes`. Because `try_cast_into` performs a runtime check to validate alignment, this requires disabling Miri's "symbolic alignment check" feature. While it would be possible to run both with and without that feature in order to still test other code using symbolic alignment checking, I don't think that the benefit is worth a) the complexity of passing the information necessary for certain tests to not run under symbolic alignment checking and, b) the extra CI time to run Miri tests twice. Makes progress on #29
`TryFromBytes` can be implemented for types which are not `FromZeroes` or `FromBytes`; it supports performing a runtime check to determine whether a given byte sequence contains a valid instance of `Self`. This is the first step of #5. Future commits will add support for a custom derive and for implementing `TryFromBytes` on unsized types. TODO: - More thorough tests for non-FromBytes types (bool, char, etc) - Tests that go through the `TryFromBytes` public methods rather than testing `is_bit_valid` directly - Update safety requirements for `is_bit_valid` now that it takes a `Ptr` rather than a `NonNull` - Update SAFETY comments in various places Makes progress on #5
TODO: Commit message body TODO: - In `try_transmute!`, should the argument be dropped or forgotten (ie, `mem::forget`) when the transmute fails? We could also return the original value in a `Result::Error`, but that would be inconsistent with `TryFrom`. Then again, `TryFrom` provides a custom error type, so in theory implementers could do that if they wanted to. Most of the types that don't consume Copy types. - Add UI tests Makes progress on #5
1e309ca
to
82d9322
Compare
73961ac
to
df96950
Compare
b69386f
to
806a8d7
Compare
806a8d7
to
e54c8d6
Compare
Superseded by #1622. |
TODO: Body