• Home
  • Raw
  • Download

Lines Matching full:bytes

21 /// `Bytes` is an efficient container for storing and operating on contiguous
25 /// `Bytes` values facilitate zero-copy network programming by allowing multiple
26 /// `Bytes` objects to point to the same underlying memory.
28 /// `Bytes` does not have a single implementation. It is an interface, whose
30 /// implementations of `Bytes`.
32 /// All `Bytes` implementations must fulfill the following requirements:
38 /// use bytes::Bytes;
40 /// let mut mem = Bytes::from("Hello world");
53 /// The `Bytes` struct itself is fairly small, limited to 4 `usize` fields used
55 /// `Bytes` handle has access to.
57 /// `Bytes` keeps both a pointer to the shared state containing the full memory
59 /// `Bytes` also tracks the length of its view into the memory.
63 /// `Bytes` contains a vtable, which allows implementations of `Bytes` to define
65 /// When `Bytes::clone()` is called, `Bytes` will call the vtable function for
67 /// `Bytes` instances.
69 /// For `Bytes` implementations which refer to constant memory (e.g. created
70 /// via `Bytes::from_static()`) the cloning implementation will be a no-op.
72 /// For `Bytes` implementations which point to a reference counted shared storage
76 /// Due to this mechanism, multiple `Bytes` instances may point to the same
78 /// Each `Bytes` instance can point to different sections within that
79 /// memory region, and `Bytes` instances may or may not have overlapping views
82 /// The following diagram visualizes a scenario where 2 `Bytes` instances make
88 /// ________________________ / │ Bytes 2 │
91 /// |_________/ │ Bytes 1 │ | |
100 pub struct Bytes { struct
110 pub clone: unsafe fn(&AtomicPtr<()>, *const u8, usize) -> Bytes, argument
113 /// takes `Bytes` to value
119 impl Bytes { impl
120 /// Creates a new empty `Bytes`.
122 /// This will not allocate and the returned `Bytes` handle will be empty.
127 /// use bytes::Bytes;
129 /// let b = Bytes::new();
138 Bytes::from_static(EMPTY) in new()
144 Bytes::from_static(EMPTY) in new()
147 /// Creates a new `Bytes` from a static slice.
149 /// The returned `Bytes` will point directly to the static slice. There is
155 /// use bytes::Bytes;
157 /// let b = Bytes::from_static(b"hello");
162 pub const fn from_static(bytes: &'static [u8]) -> Self { in from_static()
163 Bytes { in from_static()
164 ptr: bytes.as_ptr(), in from_static()
165 len: bytes.len(), in from_static()
172 pub fn from_static(bytes: &'static [u8]) -> Self { in from_static()
173 Bytes { in from_static()
174 ptr: bytes.as_ptr(), in from_static()
175 len: bytes.len(), in from_static()
181 /// Returns the number of bytes contained in this `Bytes`.
186 /// use bytes::Bytes;
188 /// let b = Bytes::from(&b"hello"[..]);
196 /// Returns true if the `Bytes` has a length of 0.
201 /// use bytes::Bytes;
203 /// let b = Bytes::new();
211 /// Creates `Bytes` instance from slice, by copying it.
219 /// return a new `Bytes` handle set to the slice.
226 /// use bytes::Bytes;
228 /// let a = Bytes::from(&b"hello world"[..]);
269 return Bytes::new(); in slice()
282 /// When processing a `Bytes` buffer with other tools, one often gets a
283 /// `&[u8]` which is in fact a slice of the `Bytes`, i.e. a subset of it.
284 /// This function turns that `&[u8]` into another `Bytes`, as if one had
292 /// use bytes::Bytes;
294 /// let bytes = Bytes::from(&b"012345678"[..]);
295 /// let as_slice = bytes.as_ref();
297 /// let subslice = bytes.slice_ref(&subset);
304 /// `Bytes` buffer; otherwise this function will panic.
306 // Empty slice and empty Bytes may have their pointers reset in slice_ref()
309 return Bytes::new(); in slice_ref()
338 /// Splits the bytes into two at the given index.
340 /// Afterwards `self` contains elements `[0, at)`, and the returned `Bytes`
349 /// use bytes::Bytes;
351 /// let mut a = Bytes::from(&b"hello world"[..]);
361 #[must_use = "consider Bytes::truncate if you don't need the other half"]
371 return Bytes::new(); in split_off()
375 return mem::replace(self, Bytes::new()); in split_off()
387 /// Splits the bytes into two at the given index.
390 /// `Bytes` contains elements `[0, at)`.
398 /// use bytes::Bytes;
400 /// let mut a = Bytes::from(&b"hello world"[..]);
410 #[must_use = "consider Bytes::advance if you don't need the other half"]
420 return mem::replace(self, Bytes::new()); in split_to()
424 return Bytes::new(); in split_to()
435 /// Shortens the buffer, keeping the first `len` bytes and dropping the
442 /// excess bytes to be returned instead of dropped.
447 /// use bytes::Bytes;
449 /// let mut buf = Bytes::from(&b"hello world"[..]);
476 /// use bytes::Bytes;
478 /// let mut buf = Bytes::from(&b"hello world"[..]);
493 ) -> Bytes { in with_vtable() argument
494 Bytes { in with_vtable()
519 unsafe impl Send for Bytes {} implementation
520 unsafe impl Sync for Bytes {} implementation
522 impl Drop for Bytes { implementation
529 impl Clone for Bytes { implementation
531 fn clone(&self) -> Bytes { in clone() argument
536 impl Buf for Bytes { implementation
561 fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes { in copy_to_bytes() argument
563 core::mem::replace(self, Bytes::new()) in copy_to_bytes()
572 impl Deref for Bytes { implementation
581 impl AsRef<[u8]> for Bytes { implementation
588 impl hash::Hash for Bytes { implementation
597 impl Borrow<[u8]> for Bytes { implementation
603 impl IntoIterator for Bytes { implementation
605 type IntoIter = IntoIter<Bytes>;
612 impl<'a> IntoIterator for &'a Bytes { implementation
621 impl FromIterator<u8> for Bytes { implementation
629 impl PartialEq for Bytes { implementation
630 fn eq(&self, other: &Bytes) -> bool { in eq()
635 impl PartialOrd for Bytes { implementation
636 fn partial_cmp(&self, other: &Bytes) -> Option<cmp::Ordering> { in partial_cmp()
641 impl Ord for Bytes { implementation
642 fn cmp(&self, other: &Bytes) -> cmp::Ordering { in cmp()
647 impl Eq for Bytes {} implementation
649 impl PartialEq<[u8]> for Bytes { implementation
655 impl PartialOrd<[u8]> for Bytes { implementation
661 impl PartialEq<Bytes> for [u8] {
662 fn eq(&self, other: &Bytes) -> bool { in eq()
667 impl PartialOrd<Bytes> for [u8] {
668 fn partial_cmp(&self, other: &Bytes) -> Option<cmp::Ordering> { in partial_cmp()
673 impl PartialEq<str> for Bytes { implementation
679 impl PartialOrd<str> for Bytes { implementation
685 impl PartialEq<Bytes> for str {
686 fn eq(&self, other: &Bytes) -> bool { in eq()
691 impl PartialOrd<Bytes> for str {
692 fn partial_cmp(&self, other: &Bytes) -> Option<cmp::Ordering> { in partial_cmp()
697 impl PartialEq<Vec<u8>> for Bytes { implementation
703 impl PartialOrd<Vec<u8>> for Bytes { implementation
709 impl PartialEq<Bytes> for Vec<u8> {
710 fn eq(&self, other: &Bytes) -> bool { in eq()
715 impl PartialOrd<Bytes> for Vec<u8> {
716 fn partial_cmp(&self, other: &Bytes) -> Option<cmp::Ordering> { in partial_cmp()
721 impl PartialEq<String> for Bytes { implementation
727 impl PartialOrd<String> for Bytes { implementation
733 impl PartialEq<Bytes> for String {
734 fn eq(&self, other: &Bytes) -> bool { in eq()
739 impl PartialOrd<Bytes> for String {
740 fn partial_cmp(&self, other: &Bytes) -> Option<cmp::Ordering> { in partial_cmp()
745 impl PartialEq<Bytes> for &[u8] {
746 fn eq(&self, other: &Bytes) -> bool { in eq()
751 impl PartialOrd<Bytes> for &[u8] {
752 fn partial_cmp(&self, other: &Bytes) -> Option<cmp::Ordering> { in partial_cmp()
757 impl PartialEq<Bytes> for &str {
758 fn eq(&self, other: &Bytes) -> bool { in eq()
763 impl PartialOrd<Bytes> for &str {
764 fn partial_cmp(&self, other: &Bytes) -> Option<cmp::Ordering> { in partial_cmp()
769 impl<'a, T: ?Sized> PartialEq<&'a T> for Bytes implementation
771 Bytes: PartialEq<T>,
778 impl<'a, T: ?Sized> PartialOrd<&'a T> for Bytes implementation
780 Bytes: PartialOrd<T>,
789 impl Default for Bytes { implementation
791 fn default() -> Bytes { in default()
792 Bytes::new() in default()
796 impl From<&'static [u8]> for Bytes { implementation
797 fn from(slice: &'static [u8]) -> Bytes { in from() argument
798 Bytes::from_static(slice) in from()
802 impl From<&'static str> for Bytes { implementation
803 fn from(slice: &'static str) -> Bytes { in from() argument
804 Bytes::from_static(slice.as_bytes()) in from()
808 impl From<Vec<u8>> for Bytes { implementation
809 fn from(vec: Vec<u8>) -> Bytes { in from() argument
817 return Bytes::from(vec.into_boxed_slice()); in from()
834 Bytes { in from()
843 impl From<Box<[u8]>> for Bytes { implementation
844 fn from(slice: Box<[u8]>) -> Bytes { in from() argument
849 return Bytes::new(); in from()
857 Bytes { in from()
864 Bytes { in from()
874 impl From<String> for Bytes { implementation
875 fn from(s: String) -> Bytes { in from() argument
876 Bytes::from(s.into_bytes()) in from()
880 impl From<Bytes> for Vec<u8> {
881 fn from(bytes: Bytes) -> Vec<u8> { in from()
882 let bytes = mem::ManuallyDrop::new(bytes); in from() localVariable
883 unsafe { (bytes.vtable.to_vec)(&bytes.data, bytes.ptr, bytes.len) } in from()
906 unsafe fn static_clone(_: &AtomicPtr<()>, ptr: *const u8, len: usize) -> Bytes { in static_clone() argument
908 Bytes::from_static(slice) in static_clone()
934 unsafe fn promotable_even_clone(data: &AtomicPtr<()>, ptr: *const u8, len: usize) -> Bytes { in promotable_even_clone() argument
959 // If Bytes holds a Vec, then the offset must be 0. in promotable_to_vec()
994 unsafe fn promotable_odd_clone(data: &AtomicPtr<()>, ptr: *const u8, len: usize) -> Bytes { in promotable_odd_clone() argument
1061 unsafe fn shared_clone(data: &AtomicPtr<()>, ptr: *const u8, len: usize) -> Bytes { in shared_clone() argument
1105 unsafe fn shallow_clone_arc(shared: *mut Shared, ptr: *const u8, len: usize) -> Bytes { in shallow_clone_arc() argument
1112 Bytes { in shallow_clone_arc()
1127 ) -> Bytes { in shallow_clone_vec() argument
1171 Bytes { in shallow_clone_vec()
1226 // use it on miri because it gives better diagnostics for people who test bytes
1229 // See https://github.com/tokio-rs/bytes/pull/545 for more info.
1254 /// use bytes::Bytes;
1257 /// let mut b1 = Bytes::from("hello world");
1264 /// use bytes::Bytes;
1267 /// let mut b1 = Bytes::from("hello world");
1279 use super::Bytes;
1283 let a = Bytes::from(b"abcdefgh".to_vec()); in bytes_cloning_vec()
1286 // test the Bytes::clone is Sync by putting it in an Arc in bytes_cloning_vec()
1291 let b: Bytes = (*a1).clone(); in bytes_cloning_vec()
1296 let b: Bytes = (*a2).clone(); in bytes_cloning_vec()