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
use crate::{
http_context::HttpContext,
request::{FromRequest, Request},
responder::Responder,
response::Builder,
};
use std::{
borrow::{Borrow, BorrowMut},
ops::{Deref, DerefMut},
};
use crate::{body::Body, prelude::Bytes};
pub use http::Extensions;
#[derive(Debug)]
pub enum ExtError {
MissingExtension(&'static str),
}
impl Responder for ExtError {
fn respond_with_builder(self, builder: Builder, _ctx: &HttpContext) -> Builder {
debug!("Missing extension of type: {}", std::any::type_name::<Self>());
builder.status(500)
}
}
pub struct Ext<T>(pub T);
impl<T> Ext<T> {
pub fn into_inner(self) -> T {
self.0
}
}
impl<T> Deref for Ext<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> DerefMut for Ext<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<T> AsRef<T> for Ext<T> {
fn as_ref(&self) -> &T {
&self.0
}
}
impl<T> AsMut<T> for Ext<T> {
fn as_mut(&mut self) -> &mut T {
&mut self.0
}
}
impl<T> Borrow<T> for Ext<T> {
fn borrow(&self) -> &T {
&self.0
}
}
impl<T> BorrowMut<T> for Ext<T> {
fn borrow_mut(&mut self) -> &mut T {
&mut self.0
}
}
impl<T: Send + Sync + 'static> Responder for Ext<T> {
fn respond_with_builder(self, builder: Builder, _ctx: &HttpContext) -> Builder {
builder.extension(self.into_inner())
}
}
impl<T> FromRequest for Ext<T>
where
T: Send + Sync + 'static,
{
type Err = ExtError;
type Fut = futures::future::Ready<Result<Self, Self::Err>>;
fn from_request(req: &mut Request) -> Self::Fut {
futures::future::ready(
req.extensions_mut()
.remove::<T>()
.ok_or_else(|| ExtError::MissingExtension(std::any::type_name::<T>()))
.map(Ext),
)
}
}
impl FromRequest for Extensions {
type Err = ();
type Fut = futures::future::Ready<Result<Self, Self::Err>>;
fn from_request(req: &mut Request<Body<Bytes>>) -> Self::Fut {
futures::future::ready(Ok(std::mem::take(req.extensions_mut())))
}
}