Skip to main content

buffa/types/
field_payload.rs

1// Modified by ReallyMe Technologies Limited.
2use crate::bytes::{buf::Take, Buf, Bytes};
3
4/// A read-only window over exactly one validated length-delimited field.
5///
6/// Buffa constructs this buffer after checking the field's length. Generated
7/// code decodes through [`super::decode_string_to_ctx`] and
8/// [`super::decode_bytes_to_ctx`], which check it against the end of the
9/// message that holds the field, so the window never extends into the
10/// following field or past the enclosing message, and a length that would is
11/// rejected before any override runs. The context-free
12/// [`super::decode_string_to`] and [`super::decode_bytes_to`] check it only
13/// against the bytes left in their input. Overrides of
14/// [`super::ProtoBytes::decode_length_delimited`] and
15/// [`super::ProtoString::decode_length_delimited`] can use every [`Buf`]
16/// reader, but cannot widen the window, even after a delegated decoder returns
17/// an error. Unread bytes are skipped by the caller
18/// when an override returns successfully.
19///
20/// The underlying buffer and limit are deliberately private. A `Take` would
21/// expose both controls, making the field boundary depend on each override.
22///
23/// ```compile_fail
24/// use buffa::{bytes::Buf, FieldPayload};
25/// fn widen(payload: &mut FieldPayload<impl Buf>) {
26///     payload.set_limit(usize::MAX);
27/// }
28/// ```
29///
30/// ```compile_fail
31/// use buffa::{bytes::Buf, FieldPayload};
32/// fn escape(payload: &mut FieldPayload<impl Buf>) {
33///     payload.get_mut().advance(1);
34/// }
35/// ```
36pub struct FieldPayload<B> {
37    pub(super) inner: Take<B>,
38}
39
40impl<B: Buf> FieldPayload<B> {
41    // The caller has already validated this length against buf.remaining().
42    pub(super) fn new(buf: B, length: usize) -> Self {
43        Self {
44            inner: buf.take(length),
45        }
46    }
47}
48
49impl<B: Buf> Buf for FieldPayload<B> {
50    fn remaining(&self) -> usize {
51        self.inner.remaining()
52    }
53
54    fn chunk(&self) -> &[u8] {
55        self.inner.chunk()
56    }
57
58    #[cfg(feature = "std")]
59    fn chunks_vectored<'a>(&'a self, destination: &mut [std::io::IoSlice<'a>]) -> usize {
60        self.inner.chunks_vectored(destination)
61    }
62
63    fn advance(&mut self, count: usize) {
64        self.inner.advance(count);
65    }
66
67    fn copy_to_bytes(&mut self, length: usize) -> Bytes {
68        // Preserve Bytes' shared-allocation path, with Take enforcing the
69        // boundary before forwarding to the underlying buffer.
70        self.inner.copy_to_bytes(length)
71    }
72}
73
74#[cfg(all(test, feature = "std"))]
75mod tests {
76    use super::*;
77    #[test]
78    fn vectored_chunks_remain_inside_the_validated_window() {
79        let input = b"abc".as_slice().chain(b"def".as_slice());
80        let payload = FieldPayload::new(input, 4);
81        let mut chunks = [std::io::IoSlice::new(&[]), std::io::IoSlice::new(&[])];
82        assert_eq!(payload.chunks_vectored(&mut chunks), 2);
83        assert_eq!(&*chunks[0], b"abc");
84        assert_eq!(&*chunks[1], b"d");
85        assert_eq!(payload.remaining(), 4);
86    }
87}
88
89impl<B> core::fmt::Debug for FieldPayload<B> {
90    fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
91        formatter.write_str("FieldPayload([REDACTED])")
92    }
93}