1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
//! Extra utilities for `Buf` and `BufMut` types. use super::{Buf, BufMut}; mod chain; mod limit; #[cfg(feature = "std")] mod reader; mod take; #[cfg(feature = "std")] mod writer; pub use self::limit::Limit; pub use self::take::Take; pub use self::chain::Chain; #[cfg(feature = "std")] pub use self::{reader::Reader, writer::Writer}; /// Extra methods for implementations of `Buf`. pub trait BufExt: Buf { /// Creates an adaptor which will read at most `limit` bytes from `self`. /// /// This function returns a new instance of `Buf` which will read at most /// `limit` bytes. /// /// # Examples /// /// ``` /// use bytes::{Buf, BufMut, buf::BufExt}; /// /// let mut buf = b"hello world"[..].take(5); /// let mut dst = vec![]; /// /// dst.put(&mut buf); /// assert_eq!(dst, b"hello"); /// /// let mut buf = buf.into_inner(); /// dst.clear(); /// dst.put(&mut buf); /// assert_eq!(dst, b" world"); /// ``` fn take(self, limit: usize) -> Take<Self> where Self: Sized { take::new(self, limit) } /// Creates an adaptor which will chain this buffer with another. /// /// The returned `Buf` instance will first consume all bytes from `self`. /// Afterwards the output is equivalent to the output of next. /// /// # Examples /// /// ``` /// use bytes::{Buf, buf::BufExt}; /// /// let mut chain = b"hello "[..].chain(&b"world"[..]); /// /// let full = chain.to_bytes(); /// assert_eq!(full.bytes(), b"hello world"); /// ``` fn chain<U: Buf>(self, next: U) -> Chain<Self, U> where Self: Sized { Chain::new(self, next) } /// Creates an adaptor which implements the `Read` trait for `self`. /// /// This function returns a new value which implements `Read` by adapting /// the `Read` trait functions to the `Buf` trait functions. Given that /// `Buf` operations are infallible, none of the `Read` functions will /// return with `Err`. /// /// # Examples /// /// ``` /// use bytes::{Buf, Bytes, buf::BufExt}; /// use std::io::Read; /// /// let buf = Bytes::from("hello world"); /// /// let mut reader = buf.reader(); /// let mut dst = [0; 1024]; /// /// let num = reader.read(&mut dst).unwrap(); /// /// assert_eq!(11, num); /// assert_eq!(&dst[..11], &b"hello world"[..]); /// ``` #[cfg(feature = "std")] fn reader(self) -> Reader<Self> where Self: Sized { reader::new(self) } } impl<B: Buf + ?Sized> BufExt for B {} /// Extra methods for implementations of `BufMut`. pub trait BufMutExt: BufMut { /// Creates an adaptor which can write at most `limit` bytes to `self`. /// /// # Examples /// /// ``` /// use bytes::{BufMut, buf::BufMutExt}; /// /// let arr = &mut [0u8; 128][..]; /// assert_eq!(arr.remaining_mut(), 128); /// /// let dst = arr.limit(10); /// assert_eq!(dst.remaining_mut(), 10); /// ``` fn limit(self, limit: usize) -> Limit<Self> where Self: Sized { limit::new(self, limit) } /// Creates an adaptor which implements the `Write` trait for `self`. /// /// This function returns a new value which implements `Write` by adapting /// the `Write` trait functions to the `BufMut` trait functions. Given that /// `BufMut` operations are infallible, none of the `Write` functions will /// return with `Err`. /// /// # Examples /// /// ``` /// use bytes::{BufMut, buf::BufMutExt}; /// use std::io::Write; /// /// let mut buf = vec![].writer(); /// /// let num = buf.write(&b"hello world"[..]).unwrap(); /// assert_eq!(11, num); /// /// let buf = buf.into_inner(); /// /// assert_eq!(*buf, b"hello world"[..]); /// ``` #[cfg(feature = "std")] fn writer(self) -> Writer<Self> where Self: Sized { writer::new(self) } /// Creates an adapter which will chain this buffer with another. /// /// The returned `BufMut` instance will first write to all bytes from /// `self`. Afterwards, it will write to `next`. /// /// # Examples /// /// ``` /// use bytes::{BufMut, buf::BufMutExt}; /// /// let mut a = [0u8; 5]; /// let mut b = [0u8; 6]; /// /// let mut chain = (&mut a[..]).chain_mut(&mut b[..]); /// /// chain.put_slice(b"hello world"); /// /// assert_eq!(&a[..], b"hello"); /// assert_eq!(&b[..], b" world"); /// ``` fn chain_mut<U: BufMut>(self, next: U) -> Chain<Self, U> where Self: Sized { Chain::new(self, next) } } impl<B: BufMut + ?Sized> BufMutExt for B {}