Skip to main content

ntex_io/
seal.rs

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