Skip to main content

rama_core/io/
mod.rs

1use tokio::io::{AsyncRead, AsyncWrite};
2
3mod read;
4#[doc(inline)]
5pub use read::{ChainReader, HeapReader, StackReader, discard};
6
7mod prefix;
8#[doc(inline)]
9pub use prefix::PrefixedIo;
10
11mod graceful;
12pub use graceful::{CancelIo, GracefulIo};
13
14pub mod peek;
15pub mod rewind;
16pub mod timeout;
17
18mod bridge;
19pub use bridge::BridgeIo;
20
21mod owned;
22#[doc(inline)]
23pub use owned::{
24    ArrayBuf, AsyncReadOwned, AsyncWriteOwned, BufResult, BufSlot, IoBuf, IoBufMut, SetLen, Slice,
25    SplitIo, Uninit,
26};
27
28/// A generic transport of bytes is a type that implements `AsyncRead`, `AsyncWrite` and `Send`.
29/// This is specific to Rama and is directly linked to the supertraits of `Tokio`.
30pub trait Io: AsyncRead + AsyncWrite + Send + 'static {}
31
32impl<T> Io for T where T: AsyncRead + AsyncWrite + Send + 'static {}
33
34/// A higher level trait that can be used by services which
35/// wish to peek into I/O, often as part of Deep Protocol Inspections (DPI).
36///
37/// It is implemented for any [`Io`], returning itself.
38/// [`BridgeIo`] also implements it, assuming that the first element
39/// is the ingress side that is ok to be peeked.
40pub trait PeekIoProvider: Send + 'static {
41    /// The type that can be peeked.
42    type PeekIo: Io;
43
44    /// The mapped `Self` type produced as a result of
45    /// mapping the `PeekIo` type.
46    type Mapped<PeekedIo: Io>: Send + 'static;
47
48    /// Retrieve a mutable reference to the Peekable type.
49    fn peek_io_mut(&mut self) -> &mut Self::PeekIo;
50
51    /// Once peeking is finished one can reproduce `self`
52    /// by mapping the Peeked Io type and produce a new type,
53    /// usually with the peeked data in-memory as prefix.
54    fn map_peek_io<PeekedIo, F>(self, map: F) -> Self::Mapped<PeekedIo>
55    where
56        PeekedIo: Io,
57        F: FnOnce(Self::PeekIo) -> PeekedIo;
58}
59
60impl<T: Io> PeekIoProvider for T {
61    type PeekIo = T;
62    type Mapped<PeekedIo: Io> = PeekedIo;
63
64    #[inline(always)]
65    fn peek_io_mut(&mut self) -> &mut Self::PeekIo {
66        self
67    }
68
69    #[inline(always)]
70    fn map_peek_io<PeekedIo, F>(self, map: F) -> Self::Mapped<PeekedIo>
71    where
72        PeekedIo: Io,
73        F: FnOnce(Self::PeekIo) -> PeekedIo,
74    {
75        map(self)
76    }
77}