Conversation
41cf362 to
718acaa
Compare
dac2194 to
5c9afdf
Compare
src/lib.rs
Outdated
| /// | ||
| /// # Safety | ||
| /// | ||
| /// Unsafe code may assume that, if `is_bit_valid(src)` returns true, `*src` |
There was a problem hiding this comment.
| /// Unsafe code may assume that, if `is_bit_valid(src)` returns true, `*src` | |
| /// Unsafe code may assume that, if `is_src_valid(src)` returns true, `*src` |
src/lib.rs
Outdated
| /// # Panics | ||
| /// | ||
| /// `is_src_valid` panics under exactly the same circumstances as | ||
| /// [`TryFromBytes::is_bit_valid`]. |
There was a problem hiding this comment.
| /// [`TryFromBytes::is_bit_valid`]. | |
| /// [`is_bit_valid`]. | |
| /// | |
| /// [`is_bit_valid`]: TryFromBytes::is_bit_valid |
src/lib.rs
Outdated
| } | ||
|
|
||
| // SAFETY: | ||
| // - `size_of::<Src>()` <= `size_of::<Self>()` |
There was a problem hiding this comment.
| // - `size_of::<Src>()` <= `size_of::<Self>()` | |
| // - We just validated that `size_of::<Src>()` <= `size_of::<Self>()` |
src/macro_util.rs
Outdated
| Src: core::fmt::Debug + crate::IntoBytes, | ||
| Dst: core::fmt::Debug + crate::TryFromBytes, |
There was a problem hiding this comment.
| Src: core::fmt::Debug + crate::IntoBytes, | |
| Dst: core::fmt::Debug + crate::TryFromBytes, | |
| Src: crate::IntoBytes, | |
| Dst: crate::TryFromBytes, |
src/macro_util.rs
Outdated
| let src = ManuallyDrop::new(src); | ||
|
|
||
| // SAFETY: | ||
| // - `src` is a bit-valid instance of `Dst` |
There was a problem hiding this comment.
| // - `src` is a bit-valid instance of `Dst` | |
| // - `is_src_valid` validates that `src` is a bit-valid instance of `Dst` |
2050c25 to
24fa819
Compare
src/lib.rs
Outdated
| /// Conditionally transmutes a value of one type to a value of another type of | ||
| /// the same size. | ||
| /// | ||
| /// This macro an expression of type `Src` and produces a `Result<Dst, Src>`. |
There was a problem hiding this comment.
| /// This macro an expression of type `Src` and produces a `Result<Dst, Src>`. | |
| /// This macro consumes an expression of type `Src` and produces a `Result<Dst, Src>`. |
| /// Conditionally transmutes a value of one type to a value of another type of | ||
| /// the same size. | ||
| /// | ||
| /// This macro an expression of type `Src` and produces a `Result<Dst, Src>`. | ||
| /// | ||
| /// The expression `$e` must have a concrete type, `T`, which implements | ||
| /// [`IntoBytes`]. The `try_transmute!` expression must also have a concrete | ||
| /// type, `U` (`U` is inferred from the calling context), and `U` must implement | ||
| /// [`TryFromBytes`]. | ||
| /// | ||
| /// Note that the `T` produced by the expression `$e` will *not* be dropped. | ||
| /// Semantically, its bits will be copied into a new value of type `U`, the | ||
| /// original `T` will be forgotten, and the value of type `U` will be returned. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ``` | ||
| /// # use zerocopy::try_transmute; | ||
| /// assert_eq!(try_transmute!(0u8), Ok(false)); | ||
| /// assert_eq!(try_transmute!(1u8), Ok(true)); | ||
| /// assert_eq!(try_transmute!(255u8), Result::<bool, _>::Err(255)); | ||
| /// ``` |
There was a problem hiding this comment.
We should update or add language to discuss what validation is performed, and to discuss what happens on failure.
e6dc8ee to
1eba8aa
Compare
639ee24 to
e8b31b6
Compare
f2fd06c to
80bdd61
Compare
src/lib.rs
Outdated
| /// `is_src_valid` panics under exactly the same circumstances as | ||
| /// [`is_bit_valid`]. |
There was a problem hiding this comment.
Maybe also mention that size mismatch can cause a panic or a compilation failure? (just like we do for is_bit_valid)
src/macro_util.rs
Outdated
| Src: crate::IntoBytes, | ||
| Dst: crate::TryFromBytes, | ||
| { | ||
| if !Dst::is_mut_src_valid(&mut src) { |
There was a problem hiding this comment.
If we can get Unalign<Dst>: KnownLayout without Dst: KnownLayout, then we could use <Unalign<Dst> as TryFromBytes>::try_mut_from here instead of needing to add is_mut_src_valid, right?
There was a problem hiding this comment.
Using #1252, we might be able to use Unalign<SizedKnownLayout<Dst>>.
There was a problem hiding this comment.
Maybe, though doing so would entail needless runtime checks and ultimately end with throwing away the return value of try_mut_from.
I think is_src_valid (which I've now generalized to take a Ptr<Src>) will also be useful for try_transmute_mut and try_transmute_ref.
0081904 to
46cfa04
Compare
joshlf
left a comment
There was a problem hiding this comment.
UI tests? There are a lot of transmute-*.rs UI tests that we could duplicate, and possible some we could add (e.g. for Dst: !TryFromBytes).
d3b7f6e to
004b5b4
Compare
|
Closes #1013.
Makes progress towards #5.