From 094fd71d1aa61e0aa6cefa5ee66b6f598c4f057b Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Tue, 1 Mar 2022 09:12:45 +0100 Subject: [PATCH] Move `Extension` into root (#804) --- axum/src/{extract => }/extension.rs | 35 +++++++++++++++++++++++------ axum/src/extract/mod.rs | 5 +++-- axum/src/lib.rs | 3 +++ axum/src/response/mod.rs | 3 +++ 4 files changed, 37 insertions(+), 9 deletions(-) rename axum/src/{extract => }/extension.rs (81%) diff --git a/axum/src/extract/extension.rs b/axum/src/extension.rs similarity index 81% rename from axum/src/extract/extension.rs rename to axum/src/extension.rs index 0a200600..8ca50d88 100644 --- a/axum/src/extract/extension.rs +++ b/axum/src/extension.rs @@ -1,20 +1,20 @@ -use super::{rejection::*, FromRequest, RequestParts}; +use crate::extract::{rejection::*, FromRequest, RequestParts}; use crate::response::IntoResponseParts; use async_trait::async_trait; use axum_core::response::{IntoResponse, Response, ResponseParts}; use std::ops::Deref; -/// Extractor that gets a value from request extensions. +/// Extractor and response for extensions. +/// +/// # As extractor /// /// This is commonly used to share state across handlers. /// -/// # Example -/// /// ```rust,no_run /// use axum::{ -/// extract::Extension, -/// routing::get, /// Router, +/// Extension, +/// routing::get, /// }; /// use std::sync::Arc; /// @@ -40,6 +40,27 @@ use std::ops::Deref; /// /// If the extension is missing it will reject the request with a `500 Internal /// Server Error` response. +/// +/// # As response +/// +/// Response extensions can be used to share state with middleware. +/// +/// ```rust +/// use axum::{ +/// Extension, +/// response::IntoResponse, +/// }; +/// +/// async fn handler() -> impl IntoResponse { +/// ( +/// Extension(Foo("foo")), +/// "Hello, World!" +/// ) +/// } +/// +/// #[derive(Clone)] +/// struct Foo(&'static str); +/// ``` #[derive(Debug, Clone, Copy)] pub struct Extension(pub T); @@ -57,7 +78,7 @@ where .get::() .ok_or_else(|| { MissingExtension::from_err(format!( - "Extension of type `{}` was not found. Perhaps you forgot to add it? See `axum::extract::Extension`.", + "Extension of type `{}` was not found. Perhaps you forgot to add it? See `axum::Extension`.", std::any::type_name::() )) }) diff --git a/axum/src/extract/mod.rs b/axum/src/extract/mod.rs index e7ccabd7..fcdc08df 100644 --- a/axum/src/extract/mod.rs +++ b/axum/src/extract/mod.rs @@ -12,7 +12,6 @@ pub mod rejection; pub mod ws; mod content_length_limit; -mod extension; mod raw_query; mod request_parts; @@ -23,7 +22,6 @@ pub use axum_core::extract::{FromRequest, RequestParts}; pub use self::{ connect_info::ConnectInfo, content_length_limit::ContentLengthLimit, - extension::Extension, extractor_middleware::extractor_middleware, path::Path, raw_query::RawQuery, @@ -34,6 +32,9 @@ pub use self::{ #[cfg(feature = "json")] pub use crate::Json; +#[doc(no_inline)] +pub use crate::Extension; + #[cfg(feature = "form")] mod form; diff --git a/axum/src/lib.rs b/axum/src/lib.rs index 357c1021..5f0b996a 100644 --- a/axum/src/lib.rs +++ b/axum/src/lib.rs @@ -393,6 +393,7 @@ pub(crate) mod macros; mod add_extension; +mod extension; #[cfg(feature = "json")] mod json; mod util; @@ -419,6 +420,8 @@ pub use http; #[doc(no_inline)] pub use hyper::Server; +#[doc(inline)] +pub use self::extension::Extension; #[doc(inline)] #[cfg(feature = "json")] pub use self::json::Json; diff --git a/axum/src/response/mod.rs b/axum/src/response/mod.rs index db5a20ea..1cfbdfba 100644 --- a/axum/src/response/mod.rs +++ b/axum/src/response/mod.rs @@ -11,6 +11,9 @@ pub mod sse; #[cfg(feature = "json")] pub use crate::Json; +#[doc(no_inline)] +pub use crate::Extension; + #[doc(inline)] pub use axum_core::response::{IntoResponse, IntoResponseParts, Response, ResponseParts};