Skip to content

Commit

Permalink
Add FromParts impl example
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry Barber committed Oct 11, 2022
1 parent 63933d5 commit a47ba8b
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion design/src/server/anatomy.md
Original file line number Diff line number Diff line change
Expand Up @@ -840,4 +840,17 @@ pub struct Parts {
}
```

This is commonly used to access types stored within [`Extensions`](https://docs.rs/http/0.2.8/http/struct.Extensions.html) which have been inserted by a middleware.
This is commonly used to access types stored within [`Extensions`](https://docs.rs/http/0.2.8/http/struct.Extensions.html) which have been inserted by a middleware. Here is the `FromParts` implementation supporting this:

```rust
impl<Protocol, T> FromParts<Protocol> for Extension<T>
where
T: Clone + Send + Sync + 'static,
{
type Rejection = MissingExtension;

fn from_parts(parts: &mut http::request::Parts) -> Result<Self, Self::Rejection> {
parts.extensions.remove::<T>().map(Extension).ok_or(MissingExtension)
}
}
```

0 comments on commit a47ba8b

Please sign in to comment.