@@ -7,7 +7,7 @@ use alloc::{borrow::ToOwned, string::String, vec::Vec};
77use core:: {
88 fmt:: { self , LowerHex , UpperHex } ,
99 net:: IpAddr ,
10- ops:: { Index , IndexMut } ,
10+ ops:: { Deref , DerefMut , Index , IndexMut } ,
1111 slice:: { Iter , IterMut , SliceIndex } ,
1212} ;
1313
@@ -524,6 +524,41 @@ impl AsRef<[u8]> for PacketMut {
524524 }
525525}
526526
527+ impl Deref for Packet {
528+ type Target = [ u8 ] ;
529+ fn deref ( & self ) -> & Self :: Target {
530+ & self . contents
531+ }
532+ }
533+
534+ impl Deref for PacketMut {
535+ type Target = [ u8 ] ;
536+ fn deref ( & self ) -> & Self :: Target {
537+ & self . contents
538+ }
539+ }
540+
541+ impl DerefMut for PacketMut {
542+ fn deref_mut ( & mut self ) -> & mut Self :: Target {
543+ & mut self . contents
544+ }
545+ }
546+
547+ // SAFETY: the underlying buf ptr is on the heap, hence stable. This is only valid because
548+ // PacketMut's DerefMut::Target is [u8], meaning that the underlying ptr can't be modified through
549+ // the `deref_mut`ed ref, and `split_to` (which does modify the heap pointer) takes `&mut self`,
550+ // which is called out by StableDeref docs as being unrestricted other than for DerefMut and Drop.
551+ unsafe impl stable_deref_trait:: StableDeref for PacketMut { }
552+
553+ // SAFETY: the underlying buf ptr is on the heap, hence stable. While the underlying Bytes does also
554+ // provide split_to, Packet does not, so the pointer can't be modified. But even if it did, this
555+ // wouldn't cause issues for StableDeref since Packet only implements Deref, and in that case the
556+ // restriction is that the pointer can't change through &Self (split_to takes &mut self).
557+ unsafe impl stable_deref_trait:: StableDeref for Packet { }
558+
559+ // SAFETY: cloning the underlying bytes just increments a refcount.
560+ unsafe impl yoke:: CloneableCart for Packet { }
561+
527562impl Buf for PacketMut {
528563 fn remaining ( & self ) -> usize {
529564 self . contents . remaining_mut ( )
0 commit comments