/rust/registry/src/index.crates.io-6f17d22bba15001f/bytes-1.10.1/src/buf/take.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use crate::Buf; |
2 | | |
3 | | use core::cmp; |
4 | | |
5 | | #[cfg(feature = "std")] |
6 | | use std::io::IoSlice; |
7 | | |
8 | | /// A `Buf` adapter which limits the bytes read from an underlying buffer. |
9 | | /// |
10 | | /// This struct is generally created by calling `take()` on `Buf`. See |
11 | | /// documentation of [`take()`](Buf::take) for more details. |
12 | | #[derive(Debug)] |
13 | | pub struct Take<T> { |
14 | | inner: T, |
15 | | limit: usize, |
16 | | } |
17 | | |
18 | 2.51M | pub fn new<T>(inner: T, limit: usize) -> Take<T> { |
19 | 2.51M | Take { inner, limit } |
20 | 2.51M | } bytes::buf::take::new::<&mut &[u8]> Line | Count | Source | 18 | 1.52M | pub fn new<T>(inner: T, limit: usize) -> Take<T> { | 19 | 1.52M | Take { inner, limit } | 20 | 1.52M | } |
Unexecuted instantiation: bytes::buf::take::new::<_> bytes::buf::take::new::<&mut &[u8]> Line | Count | Source | 18 | 996k | pub fn new<T>(inner: T, limit: usize) -> Take<T> { | 19 | 996k | Take { inner, limit } | 20 | 996k | } |
|
21 | | |
22 | | impl<T> Take<T> { |
23 | | /// Consumes this `Take`, returning the underlying value. |
24 | | /// |
25 | | /// # Examples |
26 | | /// |
27 | | /// ```rust |
28 | | /// use bytes::{Buf, BufMut}; |
29 | | /// |
30 | | /// let mut buf = b"hello world".take(2); |
31 | | /// let mut dst = vec![]; |
32 | | /// |
33 | | /// dst.put(&mut buf); |
34 | | /// assert_eq!(*dst, b"he"[..]); |
35 | | /// |
36 | | /// let mut buf = buf.into_inner(); |
37 | | /// |
38 | | /// dst.clear(); |
39 | | /// dst.put(&mut buf); |
40 | | /// assert_eq!(*dst, b"llo world"[..]); |
41 | | /// ``` |
42 | 0 | pub fn into_inner(self) -> T { |
43 | 0 | self.inner |
44 | 0 | } |
45 | | |
46 | | /// Gets a reference to the underlying `Buf`. |
47 | | /// |
48 | | /// It is inadvisable to directly read from the underlying `Buf`. |
49 | | /// |
50 | | /// # Examples |
51 | | /// |
52 | | /// ```rust |
53 | | /// use bytes::Buf; |
54 | | /// |
55 | | /// let buf = b"hello world".take(2); |
56 | | /// |
57 | | /// assert_eq!(11, buf.get_ref().remaining()); |
58 | | /// ``` |
59 | 0 | pub fn get_ref(&self) -> &T { |
60 | 0 | &self.inner |
61 | 0 | } |
62 | | |
63 | | /// Gets a mutable reference to the underlying `Buf`. |
64 | | /// |
65 | | /// It is inadvisable to directly read from the underlying `Buf`. |
66 | | /// |
67 | | /// # Examples |
68 | | /// |
69 | | /// ```rust |
70 | | /// use bytes::{Buf, BufMut}; |
71 | | /// |
72 | | /// let mut buf = b"hello world".take(2); |
73 | | /// let mut dst = vec![]; |
74 | | /// |
75 | | /// buf.get_mut().advance(2); |
76 | | /// |
77 | | /// dst.put(&mut buf); |
78 | | /// assert_eq!(*dst, b"ll"[..]); |
79 | | /// ``` |
80 | 0 | pub fn get_mut(&mut self) -> &mut T { |
81 | 0 | &mut self.inner |
82 | 0 | } |
83 | | |
84 | | /// Returns the maximum number of bytes that can be read. |
85 | | /// |
86 | | /// # Note |
87 | | /// |
88 | | /// If the inner `Buf` has fewer bytes than indicated by this method then |
89 | | /// that is the actual number of available bytes. |
90 | | /// |
91 | | /// # Examples |
92 | | /// |
93 | | /// ```rust |
94 | | /// use bytes::Buf; |
95 | | /// |
96 | | /// let mut buf = b"hello world".take(2); |
97 | | /// |
98 | | /// assert_eq!(2, buf.limit()); |
99 | | /// assert_eq!(b'h', buf.get_u8()); |
100 | | /// assert_eq!(1, buf.limit()); |
101 | | /// ``` |
102 | 0 | pub fn limit(&self) -> usize { |
103 | 0 | self.limit |
104 | 0 | } |
105 | | |
106 | | /// Sets the maximum number of bytes that can be read. |
107 | | /// |
108 | | /// # Note |
109 | | /// |
110 | | /// If the inner `Buf` has fewer bytes than `lim` then that is the actual |
111 | | /// number of available bytes. |
112 | | /// |
113 | | /// # Examples |
114 | | /// |
115 | | /// ```rust |
116 | | /// use bytes::{Buf, BufMut}; |
117 | | /// |
118 | | /// let mut buf = b"hello world".take(2); |
119 | | /// let mut dst = vec![]; |
120 | | /// |
121 | | /// dst.put(&mut buf); |
122 | | /// assert_eq!(*dst, b"he"[..]); |
123 | | /// |
124 | | /// dst.clear(); |
125 | | /// |
126 | | /// buf.set_limit(3); |
127 | | /// dst.put(&mut buf); |
128 | | /// assert_eq!(*dst, b"llo"[..]); |
129 | | /// ``` |
130 | 0 | pub fn set_limit(&mut self, lim: usize) { |
131 | 0 | self.limit = lim |
132 | 0 | } |
133 | | } |
134 | | |
135 | | impl<T: Buf> Buf for Take<T> { |
136 | 4.75M | fn remaining(&self) -> usize { |
137 | 4.75M | cmp::min(self.inner.remaining(), self.limit) |
138 | 4.75M | } <bytes::buf::take::Take<&mut &[u8]> as bytes::buf::buf_impl::Buf>::remaining Line | Count | Source | 136 | 2.89M | fn remaining(&self) -> usize { | 137 | 2.89M | cmp::min(self.inner.remaining(), self.limit) | 138 | 2.89M | } |
Unexecuted instantiation: <bytes::buf::take::Take<_> as bytes::buf::buf_impl::Buf>::remaining <bytes::buf::take::Take<&mut &[u8]> as bytes::buf::buf_impl::Buf>::remaining Line | Count | Source | 136 | 1.85M | fn remaining(&self) -> usize { | 137 | 1.85M | cmp::min(self.inner.remaining(), self.limit) | 138 | 1.85M | } |
|
139 | | |
140 | 2.23M | fn chunk(&self) -> &[u8] { |
141 | 2.23M | let bytes = self.inner.chunk(); |
142 | 2.23M | &bytes[..cmp::min(bytes.len(), self.limit)] |
143 | 2.23M | } <bytes::buf::take::Take<&mut &[u8]> as bytes::buf::buf_impl::Buf>::chunk Line | Count | Source | 140 | 1.37M | fn chunk(&self) -> &[u8] { | 141 | 1.37M | let bytes = self.inner.chunk(); | 142 | 1.37M | &bytes[..cmp::min(bytes.len(), self.limit)] | 143 | 1.37M | } |
Unexecuted instantiation: <bytes::buf::take::Take<_> as bytes::buf::buf_impl::Buf>::chunk <bytes::buf::take::Take<&mut &[u8]> as bytes::buf::buf_impl::Buf>::chunk Line | Count | Source | 140 | 861k | fn chunk(&self) -> &[u8] { | 141 | 861k | let bytes = self.inner.chunk(); | 142 | 861k | &bytes[..cmp::min(bytes.len(), self.limit)] | 143 | 861k | } |
|
144 | | |
145 | 2.23M | fn advance(&mut self, cnt: usize) { |
146 | 2.23M | assert!(cnt <= self.limit); |
147 | 2.23M | self.inner.advance(cnt); |
148 | 2.23M | self.limit -= cnt; |
149 | 2.23M | } <bytes::buf::take::Take<&mut &[u8]> as bytes::buf::buf_impl::Buf>::advance Line | Count | Source | 145 | 1.37M | fn advance(&mut self, cnt: usize) { | 146 | 1.37M | assert!(cnt <= self.limit); | 147 | 1.37M | self.inner.advance(cnt); | 148 | 1.37M | self.limit -= cnt; | 149 | 1.37M | } |
Unexecuted instantiation: <bytes::buf::take::Take<_> as bytes::buf::buf_impl::Buf>::advance <bytes::buf::take::Take<&mut &[u8]> as bytes::buf::buf_impl::Buf>::advance Line | Count | Source | 145 | 861k | fn advance(&mut self, cnt: usize) { | 146 | 861k | assert!(cnt <= self.limit); | 147 | 861k | self.inner.advance(cnt); | 148 | 861k | self.limit -= cnt; | 149 | 861k | } |
|
150 | | |
151 | 0 | fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes { |
152 | 0 | assert!(len <= self.remaining(), "`len` greater than remaining"); |
153 | | |
154 | 0 | let r = self.inner.copy_to_bytes(len); |
155 | 0 | self.limit -= len; |
156 | 0 | r |
157 | 0 | } |
158 | | |
159 | | #[cfg(feature = "std")] |
160 | 0 | fn chunks_vectored<'a>(&'a self, dst: &mut [IoSlice<'a>]) -> usize { |
161 | 0 | if self.limit == 0 { |
162 | 0 | return 0; |
163 | 0 | } |
164 | | |
165 | | const LEN: usize = 16; |
166 | 0 | let mut slices: [IoSlice<'a>; LEN] = [ |
167 | 0 | IoSlice::new(&[]), |
168 | 0 | IoSlice::new(&[]), |
169 | 0 | IoSlice::new(&[]), |
170 | 0 | IoSlice::new(&[]), |
171 | 0 | IoSlice::new(&[]), |
172 | 0 | IoSlice::new(&[]), |
173 | 0 | IoSlice::new(&[]), |
174 | 0 | IoSlice::new(&[]), |
175 | 0 | IoSlice::new(&[]), |
176 | 0 | IoSlice::new(&[]), |
177 | 0 | IoSlice::new(&[]), |
178 | 0 | IoSlice::new(&[]), |
179 | 0 | IoSlice::new(&[]), |
180 | 0 | IoSlice::new(&[]), |
181 | 0 | IoSlice::new(&[]), |
182 | 0 | IoSlice::new(&[]), |
183 | 0 | ]; |
184 | 0 |
|
185 | 0 | let cnt = self |
186 | 0 | .inner |
187 | 0 | .chunks_vectored(&mut slices[..dst.len().min(LEN)]); |
188 | 0 | let mut limit = self.limit; |
189 | 0 | for (i, (dst, slice)) in dst[..cnt].iter_mut().zip(slices.iter()).enumerate() { |
190 | 0 | if let Some(buf) = slice.get(..limit) { |
191 | | // SAFETY: We could do this safely with `IoSlice::advance` if we had a larger MSRV. |
192 | 0 | let buf = unsafe { std::mem::transmute::<&[u8], &'a [u8]>(buf) }; |
193 | 0 | *dst = IoSlice::new(buf); |
194 | 0 | return i + 1; |
195 | 0 | } else { |
196 | 0 | // SAFETY: We could do this safely with `IoSlice::advance` if we had a larger MSRV. |
197 | 0 | let buf = unsafe { std::mem::transmute::<&[u8], &'a [u8]>(slice) }; |
198 | 0 | *dst = IoSlice::new(buf); |
199 | 0 | limit -= slice.len(); |
200 | 0 | } |
201 | | } |
202 | 0 | cnt |
203 | 0 | } |
204 | | } |