s2n_quic_core/buffer/reader/
storage.rs

1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4mod buf;
5mod bytes;
6mod chunk;
7mod empty;
8mod full_copy;
9mod infallible;
10mod io_slice;
11mod slice;
12mod tracked;
13
14#[cfg(test)]
15mod tests;
16
17pub use buf::Buf;
18pub use chunk::Chunk;
19pub use empty::Empty;
20pub use full_copy::FullCopy;
21pub use infallible::Infallible;
22pub use io_slice::IoSlice;
23pub use tracked::Tracked;
24
25pub trait Storage {
26    type Error: 'static;
27
28    /// Returns the length of the chunk
29    fn buffered_len(&self) -> usize;
30
31    /// Returns if the chunk is empty
32    #[inline]
33    fn buffer_is_empty(&self) -> bool {
34        self.buffered_len() == 0
35    }
36
37    /// Reads the current contiguous chunk
38    fn read_chunk(&mut self, watermark: usize) -> Result<Chunk<'_>, Self::Error>;
39
40    /// Copies the reader into `dest`, with a trailing chunk of bytes.
41    ///
42    /// Implementations should either fill the `dest` completely or exhaust the buffered data.
43    ///
44    /// The storage also returns a `Chunk`, which can be used by the caller to defer
45    /// copying the trailing chunk until later. The returned chunk must fit into the target
46    /// destination. The caller must eventually copy the chunk into the destination, otherwise this
47    /// data will be discarded.
48    fn partial_copy_into<Dest>(&mut self, dest: &mut Dest) -> Result<Chunk<'_>, Self::Error>
49    where
50        Dest: crate::buffer::writer::Storage + ?Sized;
51
52    /// Copies the reader into `dest`.
53    ///
54    /// Implementations should either fill the `dest` completely or exhaust the buffered data.
55    #[inline]
56    fn copy_into<Dest>(&mut self, dest: &mut Dest) -> Result<(), Self::Error>
57    where
58        Dest: crate::buffer::writer::Storage + ?Sized,
59    {
60        let mut chunk = self.partial_copy_into(dest)?;
61        chunk.infallible_copy_into(dest);
62        Ok(())
63    }
64
65    /// Forces the entire reader to be copied, even when calling `partial_copy_into`.
66    ///
67    /// The returned `Chunk` from `partial_copy_into` will always be empty.
68    #[inline]
69    fn full_copy(&mut self) -> FullCopy<'_, Self> {
70        FullCopy::new(self)
71    }
72
73    /// Tracks the number of bytes read from the storage
74    #[inline]
75    fn track_read(&mut self) -> Tracked<'_, Self> {
76        Tracked::new(self)
77    }
78}