ntex_io/
types.rs

1//! Query related types
2use std::{any, fmt, marker::PhantomData, net::SocketAddr};
3
4#[derive(Copy, Clone, PartialEq, Eq)]
5pub struct PeerAddr(pub SocketAddr);
6
7impl PeerAddr {
8    pub fn into_inner(self) -> SocketAddr {
9        self.0
10    }
11}
12
13impl From<SocketAddr> for PeerAddr {
14    fn from(addr: SocketAddr) -> Self {
15        Self(addr)
16    }
17}
18
19impl fmt::Debug for PeerAddr {
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        self.0.fmt(f)
22    }
23}
24
25#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
26/// Http protocol definition
27pub enum HttpProtocol {
28    Http1,
29    Http2,
30    Unknown,
31}
32
33pub struct QueryItem<T> {
34    item: Option<Box<dyn any::Any>>,
35    _t: PhantomData<T>,
36}
37
38impl<T: any::Any> QueryItem<T> {
39    pub(crate) fn new(item: Box<dyn any::Any>) -> Self {
40        Self {
41            item: Some(item),
42            _t: PhantomData,
43        }
44    }
45
46    pub(crate) fn empty() -> Self {
47        Self {
48            item: None,
49            _t: PhantomData,
50        }
51    }
52
53    pub fn get(&self) -> Option<T>
54    where
55        T: Copy,
56    {
57        self.item.as_ref().and_then(|v| v.downcast_ref().copied())
58    }
59
60    pub fn as_ref(&self) -> Option<&T> {
61        if let Some(ref item) = self.item {
62            item.downcast_ref()
63        } else {
64            None
65        }
66    }
67}
68
69impl<T: any::Any + fmt::Debug> fmt::Debug for QueryItem<T> {
70    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71        if let Some(v) = self.as_ref() {
72            f.debug_tuple("QueryItem").field(v).finish()
73        } else {
74            f.debug_tuple("QueryItem").field(&None::<T>).finish()
75        }
76    }
77}