You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
gives use the [right method](https://docs.rs/actix-web/0.7.17/actix_web/struct.App.html#method.resource) it seems.
24
-
The interesting part here is the closure type
25
-
(`FnOnce(&mut Resource<S>) -> R`)
26
-
because it tells use the `r` is a reference to a `Resource`.
27
-
Clicking on that
28
-
and scrolling down
29
-
leads us to [`Resource::with`](https://docs.rs/actix-web/0.7.17/actix_web/dev/struct.Resource.html#method.with).
30
-
Nice. But now it gets complicated.
31
-
32
-
The full signature for `with` is:
33
-
34
-
pub fn with<T, F, R>(&mut self, handler: F) where
35
-
F: WithFactory<T, S, R>,
36
-
R: Responder + 'static,
37
-
T: FromRequest<S> + 'static,
38
-
39
-
Let's unwrap that a bit.
40
-
The parameter we give to `with` has to be something that implements the `WithFactory` trait.
41
-
But now it gets weird:
42
-
This trait is private!
43
-
So, from the docs,
44
-
we can only infer that it has three type parameters.
45
-
The first is something that implements `FromRequest`,
46
-
the second (`S`) is probably some state (guessing from the name only),
47
-
and the last one is something that implements `Responder`.
48
-
So I'd guess we are dealing with something that
49
-
takes some data from a request,
50
-
some state,
51
-
and returns something new
52
-
that can be used as a response.
53
-
Sounds useful in the context of a web framework.
54
-
55
-
The part we are interested in is [`FromRequest`](https://docs.rs/actix-web/0.7.17/actix_web/trait.FromRequest.html).
56
-
This is a trait that abstracts over extracting data from a request structure
57
-
(its two methods are `from_request` and `extract`!).
58
-
59
-
This is a long docs page.
60
-
The part you ask about is almost at the bottom in the "Implementors" section.
61
-
For example,
62
-
[`impl<T, S> FromRequest<S> for Form<T>`](https://docs.rs/actix-web/0.7.17/actix_web/trait.FromRequest.html#impl-FromRequest%3CS%3E-18),
63
-
or [`impl<T, S> FromRequest<S> for Path<T>`](https://docs.rs/actix-web/0.7.17/actix_web/trait.FromRequest.html#impl-FromRequest%3CS%3E-20).
64
-
And this is basically all there is to it!
65
-
These types allow you to use them in a context
66
-
where you want to extract data from a request!
67
-
68
-
The concrete usage of that
69
-
and the way that the obscure `WithFactory` comes into play is also quite interesting.
70
-
I wrote above that "no code generation magic" was used
71
-
-- I might have lied a bit.
72
-
To support *multiple* parameters/extractors in the functions you pass to `with`
73
-
the `WithFactory` trait must be implemented for functions/closures
74
-
that have multiple parameters.
75
-
For that, the actix developers use [a macro](https://github.com/actix/actix-web/blob/0745a1a9f8d43840454c6aae24df5e2c6f781c36/src/with.rs#L291-L306) internally
76
-
to generate implementations of `WithFactory`
77
-
for functions that take tuples of up to 10 fields that implement `FromRequest`.
78
-
79
-
I couldn't find this documented in the API docs,
80
-
but the website contains user documentation, too,
81
-
and as mentioned above has a page on [Extractors](https://actix.rs/docs/extractors/)
82
-
with [this section](https://actix.rs/docs/extractors/#multiple-extractors)
83
-
showing an example of using a function with multiple extractors.
84
-
So, all in all,
85
-
this means that you can write `.with(index)` and have this functions:
0 commit comments