mirror of
https://github.com/tokio-rs/axum.git
synced 2025-01-05 18:27:07 +01:00
Move Extension
into root (#804)
This commit is contained in:
parent
a2b568c7c1
commit
094fd71d1a
4 changed files with 37 additions and 9 deletions
|
@ -1,20 +1,20 @@
|
||||||
use super::{rejection::*, FromRequest, RequestParts};
|
use crate::extract::{rejection::*, FromRequest, RequestParts};
|
||||||
use crate::response::IntoResponseParts;
|
use crate::response::IntoResponseParts;
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use axum_core::response::{IntoResponse, Response, ResponseParts};
|
use axum_core::response::{IntoResponse, Response, ResponseParts};
|
||||||
use std::ops::Deref;
|
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.
|
/// This is commonly used to share state across handlers.
|
||||||
///
|
///
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust,no_run
|
/// ```rust,no_run
|
||||||
/// use axum::{
|
/// use axum::{
|
||||||
/// extract::Extension,
|
|
||||||
/// routing::get,
|
|
||||||
/// Router,
|
/// Router,
|
||||||
|
/// Extension,
|
||||||
|
/// routing::get,
|
||||||
/// };
|
/// };
|
||||||
/// use std::sync::Arc;
|
/// 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
|
/// If the extension is missing it will reject the request with a `500 Internal
|
||||||
/// Server Error` response.
|
/// 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)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub struct Extension<T>(pub T);
|
pub struct Extension<T>(pub T);
|
||||||
|
|
||||||
|
@ -57,7 +78,7 @@ where
|
||||||
.get::<T>()
|
.get::<T>()
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| {
|
||||||
MissingExtension::from_err(format!(
|
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>()
|
std::any::type_name::<T>()
|
||||||
))
|
))
|
||||||
})
|
})
|
|
@ -12,7 +12,6 @@ pub mod rejection;
|
||||||
pub mod ws;
|
pub mod ws;
|
||||||
|
|
||||||
mod content_length_limit;
|
mod content_length_limit;
|
||||||
mod extension;
|
|
||||||
mod raw_query;
|
mod raw_query;
|
||||||
mod request_parts;
|
mod request_parts;
|
||||||
|
|
||||||
|
@ -23,7 +22,6 @@ pub use axum_core::extract::{FromRequest, RequestParts};
|
||||||
pub use self::{
|
pub use self::{
|
||||||
connect_info::ConnectInfo,
|
connect_info::ConnectInfo,
|
||||||
content_length_limit::ContentLengthLimit,
|
content_length_limit::ContentLengthLimit,
|
||||||
extension::Extension,
|
|
||||||
extractor_middleware::extractor_middleware,
|
extractor_middleware::extractor_middleware,
|
||||||
path::Path,
|
path::Path,
|
||||||
raw_query::RawQuery,
|
raw_query::RawQuery,
|
||||||
|
@ -34,6 +32,9 @@ pub use self::{
|
||||||
#[cfg(feature = "json")]
|
#[cfg(feature = "json")]
|
||||||
pub use crate::Json;
|
pub use crate::Json;
|
||||||
|
|
||||||
|
#[doc(no_inline)]
|
||||||
|
pub use crate::Extension;
|
||||||
|
|
||||||
#[cfg(feature = "form")]
|
#[cfg(feature = "form")]
|
||||||
mod form;
|
mod form;
|
||||||
|
|
||||||
|
|
|
@ -393,6 +393,7 @@
|
||||||
pub(crate) mod macros;
|
pub(crate) mod macros;
|
||||||
|
|
||||||
mod add_extension;
|
mod add_extension;
|
||||||
|
mod extension;
|
||||||
#[cfg(feature = "json")]
|
#[cfg(feature = "json")]
|
||||||
mod json;
|
mod json;
|
||||||
mod util;
|
mod util;
|
||||||
|
@ -419,6 +420,8 @@ pub use http;
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
pub use hyper::Server;
|
pub use hyper::Server;
|
||||||
|
|
||||||
|
#[doc(inline)]
|
||||||
|
pub use self::extension::Extension;
|
||||||
#[doc(inline)]
|
#[doc(inline)]
|
||||||
#[cfg(feature = "json")]
|
#[cfg(feature = "json")]
|
||||||
pub use self::json::Json;
|
pub use self::json::Json;
|
||||||
|
|
|
@ -11,6 +11,9 @@ pub mod sse;
|
||||||
#[cfg(feature = "json")]
|
#[cfg(feature = "json")]
|
||||||
pub use crate::Json;
|
pub use crate::Json;
|
||||||
|
|
||||||
|
#[doc(no_inline)]
|
||||||
|
pub use crate::Extension;
|
||||||
|
|
||||||
#[doc(inline)]
|
#[doc(inline)]
|
||||||
pub use axum_core::response::{IntoResponse, IntoResponseParts, Response, ResponseParts};
|
pub use axum_core::response::{IntoResponse, IntoResponseParts, Response, ResponseParts};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue