1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
macro_rules! has_extensions {
($ty:ty) => {
impl $ty {
/// Returns state information provided by the
/// [`crate::middleware::StateMiddleware`] middleware. This is a
/// shortcut to retrieving the [`crate::middleware::State`]
/// extension from the request.
///
/// # Examples
/// ```rust
/// # use under::*;
/// use under::middleware::State;
/// let mut request = Request::get("/").unwrap();
/// request.extensions_mut().insert(State(123u32));
/// assert_eq!(request.state::<u32>(), Some(&123u32));
/// ```
pub fn state<T: Send + Sync + 'static>(&self) -> Option<&T> {
self.ext::<crate::middleware::State<T>>().map(|v| &v.0)
}
/// Retrieves a specific extension from the extensions map. This is
/// the same as calling [`Self::extensions`].`get` wit the given
/// type parameter.
///
/// # Examples
/// ```rust
/// # use under::*;
/// let mut request = Request::get("/").unwrap();
/// assert_eq!(request.ext::<u32>(), None);
/// ```
pub fn ext<T: Send + Sync + 'static>(&self) -> Option<&T> {
self.extensions().get::<T>()
}
/// Retrieves a mutable reference to the specific extension from the
/// extensions map. This is the same as calling
/// [`Self::extensions_mut`].`get_mut` with the given type
/// parameter.
///
/// # Examples
/// ```rust
/// # use under::*;
/// let mut request = Request::get("/").unwrap();
/// assert_eq!(request.ext_mut::<u32>(), None);
/// ```
pub fn ext_mut<T: Send + Sync + 'static>(&mut self) -> Option<&mut T> {
self.extensions_mut().get_mut::<T>()
}
/// Sets the value of the specific extension in the extensions map.
/// This is the same as calling [`Self::extensions_mut`].`insert`
/// with the given parameter.
///
/// # Examples
/// ```rust
/// # use under::*;
/// let mut request = Request::get("/").unwrap();
/// request.set_ext(123u32);
/// assert_eq!(request.ext::<u32>(), Some(&123u32));
/// ```
pub fn set_ext<T: Send + Sync + 'static>(&mut self, value: T) -> &mut Self {
self.extensions_mut().insert(value);
self
}
/// Sets the value of the specific extension in the extensions map,
/// consuming `self`, and then returning the new value. This is
/// the same as calling [`Self::set_ext`], but it consumes `self`.
///
/// # Examples
/// ```rust
/// # use under::*;
/// let request = Request::get("/").unwrap();
/// let request = request.with_ext(123u32);
/// assert_eq!(request.ext::<u32>(), Some(&123u32));
/// ```
pub fn with_ext<T: Send + Sync + 'static>(mut self, value: T) -> Self {
self.set_ext(value);
self
}
/// Removes the specific extension from the extensions map. This is
/// the same as calling [`Self::extensions_mut`].`remove` with the
/// given type parameter.
///
/// # Examples
/// ```rust
/// # use under::*;
/// let mut request = Request::get("/").unwrap()
/// .with_ext(123u32);
/// assert_eq!(request.ext::<u32>(), Some(&123u32));
/// request.remove_ext::<u32>();
/// assert_eq!(request.ext::<u32>(), None);
/// ```
pub fn remove_ext<T: Send + Sync + 'static>(&mut self) -> Option<T> {
self.extensions_mut().remove::<T>()
}
/// Removes the specific extension from the extensions map,
/// consuming `self`, and then returning the removed value. This
/// is the same as calling [`Self::remove_ext`], but it consumes
/// `self`.
///
/// # Examples
/// ```rust
/// # use under::*;
/// let request = Request::get("/").unwrap()
/// .with_ext(123u32);
/// assert_eq!(request.ext::<u32>(), Some(&123u32));
/// let request = request.without_ext::<u32>();
/// assert_eq!(request.ext::<u32>(), None);
/// ```
pub fn without_ext<T: Send + Sync + 'static>(mut self) -> Self {
self.remove_ext::<T>();
self
}
}
};
}