Skip to content

Commit

Permalink
[FromBytes] Replace Ref with Ptr in some methods (#1163)
Browse files Browse the repository at this point in the history
While we're here, replace some `bikeshed_xxx` `Ref` constructors with
their non-`bikeshed` equivalents. It appears that we accidentally
introduced complete duplication in our API, so these `bikeshed_xxx`
constructors can simply be deleted.
  • Loading branch information
joshlf authored May 2, 2024
1 parent 8944c61 commit 0a5f5f6
Showing 1 changed file with 8 additions and 56 deletions.
64 changes: 8 additions & 56 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1749,7 +1749,8 @@ pub unsafe trait FromBytes: FromZeros {
Self: KnownLayout + Immutable,
{
util::assert_dst_is_not_zst::<Self>();
Ref::<&[u8], Self>::bikeshed_new_known_layout(bytes).map(Ref::into_ref)
let ptr = Ptr::from_ref(bytes).try_cast_into_no_leftover()?;
Some(ptr.bikeshed_recall_valid().as_ref())
}

/// Interprets the prefix of the given `bytes` as a `&Self` without copying.
Expand Down Expand Up @@ -1820,8 +1821,7 @@ pub unsafe trait FromBytes: FromZeros {
Self: KnownLayout + Immutable,
{
util::assert_dst_is_not_zst::<Self>();
Ref::<&[u8], Self>::bikeshed_new_from_prefix_known_layout(bytes)
.map(|(r, s)| (r.into_ref(), s))
Ref::<&[u8], Self>::new_from_prefix(bytes).map(|(r, s)| (r.into_ref(), s))
}

/// Interprets the suffix of the given `bytes` as a `&Self` without copying.
Expand Down Expand Up @@ -1878,8 +1878,7 @@ pub unsafe trait FromBytes: FromZeros {
Self: Immutable + KnownLayout,
{
util::assert_dst_is_not_zst::<Self>();
Ref::<&[u8], Self>::bikeshed_new_from_suffix_known_layout(bytes)
.map(|(p, r)| (p, r.into_ref()))
Ref::<&[u8], Self>::new_from_suffix(bytes).map(|(p, r)| (p, r.into_ref()))
}

/// Interprets the given `bytes` as a `&mut Self` without copying.
Expand Down Expand Up @@ -1944,7 +1943,8 @@ pub unsafe trait FromBytes: FromZeros {
Self: IntoBytes + KnownLayout + Immutable,
{
util::assert_dst_is_not_zst::<Self>();
Ref::<&mut [u8], Self>::bikeshed_new_known_layout(bytes).map(Ref::into_mut)
let ptr = Ptr::from_mut(bytes).try_cast_into_no_leftover()?;
Some(ptr.bikeshed_recall_valid().as_mut())
}

/// Interprets the prefix of the given `bytes` as a `&mut Self` without
Expand Down Expand Up @@ -2014,8 +2014,7 @@ pub unsafe trait FromBytes: FromZeros {
Self: IntoBytes + KnownLayout + Immutable,
{
util::assert_dst_is_not_zst::<Self>();
Ref::<&mut [u8], Self>::bikeshed_new_from_prefix_known_layout(bytes)
.map(|(r, s)| (r.into_mut(), s))
Ref::<&mut [u8], Self>::new_from_prefix(bytes).map(|(r, s)| (r.into_mut(), s))
}

/// Interprets the suffix of the given `bytes` as a `&mut Self` without
Expand Down Expand Up @@ -2079,8 +2078,7 @@ pub unsafe trait FromBytes: FromZeros {
Self: IntoBytes + KnownLayout + Immutable,
{
util::assert_dst_is_not_zst::<Self>();
Ref::<&mut [u8], Self>::bikeshed_new_from_suffix_known_layout(bytes)
.map(|(p, r)| (p, r.into_mut()))
Ref::<&mut [u8], Self>::new_from_suffix(bytes).map(|(p, r)| (p, r.into_mut()))
}

/// Interprets the prefix of the given `bytes` as a `&[Self]` with length
Expand Down Expand Up @@ -4619,52 +4617,6 @@ impl<B: ByteSlice + Clone, T: ?Sized> Clone for Ref<B, T> {
// invariant on `ByteSlice`, these properties are preserved by `Copy`.
impl<B: ByteSlice + Copy, T: ?Sized> Copy for Ref<B, T> {}

impl<B, T> Ref<B, T>
where
B: ByteSlice,
T: KnownLayout + Immutable + ?Sized,
{
#[must_use = "has no side effects"]
fn bikeshed_new_known_layout(bytes: B) -> Option<Ref<B, T>> {
util::assert_dst_is_not_zst::<T>();
let _ = Ptr::from_ref(bytes.deref()).try_cast_into_no_leftover::<T>()?;
// INVARIANTS: `try_cast_into_no_leftover` validates size and alignment.
Some(Ref(bytes, PhantomData))
}
}

impl<B, T> Ref<B, T>
where
B: SplitByteSlice,
T: KnownLayout + Immutable + ?Sized,
{
#[must_use = "has no side effects"]
fn bikeshed_new_from_prefix_known_layout(bytes: B) -> Option<(Ref<B, T>, B)> {
util::assert_dst_is_not_zst::<T>();
let (_, split_at) = Ptr::from_ref(bytes.deref()).try_cast_into::<T>(CastType::Prefix)?;
let (bytes, suffix) = try_split_at(bytes, split_at)?;
// INVARIANTS: `try_cast_into` validates size and alignment, and returns
// a `split_at` that indicates how many bytes of `bytes` correspond to a
// valid `T`. By safety postcondition on `SplitByteSlice::try_split_at`
// we can rely `try_split_at` to produce the correct `bytes` and
// `suffix`.
Some((Ref(bytes, PhantomData), suffix))
}

#[must_use = "has no side effects"]
fn bikeshed_new_from_suffix_known_layout(bytes: B) -> Option<(B, Ref<B, T>)> {
util::assert_dst_is_not_zst::<T>();
let (_, split_at) = Ptr::from_ref(bytes.deref()).try_cast_into::<T>(CastType::Suffix)?;
let (prefix, bytes) = try_split_at(bytes, split_at)?;
// INVARIANTS: `try_cast_into` validates size and alignment, and returns
// a `split_at` that indicates how many bytes of `bytes` correspond to a
// valid `T`. By safety postcondition on `SplitByteSlice::try_split_at`
// we can rely on `try_split_at` to produce the correct `prefix` and
// `bytes`.
Some((prefix, Ref(bytes, PhantomData)))
}
}

impl<B, T> Ref<B, T>
where
B: ByteSlice,
Expand Down

0 comments on commit 0a5f5f6

Please sign in to comment.