Skip to content

Commit

Permalink
Implement Copy and Clone for Ref
Browse files Browse the repository at this point in the history
Closes #627
  • Loading branch information
joshlf committed Mar 1, 2024
1 parent e6c0ff8 commit 469a27c
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4780,6 +4780,20 @@ pub struct Ref<B, T: ?Sized>(
#[doc(hidden)]
pub use Ref as LayoutVerified;

impl<B: Clone, T: ?Sized> Clone for Ref<B, T> {
fn clone(&self) -> Ref<B, T> {
// INVARIANTS: By invariant on `self.0`, it is aligned to `T`'s
// alignment and its size corresponds to a valid size for `T`. By safety
// invariant on `ByteSlice`, these properties are preserved by `clone`.
Ref(self.0.clone(), PhantomData)
}
}

// INVARIANTS: By invariant on `Ref`'s `.0` field, it is aligned to `T`'s
// alignment and its size corresponds to a valid size for `T`. By safety
// invariant on `ByteSlice`, these properties are preserved by `Copy`.
impl<B: Copy, T: ?Sized> Copy for Ref<B, T> {}

impl<B, T> Ref<B, T>
where
B: ByteSlice,
Expand Down Expand Up @@ -5588,6 +5602,15 @@ mod sealed {
/// operation in order for the utilities in this crate to perform as designed.
///
/// [`split_at`]: crate::ByteSlice::split_at
///
/// # Safety
///
/// If `Self: Clone` or `Self: Copy`, these implementations must be "stable" in
/// the sense that, given `bytes: Self`, if `bytes.deref()` produces a byte
/// slice of a given address and length, any copy or clone of `bytes`,
/// `bytes_new`, must also produce a byte slice of the same address and length
/// from `bytes_new.deref()`.
///
// It may seem overkill to go to this length to ensure that this doc link never
// breaks. We do this because it simplifies CI - it means that generating docs
// always succeeds, so we don't need special logic to only generate docs under
Expand Down

0 comments on commit 469a27c

Please sign in to comment.