From 5407247e90c3e6439bfd9050ce969f441b2fefad Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Sat, 31 Jul 2021 14:54:10 +0200 Subject: [PATCH] Implement `Deref` for extractors (#56) Fixes https://github.com/tokio-rs/axum/issues/54 --- CHANGELOG.md | 1 + src/extract/mod.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 67c48bb8..8ca21426 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Implement `Stream` for `WebSocket`. - Implement `Sink` for `WebSocket`. +- Implement `Deref` most extractors. ## Breaking changes diff --git a/src/extract/mod.rs b/src/extract/mod.rs index 323c505f..d6a7d804 100644 --- a/src/extract/mod.rs +++ b/src/extract/mod.rs @@ -254,6 +254,7 @@ use serde::de::DeserializeOwned; use std::{ collections::HashMap, convert::Infallible, + ops::Deref, pin::Pin, str::FromStr, task::{Context, Poll}, @@ -596,6 +597,14 @@ where } } +impl Deref for Query { + type Target = T; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + /// Extractor that deserializes `application/x-www-form-urlencoded` requests /// into some type. /// @@ -667,6 +676,14 @@ where } } +impl Deref for Form { + type Target = T; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + /// Extractor that deserializes request bodies into some type. /// /// `T` is expected to implement [`serde::Deserialize`]. @@ -731,6 +748,14 @@ where } } +impl Deref for Json { + type Target = T; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + fn has_content_type( req: &RequestParts, expected_content_type: &str, @@ -809,6 +834,14 @@ where } } +impl Deref for Extension { + type Target = T; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + #[async_trait] impl FromRequest for Bytes where @@ -1071,6 +1104,14 @@ where } } +impl Deref for ContentLengthLimit { + type Target = T; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + /// Extractor that will get captures from the URL. /// /// # Example @@ -1219,6 +1260,14 @@ macro_rules! impl_parse_url { impl_parse_url!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16); +impl Deref for UrlParams { + type Target = T; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + fn take_body(req: &mut RequestParts) -> Result { req.take_body().ok_or(BodyAlreadyExtracted) } @@ -1274,6 +1323,16 @@ where } } +#[cfg(feature = "headers")] +#[cfg_attr(docsrs, doc(cfg(feature = "headers")))] +impl Deref for TypedHeader { + type Target = T; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + /// Extractor that extracts the raw query string, without parsing it. /// /// # Example