Skip to content

Commit 6e0f30c

Browse files
authored
Implement Copy and Clone for Ref (#989)
Closes #627
1 parent ac36ddb commit 6e0f30c

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4760,6 +4760,21 @@ pub struct Ref<B, T: ?Sized>(
47604760
PhantomData<T>,
47614761
);
47624762

4763+
impl<B: ByteSlice + Clone, T: ?Sized> Clone for Ref<B, T> {
4764+
#[inline]
4765+
fn clone(&self) -> Ref<B, T> {
4766+
// INVARIANTS: By invariant on `self.0`, it is aligned to `T`'s
4767+
// alignment and its size corresponds to a valid size for `T`. By safety
4768+
// invariant on `ByteSlice`, these properties are preserved by `clone`.
4769+
Ref(self.0.clone(), PhantomData)
4770+
}
4771+
}
4772+
4773+
// INVARIANTS: By invariant on `Ref`'s `.0` field, it is aligned to `T`'s
4774+
// alignment and its size corresponds to a valid size for `T`. By safety
4775+
// invariant on `ByteSlice`, these properties are preserved by `Copy`.
4776+
impl<B: ByteSlice + Copy, T: ?Sized> Copy for Ref<B, T> {}
4777+
47634778
impl<B, T> Ref<B, T>
47644779
where
47654780
B: ByteSlice,
@@ -5568,6 +5583,15 @@ mod sealed {
55685583
/// operation in order for the utilities in this crate to perform as designed.
55695584
///
55705585
/// [`split_at`]: crate::ByteSlice::split_at
5586+
///
5587+
/// # Safety
5588+
///
5589+
/// If `Self: Clone` or `Self: Copy`, these implementations must be "stable" in
5590+
/// the sense that, given `bytes: Self`, if `bytes.deref()` produces a byte
5591+
/// slice of a given address and length, any copy or clone of `bytes`,
5592+
/// `bytes_new`, must also produce a byte slice of the same address and length
5593+
/// from `bytes_new.deref()`.
5594+
///
55715595
// It may seem overkill to go to this length to ensure that this doc link never
55725596
// breaks. We do this because it simplifies CI - it means that generating docs
55735597
// always succeeds, so we don't need special logic to only generate docs under

0 commit comments

Comments
 (0)