Skip to content

Commit 423308d

Browse files
davidpdrsndanipardojplatte
authored
Add type safe state extractor (#1155)
* begin threading the state through * Pass state to extractors * make state extractor work * make sure nesting with different states work * impl Service for MethodRouter<()> * Fix some of axum-macro's tests * Implement more traits for `State` * Update examples to use `State` * consistent naming of request body param * swap type params * Default the state param to () * fix docs references * Docs and handler state refactoring * docs clean ups * more consistent naming * when does MethodRouter implement Service? * add missing docs * use `Router`'s default state type param * changelog * don't use default type param for FromRequest and RequestParts probably safer for library authors so you don't accidentally forget * fix examples * minor docs tweaks * clarify how to convert handlers into services * group methods in one impl block * make sure merged `MethodRouter`s can access state * fix docs link * test merge with same state type * Document how to access state from middleware * Port cookie extractors to use state to extract keys (#1250) * Updates ECOSYSTEM with a new sample project (#1252) * Avoid unhelpful compiler suggestion (#1251) * fix docs typo * document how library authors should access state * Add `RequestParts::with_state` * fix example * apply suggestions from review * add relevant changes to axum-extra and axum-core changelogs * Add `route_service_with_tsr` * fix trybuild expectations * make sure `SpaRouter` works with routers that have state * Change order of type params on FromRequest and RequestParts * reverse order of `RequestParts::with_state` args to match type params * Add `FromRef` trait (#1268) * Add `FromRef` trait * Remove unnecessary type params * format * fix docs link * format examples * Avoid unnecessary `MethodRouter` * apply suggestions from review Co-authored-by: Dani Pardo <[email protected]> Co-authored-by: Jonas Platte <[email protected]>
1 parent 90dbd52 commit 423308d

File tree

132 files changed

+2404
-1126
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

132 files changed

+2404
-1126
lines changed

axum-core/CHANGELOG.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
# Unreleased
99

10-
- None.
10+
- **breaking:** `FromRequest` and `RequestParts` has a new `S` type param which
11+
represents the state ([#1155])
12+
13+
[#1155]: https://github.com/tokio-rs/axum/pull/1155
1114

1215
# 0.2.6 (18. June, 2022)
1316

axum-core/src/extract/from_ref.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/// Used to do reference-to-value conversions thus not consuming the input value.
2+
///
3+
/// This is mainly used with [`State`] to extract "substates" from a reference to main application
4+
/// state.
5+
///
6+
/// See [`State`] for more details on how library authors should use this trait.
7+
///
8+
/// [`State`]: https://docs.rs/axum/0.6/axum/extract/struct.State.html
9+
// NOTE: This trait is defined in axum-core, even though it is mainly used with `State` which is
10+
// defined in axum. That allows crate authors to use it when implementing extractors.
11+
pub trait FromRef<T> {
12+
/// Converts to this type from a reference to the input type.
13+
fn from_ref(input: &T) -> Self;
14+
}
15+
16+
impl<T> FromRef<T> for T
17+
where
18+
T: Clone,
19+
{
20+
fn from_ref(input: &T) -> Self {
21+
input.clone()
22+
}
23+
}

axum-core/src/extract/mod.rs

+52-17
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ use std::convert::Infallible;
1212

1313
pub mod rejection;
1414

15+
mod from_ref;
1516
mod request_parts;
1617
mod tuple;
1718

19+
pub use self::from_ref::FromRef;
20+
1821
/// Types that can be created from requests.
1922
///
2023
/// See [`axum::extract`] for more details.
@@ -42,13 +45,15 @@ mod tuple;
4245
/// struct MyExtractor;
4346
///
4447
/// #[async_trait]
45-
/// impl<B> FromRequest<B> for MyExtractor
48+
/// impl<S, B> FromRequest<S, B> for MyExtractor
4649
/// where
47-
/// B: Send, // required by `async_trait`
50+
/// // these bounds are required by `async_trait`
51+
/// B: Send,
52+
/// S: Send,
4853
/// {
4954
/// type Rejection = http::StatusCode;
5055
///
51-
/// async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
56+
/// async fn from_request(req: &mut RequestParts<S, B>) -> Result<Self, Self::Rejection> {
5257
/// // ...
5358
/// # unimplemented!()
5459
/// }
@@ -60,20 +65,21 @@ mod tuple;
6065
/// [`http::Request<B>`]: http::Request
6166
/// [`axum::extract`]: https://docs.rs/axum/latest/axum/extract/index.html
6267
#[async_trait]
63-
pub trait FromRequest<B>: Sized {
68+
pub trait FromRequest<S, B>: Sized {
6469
/// If the extractor fails it'll use this "rejection" type. A rejection is
6570
/// a kind of error that can be converted into a response.
6671
type Rejection: IntoResponse;
6772

6873
/// Perform the extraction.
69-
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection>;
74+
async fn from_request(req: &mut RequestParts<S, B>) -> Result<Self, Self::Rejection>;
7075
}
7176

7277
/// The type used with [`FromRequest`] to extract data from requests.
7378
///
7479
/// Has several convenience methods for getting owned parts of the request.
7580
#[derive(Debug)]
76-
pub struct RequestParts<B> {
81+
pub struct RequestParts<S, B> {
82+
state: S,
7783
method: Method,
7884
uri: Uri,
7985
version: Version,
@@ -82,15 +88,28 @@ pub struct RequestParts<B> {
8288
body: Option<B>,
8389
}
8490

85-
impl<B> RequestParts<B> {
86-
/// Create a new `RequestParts`.
91+
impl<B> RequestParts<(), B> {
92+
/// Create a new `RequestParts` without any state.
8793
///
8894
/// You generally shouldn't need to construct this type yourself, unless
8995
/// using extractors outside of axum for example to implement a
9096
/// [`tower::Service`].
9197
///
9298
/// [`tower::Service`]: https://docs.rs/tower/lastest/tower/trait.Service.html
9399
pub fn new(req: Request<B>) -> Self {
100+
Self::with_state((), req)
101+
}
102+
}
103+
104+
impl<S, B> RequestParts<S, B> {
105+
/// Create a new `RequestParts` with the given state.
106+
///
107+
/// You generally shouldn't need to construct this type yourself, unless
108+
/// using extractors outside of axum for example to implement a
109+
/// [`tower::Service`].
110+
///
111+
/// [`tower::Service`]: https://docs.rs/tower/lastest/tower/trait.Service.html
112+
pub fn with_state(state: S, req: Request<B>) -> Self {
94113
let (
95114
http::request::Parts {
96115
method,
@@ -104,6 +123,7 @@ impl<B> RequestParts<B> {
104123
) = req.into_parts();
105124

106125
RequestParts {
126+
state,
107127
method,
108128
uri,
109129
version,
@@ -130,18 +150,25 @@ impl<B> RequestParts<B> {
130150
/// use http::{Method, Uri};
131151
///
132152
/// #[async_trait]
133-
/// impl<B: Send> FromRequest<B> for MyExtractor {
153+
/// impl<S, B> FromRequest<S, B> for MyExtractor
154+
/// where
155+
/// B: Send,
156+
/// S: Send,
157+
/// {
134158
/// type Rejection = Infallible;
135159
///
136-
/// async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Infallible> {
160+
/// async fn from_request(req: &mut RequestParts<S, B>) -> Result<Self, Infallible> {
137161
/// let method = req.extract::<Method>().await?;
138162
/// let path = req.extract::<Uri>().await?.path().to_owned();
139163
///
140164
/// todo!()
141165
/// }
142166
/// }
143167
/// ```
144-
pub async fn extract<E: FromRequest<B>>(&mut self) -> Result<E, E::Rejection> {
168+
pub async fn extract<E>(&mut self) -> Result<E, E::Rejection>
169+
where
170+
E: FromRequest<S, B>,
171+
{
145172
E::from_request(self).await
146173
}
147174

@@ -153,6 +180,7 @@ impl<B> RequestParts<B> {
153180
/// [`take_body`]: RequestParts::take_body
154181
pub fn try_into_request(self) -> Result<Request<B>, BodyAlreadyExtracted> {
155182
let Self {
183+
state: _,
156184
method,
157185
uri,
158186
version,
@@ -245,30 +273,37 @@ impl<B> RequestParts<B> {
245273
pub fn take_body(&mut self) -> Option<B> {
246274
self.body.take()
247275
}
276+
277+
/// Get a reference to the state.
278+
pub fn state(&self) -> &S {
279+
&self.state
280+
}
248281
}
249282

250283
#[async_trait]
251-
impl<T, B> FromRequest<B> for Option<T>
284+
impl<S, T, B> FromRequest<S, B> for Option<T>
252285
where
253-
T: FromRequest<B>,
286+
T: FromRequest<S, B>,
254287
B: Send,
288+
S: Send,
255289
{
256290
type Rejection = Infallible;
257291

258-
async fn from_request(req: &mut RequestParts<B>) -> Result<Option<T>, Self::Rejection> {
292+
async fn from_request(req: &mut RequestParts<S, B>) -> Result<Option<T>, Self::Rejection> {
259293
Ok(T::from_request(req).await.ok())
260294
}
261295
}
262296

263297
#[async_trait]
264-
impl<T, B> FromRequest<B> for Result<T, T::Rejection>
298+
impl<S, T, B> FromRequest<S, B> for Result<T, T::Rejection>
265299
where
266-
T: FromRequest<B>,
300+
T: FromRequest<S, B>,
267301
B: Send,
302+
S: Send,
268303
{
269304
type Rejection = Infallible;
270305

271-
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
306+
async fn from_request(req: &mut RequestParts<S, B>) -> Result<Self, Self::Rejection> {
272307
Ok(T::from_request(req).await)
273308
}
274309
}

axum-core/src/extract/request_parts.rs

+26-17
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@ use http::{Extensions, HeaderMap, Method, Request, Uri, Version};
66
use std::convert::Infallible;
77

88
#[async_trait]
9-
impl<B> FromRequest<B> for Request<B>
9+
impl<S, B> FromRequest<S, B> for Request<B>
1010
where
1111
B: Send,
12+
S: Clone + Send,
1213
{
1314
type Rejection = BodyAlreadyExtracted;
1415

15-
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
16+
async fn from_request(req: &mut RequestParts<S, B>) -> Result<Self, Self::Rejection> {
1617
let req = std::mem::replace(
1718
req,
1819
RequestParts {
20+
state: req.state().clone(),
1921
method: req.method.clone(),
2022
version: req.version,
2123
uri: req.uri.clone(),
@@ -30,37 +32,40 @@ where
3032
}
3133

3234
#[async_trait]
33-
impl<B> FromRequest<B> for Method
35+
impl<S, B> FromRequest<S, B> for Method
3436
where
3537
B: Send,
38+
S: Send,
3639
{
3740
type Rejection = Infallible;
3841

39-
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
42+
async fn from_request(req: &mut RequestParts<S, B>) -> Result<Self, Self::Rejection> {
4043
Ok(req.method().clone())
4144
}
4245
}
4346

4447
#[async_trait]
45-
impl<B> FromRequest<B> for Uri
48+
impl<S, B> FromRequest<S, B> for Uri
4649
where
4750
B: Send,
51+
S: Send,
4852
{
4953
type Rejection = Infallible;
5054

51-
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
55+
async fn from_request(req: &mut RequestParts<S, B>) -> Result<Self, Self::Rejection> {
5256
Ok(req.uri().clone())
5357
}
5458
}
5559

5660
#[async_trait]
57-
impl<B> FromRequest<B> for Version
61+
impl<S, B> FromRequest<S, B> for Version
5862
where
5963
B: Send,
64+
S: Send,
6065
{
6166
type Rejection = Infallible;
6267

63-
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
68+
async fn from_request(req: &mut RequestParts<S, B>) -> Result<Self, Self::Rejection> {
6469
Ok(req.version())
6570
}
6671
}
@@ -71,27 +76,29 @@ where
7176
///
7277
/// [`TypedHeader`]: https://docs.rs/axum/latest/axum/extract/struct.TypedHeader.html
7378
#[async_trait]
74-
impl<B> FromRequest<B> for HeaderMap
79+
impl<S, B> FromRequest<S, B> for HeaderMap
7580
where
7681
B: Send,
82+
S: Send,
7783
{
7884
type Rejection = Infallible;
7985

80-
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
86+
async fn from_request(req: &mut RequestParts<S, B>) -> Result<Self, Self::Rejection> {
8187
Ok(req.headers().clone())
8288
}
8389
}
8490

8591
#[async_trait]
86-
impl<B> FromRequest<B> for Bytes
92+
impl<S, B> FromRequest<S, B> for Bytes
8793
where
8894
B: http_body::Body + Send,
8995
B::Data: Send,
9096
B::Error: Into<BoxError>,
97+
S: Send,
9198
{
9299
type Rejection = BytesRejection;
93100

94-
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
101+
async fn from_request(req: &mut RequestParts<S, B>) -> Result<Self, Self::Rejection> {
95102
let body = take_body(req)?;
96103

97104
let bytes = crate::body::to_bytes(body)
@@ -103,15 +110,16 @@ where
103110
}
104111

105112
#[async_trait]
106-
impl<B> FromRequest<B> for String
113+
impl<S, B> FromRequest<S, B> for String
107114
where
108115
B: http_body::Body + Send,
109116
B::Data: Send,
110117
B::Error: Into<BoxError>,
118+
S: Send,
111119
{
112120
type Rejection = StringRejection;
113121

114-
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
122+
async fn from_request(req: &mut RequestParts<S, B>) -> Result<Self, Self::Rejection> {
115123
let body = take_body(req)?;
116124

117125
let bytes = crate::body::to_bytes(body)
@@ -126,13 +134,14 @@ where
126134
}
127135

128136
#[async_trait]
129-
impl<B> FromRequest<B> for http::request::Parts
137+
impl<S, B> FromRequest<S, B> for http::request::Parts
130138
where
131139
B: Send,
140+
S: Send,
132141
{
133142
type Rejection = Infallible;
134143

135-
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
144+
async fn from_request(req: &mut RequestParts<S, B>) -> Result<Self, Self::Rejection> {
136145
let method = unwrap_infallible(Method::from_request(req).await);
137146
let uri = unwrap_infallible(Uri::from_request(req).await);
138147
let version = unwrap_infallible(Version::from_request(req).await);
@@ -159,6 +168,6 @@ fn unwrap_infallible<T>(result: Result<T, Infallible>) -> T {
159168
}
160169
}
161170

162-
pub(crate) fn take_body<B>(req: &mut RequestParts<B>) -> Result<B, BodyAlreadyExtracted> {
171+
pub(crate) fn take_body<S, B>(req: &mut RequestParts<S, B>) -> Result<B, BodyAlreadyExtracted> {
163172
req.take_body().ok_or(BodyAlreadyExtracted)
164173
}

axum-core/src/extract/tuple.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ use async_trait::async_trait;
44
use std::convert::Infallible;
55

66
#[async_trait]
7-
impl<B> FromRequest<B> for ()
7+
impl<S, B> FromRequest<S, B> for ()
88
where
99
B: Send,
10+
S: Send,
1011
{
1112
type Rejection = Infallible;
1213

13-
async fn from_request(_: &mut RequestParts<B>) -> Result<(), Self::Rejection> {
14+
async fn from_request(_: &mut RequestParts<S, B>) -> Result<(), Self::Rejection> {
1415
Ok(())
1516
}
1617
}
@@ -21,14 +22,15 @@ macro_rules! impl_from_request {
2122
( $($ty:ident),* $(,)? ) => {
2223
#[async_trait]
2324
#[allow(non_snake_case)]
24-
impl<B, $($ty,)*> FromRequest<B> for ($($ty,)*)
25+
impl<S, B, $($ty,)*> FromRequest<S, B> for ($($ty,)*)
2526
where
26-
$( $ty: FromRequest<B> + Send, )*
27+
$( $ty: FromRequest<S, B> + Send, )*
2728
B: Send,
29+
S: Send,
2830
{
2931
type Rejection = Response;
3032

31-
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
33+
async fn from_request(req: &mut RequestParts<S, B>) -> Result<Self, Self::Rejection> {
3234
$( let $ty = $ty::from_request(req).await.map_err(|err| err.into_response())?; )*
3335
Ok(($($ty,)*))
3436
}

0 commit comments

Comments
 (0)