Move Extension into root (#804)

This commit is contained in:
David Pedersen 2022-03-01 09:12:45 +01:00 committed by GitHub
parent a2b568c7c1
commit 094fd71d1a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 9 deletions

View file

@ -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<T>(pub T);
@ -57,7 +78,7 @@ where
.get::<T>()
.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::<T>()
))
})

View file

@ -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;

View file

@ -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;

View file

@ -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};