ntex_io/
seal.rs

1use std::{any::Any, any::TypeId, fmt, io, ops, task::Context, task::Poll};
2
3use crate::filter::{Filter, FilterReadStatus};
4use crate::{buf::Stack, Io, IoRef, ReadStatus, WriteStatus};
5
6/// Sealed filter type
7pub struct Sealed(pub(crate) Box<dyn Filter>);
8
9impl fmt::Debug for Sealed {
10    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11        f.debug_struct("Sealed").finish()
12    }
13}
14
15impl Filter for Sealed {
16    #[inline]
17    fn query(&self, id: TypeId) -> Option<Box<dyn Any>> {
18        self.0.query(id)
19    }
20
21    #[inline]
22    fn process_read_buf(
23        &self,
24        io: &IoRef,
25        stack: &Stack,
26        idx: usize,
27        nbytes: usize,
28    ) -> io::Result<FilterReadStatus> {
29        self.0.process_read_buf(io, stack, idx, nbytes)
30    }
31
32    #[inline]
33    fn process_write_buf(&self, io: &IoRef, stack: &Stack, idx: usize) -> io::Result<()> {
34        self.0.process_write_buf(io, stack, idx)
35    }
36
37    #[inline]
38    fn shutdown(&self, io: &IoRef, stack: &Stack, idx: usize) -> io::Result<Poll<()>> {
39        self.0.shutdown(io, stack, idx)
40    }
41
42    #[inline]
43    fn poll_read_ready(&self, cx: &mut Context<'_>) -> Poll<ReadStatus> {
44        self.0.poll_read_ready(cx)
45    }
46
47    #[inline]
48    fn poll_write_ready(&self, cx: &mut Context<'_>) -> Poll<WriteStatus> {
49        self.0.poll_write_ready(cx)
50    }
51}
52
53#[derive(Debug)]
54/// Boxed `Io` object with erased filter type
55pub struct IoBoxed(Io<Sealed>);
56
57impl IoBoxed {
58    #[inline]
59    /// Clone current io object.
60    ///
61    /// Current io object becomes closed.
62    pub fn take(&mut self) -> Self {
63        IoBoxed(self.0.take())
64    }
65}
66
67impl<F: Filter> From<Io<F>> for IoBoxed {
68    fn from(io: Io<F>) -> Self {
69        Self(io.seal())
70    }
71}
72
73impl ops::Deref for IoBoxed {
74    type Target = Io<Sealed>;
75
76    #[inline]
77    fn deref(&self) -> &Self::Target {
78        &self.0
79    }
80}
81
82impl From<IoBoxed> for Io<Sealed> {
83    fn from(value: IoBoxed) -> Self {
84        value.0
85    }
86}