@@ -55,7 +55,7 @@ pub mod arc {
5555 sequestered : Arc < dyn Any > ,
5656 }
5757
58- // Synchronization happens through `self.sequestered`, which mean to ensure that even
58+ // Synchronization happens through `self.sequestered`, which means to ensure that even
5959 // across multiple threads each region of the slice is uniquely "owned", if not in the
6060 // traditional Rust sense.
6161 unsafe impl Send for BytesMut { }
@@ -90,7 +90,7 @@ pub mod arc {
9090 ///
9191 /// This method first tests `index` against `self.len`, which should ensure that both
9292 /// the returned `BytesMut` contains valid memory, and that `self` can no longer access it.
93- pub fn extract_to ( & mut self , index : usize ) -> BytesMut {
93+ pub fn extract_to ( & mut self , index : usize ) -> Bytes {
9494
9595 assert ! ( index <= self . len) ;
9696
@@ -103,7 +103,7 @@ pub mod arc {
103103 self . ptr = self . ptr . wrapping_add ( index) ;
104104 self . len -= index;
105105
106- result
106+ result. freeze ( )
107107 }
108108
109109 /// Regenerates the BytesMut if it is uniquely held.
@@ -142,6 +142,77 @@ pub mod arc {
142142 }
143143 }
144144
145+ /// Converts a writeable byte slice to an shareable byte slice.
146+ pub fn freeze ( self ) -> Bytes {
147+ Bytes {
148+ ptr : self . ptr ,
149+ len : self . len ,
150+ sequestered : self . sequestered ,
151+ }
152+ }
153+ }
154+
155+ impl Deref for BytesMut {
156+ type Target = [ u8 ] ;
157+ fn deref ( & self ) -> & [ u8 ] {
158+ unsafe { :: std:: slice:: from_raw_parts ( self . ptr , self . len ) }
159+ }
160+ }
161+
162+ impl DerefMut for BytesMut {
163+ fn deref_mut ( & mut self ) -> & mut [ u8 ] {
164+ unsafe { :: std:: slice:: from_raw_parts_mut ( self . ptr , self . len ) }
165+ }
166+ }
167+
168+
169+ /// A thread-safe shared byte buffer backed by a shared allocation.
170+ ///
171+ /// An instance of this type contends that `ptr` is valid for `len` bytes,
172+ /// and that no other mutable reference to these bytes exists, other than
173+ /// through the type currently held in `sequestered`.
174+ #[ derive( Clone ) ]
175+ pub struct Bytes {
176+ /// Pointer to the start of this slice (not the allocation).
177+ ptr : * const u8 ,
178+ /// Length of this slice.
179+ len : usize ,
180+ /// Shared access to underlying resources.
181+ ///
182+ /// Importantly, this is unavailable for as long as the struct exists, which may
183+ /// prevent shared access to ptr[0 .. len]. I'm not sure I understand Rust's rules
184+ /// enough to make a stronger statement about this.
185+ sequestered : Arc < dyn Any > ,
186+ }
187+
188+ // Synchronization happens through `self.sequestered`, which means to ensure that even
189+ // across multiple threads the referenced range of bytes remain valid.
190+ unsafe impl Send for Bytes { }
191+
192+ impl Bytes {
193+
194+ /// Extracts [0, index) into a new `BytesMut` which is returned, updating `self`.
195+ ///
196+ /// # Safety
197+ ///
198+ /// This method first tests `index` against `self.len`, which should ensure that both
199+ /// the returned `BytesMut` contains valid memory, and that `self` can no longer access it.
200+ pub fn extract_to ( & mut self , index : usize ) -> Bytes {
201+
202+ assert ! ( index <= self . len) ;
203+
204+ let result = Bytes {
205+ ptr : self . ptr ,
206+ len : index,
207+ sequestered : self . sequestered . clone ( ) ,
208+ } ;
209+
210+ self . ptr = self . ptr . wrapping_add ( index) ;
211+ self . len -= index;
212+
213+ result
214+ }
215+
145216 /// Attempts to merge adjacent slices from the same allocation.
146217 ///
147218 /// If the merge succeeds then `other.len` is added to `self` and the result is `Ok(())`.
@@ -164,7 +235,7 @@ pub mod arc {
164235 /// shared2.try_merge(shared1).ok().expect("Failed to merge 23 and 1");
165236 /// shared4.try_merge(shared2).ok().expect("Failed to merge 4 and 231");
166237 /// ```
167- pub fn try_merge ( & mut self , other : BytesMut ) -> Result < ( ) , BytesMut > {
238+ pub fn try_merge ( & mut self , other : Bytes ) -> Result < ( ) , Bytes > {
168239 if Arc :: ptr_eq ( & self . sequestered , & other. sequestered ) && :: std:: ptr:: eq ( self . ptr . wrapping_add ( self . len ) , other. ptr ) {
169240 self . len += other. len ;
170241 Ok ( ( ) )
@@ -175,16 +246,10 @@ pub mod arc {
175246 }
176247 }
177248
178- impl Deref for BytesMut {
249+ impl Deref for Bytes {
179250 type Target = [ u8 ] ;
180251 fn deref ( & self ) -> & [ u8 ] {
181252 unsafe { :: std:: slice:: from_raw_parts ( self . ptr , self . len ) }
182253 }
183254 }
184-
185- impl DerefMut for BytesMut {
186- fn deref_mut ( & mut self ) -> & mut [ u8 ] {
187- unsafe { :: std:: slice:: from_raw_parts_mut ( self . ptr , self . len ) }
188- }
189- }
190255}
0 commit comments