33//! # Examples
44//!
55//! ```
6- //! use timely_bytes::arc::Bytes ;
6+ //! use timely_bytes::arc::BytesMut ;
77//!
88//! let bytes = vec![0u8; 1024];
9- //! let mut shared1 = Bytes ::from(bytes);
9+ //! let mut shared1 = BytesMut ::from(bytes);
1010//! let mut shared2 = shared1.extract_to(100);
1111//! let mut shared3 = shared1.extract_to(100);
1212//! let mut shared4 = shared2.extract_to(60);
@@ -38,7 +38,11 @@ pub mod arc {
3838 use std:: any:: Any ;
3939
4040 /// A thread-safe byte buffer backed by a shared allocation.
41- pub struct Bytes {
41+ ///
42+ /// An instance of this type contends that `ptr` is valid for `len` bytes,
43+ /// and that no other reference to these bytes exists, other than through
44+ /// the type currently held in `sequestered`.
45+ pub struct BytesMut {
4246 /// Pointer to the start of this slice (not the allocation).
4347 ptr : * mut u8 ,
4448 /// Length of this slice.
@@ -54,12 +58,12 @@ pub mod arc {
5458 // Synchronization happens through `self.sequestered`, which mean to ensure that even
5559 // across multiple threads each region of the slice is uniquely "owned", if not in the
5660 // traditional Rust sense.
57- unsafe impl Send for Bytes { }
61+ unsafe impl Send for BytesMut { }
5862
59- impl Bytes {
63+ impl BytesMut {
6064
6165 /// Create a new instance from a byte allocation.
62- pub fn from < B > ( bytes : B ) -> Bytes where B : DerefMut < Target =[ u8 ] > +' static {
66+ pub fn from < B > ( bytes : B ) -> BytesMut where B : DerefMut < Target =[ u8 ] > +' static {
6367
6468 // Sequester allocation behind an `Arc`, which *should* keep the address
6569 // stable for the lifetime of `sequestered`. The `Arc` also serves as our
@@ -73,24 +77,24 @@ pub mod arc {
7377 . map ( |a| ( a. as_mut_ptr ( ) , a. len ( ) ) )
7478 . unwrap ( ) ;
7579
76- Bytes {
80+ BytesMut {
7781 ptr,
7882 len,
7983 sequestered,
8084 }
8185 }
8286
83- /// Extracts [0, index) into a new `Bytes ` which is returned, updating `self`.
87+ /// Extracts [0, index) into a new `BytesMut ` which is returned, updating `self`.
8488 ///
8589 /// # Safety
8690 ///
8791 /// This method first tests `index` against `self.len`, which should ensure that both
88- /// the returned `Bytes ` contains valid memory, and that `self` can no longer access it.
89- pub fn extract_to ( & mut self , index : usize ) -> Bytes {
92+ /// the returned `BytesMut ` contains valid memory, and that `self` can no longer access it.
93+ pub fn extract_to ( & mut self , index : usize ) -> BytesMut {
9094
9195 assert ! ( index <= self . len) ;
9296
93- let result = Bytes {
97+ let result = BytesMut {
9498 ptr : self . ptr ,
9599 len : index,
96100 sequestered : self . sequestered . clone ( ) ,
@@ -102,19 +106,19 @@ pub mod arc {
102106 result
103107 }
104108
105- /// Regenerates the Bytes if it is uniquely held.
109+ /// Regenerates the BytesMut if it is uniquely held.
106110 ///
107111 /// If uniquely held, this method recovers the initial pointer and length
108- /// of the sequestered allocation and re-initializes the Bytes . The return
112+ /// of the sequestered allocation and re-initializes the BytesMut . The return
109113 /// value indicates whether this occurred.
110114 ///
111115 /// # Examples
112116 ///
113117 /// ```
114- /// use timely_bytes::arc::Bytes ;
118+ /// use timely_bytes::arc::BytesMut ;
115119 ///
116120 /// let bytes = vec![0u8; 1024];
117- /// let mut shared1 = Bytes ::from(bytes);
121+ /// let mut shared1 = BytesMut ::from(bytes);
118122 /// let mut shared2 = shared1.extract_to(100);
119123 /// let mut shared3 = shared1.extract_to(100);
120124 /// let mut shared4 = shared2.extract_to(60);
@@ -147,10 +151,10 @@ pub mod arc {
147151 /// # Examples
148152 ///
149153 /// ```
150- /// use timely_bytes::arc::Bytes ;
154+ /// use timely_bytes::arc::BytesMut ;
151155 ///
152156 /// let bytes = vec![0u8; 1024];
153- /// let mut shared1 = Bytes ::from(bytes);
157+ /// let mut shared1 = BytesMut ::from(bytes);
154158 /// let mut shared2 = shared1.extract_to(100);
155159 /// let mut shared3 = shared1.extract_to(100);
156160 /// let mut shared4 = shared2.extract_to(60);
@@ -160,7 +164,7 @@ pub mod arc {
160164 /// shared2.try_merge(shared1).ok().expect("Failed to merge 23 and 1");
161165 /// shared4.try_merge(shared2).ok().expect("Failed to merge 4 and 231");
162166 /// ```
163- pub fn try_merge ( & mut self , other : Bytes ) -> Result < ( ) , Bytes > {
167+ pub fn try_merge ( & mut self , other : BytesMut ) -> Result < ( ) , BytesMut > {
164168 if Arc :: ptr_eq ( & self . sequestered , & other. sequestered ) && :: std:: ptr:: eq ( self . ptr . wrapping_add ( self . len ) , other. ptr ) {
165169 self . len += other. len ;
166170 Ok ( ( ) )
@@ -171,14 +175,14 @@ pub mod arc {
171175 }
172176 }
173177
174- impl Deref for Bytes {
178+ impl Deref for BytesMut {
175179 type Target = [ u8 ] ;
176180 fn deref ( & self ) -> & [ u8 ] {
177181 unsafe { :: std:: slice:: from_raw_parts ( self . ptr , self . len ) }
178182 }
179183 }
180184
181- impl DerefMut for Bytes {
185+ impl DerefMut for BytesMut {
182186 fn deref_mut ( & mut self ) -> & mut [ u8 ] {
183187 unsafe { :: std:: slice:: from_raw_parts_mut ( self . ptr , self . len ) }
184188 }
0 commit comments