/rust/registry/src/index.crates.io-6f17d22bba15001f/bytes-1.9.0/src/buf/limit.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use crate::buf::UninitSlice; |
2 | | use crate::BufMut; |
3 | | |
4 | | use core::cmp; |
5 | | |
6 | | /// A `BufMut` adapter which limits the amount of bytes that can be written |
7 | | /// to an underlying buffer. |
8 | | #[derive(Debug)] |
9 | | pub struct Limit<T> { |
10 | | inner: T, |
11 | | limit: usize, |
12 | | } |
13 | | |
14 | 0 | pub(super) fn new<T>(inner: T, limit: usize) -> Limit<T> { |
15 | 0 | Limit { inner, limit } |
16 | 0 | } Unexecuted instantiation: bytes::buf::limit::new::<_> Unexecuted instantiation: bytes::buf::limit::new::<&mut bytes::bytes_mut::BytesMut> |
17 | | |
18 | | impl<T> Limit<T> { |
19 | | /// Consumes this `Limit`, returning the underlying value. |
20 | 0 | pub fn into_inner(self) -> T { |
21 | 0 | self.inner |
22 | 0 | } |
23 | | |
24 | | /// Gets a reference to the underlying `BufMut`. |
25 | | /// |
26 | | /// It is inadvisable to directly write to the underlying `BufMut`. |
27 | 0 | pub fn get_ref(&self) -> &T { |
28 | 0 | &self.inner |
29 | 0 | } Unexecuted instantiation: <bytes::buf::limit::Limit<_>>::get_ref Unexecuted instantiation: <bytes::buf::limit::Limit<&mut bytes::bytes_mut::BytesMut>>::get_ref Unexecuted instantiation: <bytes::buf::limit::Limit<&mut bytes::bytes_mut::BytesMut>>::get_ref Unexecuted instantiation: <bytes::buf::limit::Limit<&mut bytes::bytes_mut::BytesMut>>::get_ref |
30 | | |
31 | | /// Gets a mutable reference to the underlying `BufMut`. |
32 | | /// |
33 | | /// It is inadvisable to directly write to the underlying `BufMut`. |
34 | 0 | pub fn get_mut(&mut self) -> &mut T { |
35 | 0 | &mut self.inner |
36 | 0 | } Unexecuted instantiation: <bytes::buf::limit::Limit<_>>::get_mut Unexecuted instantiation: <bytes::buf::limit::Limit<&mut bytes::bytes_mut::BytesMut>>::get_mut Unexecuted instantiation: <bytes::buf::limit::Limit<&mut bytes::bytes_mut::BytesMut>>::get_mut Unexecuted instantiation: <bytes::buf::limit::Limit<&mut bytes::bytes_mut::BytesMut>>::get_mut |
37 | | |
38 | | /// Returns the maximum number of bytes that can be written |
39 | | /// |
40 | | /// # Note |
41 | | /// |
42 | | /// If the inner `BufMut` has fewer bytes than indicated by this method then |
43 | | /// that is the actual number of available bytes. |
44 | 0 | pub fn limit(&self) -> usize { |
45 | 0 | self.limit |
46 | 0 | } |
47 | | |
48 | | /// Sets the maximum number of bytes that can be written. |
49 | | /// |
50 | | /// # Note |
51 | | /// |
52 | | /// If the inner `BufMut` has fewer bytes than `lim` then that is the actual |
53 | | /// number of available bytes. |
54 | 0 | pub fn set_limit(&mut self, lim: usize) { |
55 | 0 | self.limit = lim |
56 | 0 | } |
57 | | } |
58 | | |
59 | | unsafe impl<T: BufMut> BufMut for Limit<T> { |
60 | 0 | fn remaining_mut(&self) -> usize { |
61 | 0 | cmp::min(self.inner.remaining_mut(), self.limit) |
62 | 0 | } Unexecuted instantiation: <bytes::buf::limit::Limit<_> as bytes::buf::buf_mut::BufMut>::remaining_mut Unexecuted instantiation: <bytes::buf::limit::Limit<&mut bytes::bytes_mut::BytesMut> as bytes::buf::buf_mut::BufMut>::remaining_mut Unexecuted instantiation: <bytes::buf::limit::Limit<&mut bytes::bytes_mut::BytesMut> as bytes::buf::buf_mut::BufMut>::remaining_mut Unexecuted instantiation: <bytes::buf::limit::Limit<&mut bytes::bytes_mut::BytesMut> as bytes::buf::buf_mut::BufMut>::remaining_mut |
63 | | |
64 | 0 | fn chunk_mut(&mut self) -> &mut UninitSlice { |
65 | 0 | let bytes = self.inner.chunk_mut(); |
66 | 0 | let end = cmp::min(bytes.len(), self.limit); |
67 | 0 | &mut bytes[..end] |
68 | 0 | } Unexecuted instantiation: <bytes::buf::limit::Limit<_> as bytes::buf::buf_mut::BufMut>::chunk_mut Unexecuted instantiation: <bytes::buf::limit::Limit<&mut bytes::bytes_mut::BytesMut> as bytes::buf::buf_mut::BufMut>::chunk_mut Unexecuted instantiation: <bytes::buf::limit::Limit<&mut bytes::bytes_mut::BytesMut> as bytes::buf::buf_mut::BufMut>::chunk_mut Unexecuted instantiation: <bytes::buf::limit::Limit<&mut bytes::bytes_mut::BytesMut> as bytes::buf::buf_mut::BufMut>::chunk_mut |
69 | | |
70 | 0 | unsafe fn advance_mut(&mut self, cnt: usize) { |
71 | 0 | assert!(cnt <= self.limit); |
72 | 0 | self.inner.advance_mut(cnt); |
73 | 0 | self.limit -= cnt; |
74 | 0 | } Unexecuted instantiation: <bytes::buf::limit::Limit<_> as bytes::buf::buf_mut::BufMut>::advance_mut Unexecuted instantiation: <bytes::buf::limit::Limit<&mut bytes::bytes_mut::BytesMut> as bytes::buf::buf_mut::BufMut>::advance_mut Unexecuted instantiation: <bytes::buf::limit::Limit<&mut bytes::bytes_mut::BytesMut> as bytes::buf::buf_mut::BufMut>::advance_mut Unexecuted instantiation: <bytes::buf::limit::Limit<&mut bytes::bytes_mut::BytesMut> as bytes::buf::buf_mut::BufMut>::advance_mut |
75 | | } |