Skip to main content

axum_core/extract/
from_ref.rs

1/// Used to do reference-to-value conversions thus not consuming the input value.
2///
3/// This is mainly used with [`State`] to extract "substates" from a reference to main application
4/// state. See ["Sharing state with handlers"][sharing-state] for a worked example.
5///
6/// See [`State`] for more details on how library authors should use this trait.
7///
8/// This trait can be derived using `#[derive(FromRef)]`.
9///
10/// [`State`]: https://docs.rs/axum/0.8/axum/extract/struct.State.html
11/// [sharing-state]: https://docs.rs/axum/0.8/axum/index.html#sharing-state-with-handlers
12// NOTE: This trait is defined in axum-core, even though it is mainly used with `State` which is
13// defined in axum. That allows crate authors to use it when implementing extractors.
14pub trait FromRef<T> {
15    /// Converts to this type from a reference to the input type.
16    fn from_ref(input: &T) -> Self;
17}
18
19impl<T> FromRef<T> for T
20where
21    T: Clone,
22{
23    fn from_ref(input: &T) -> Self {
24        input.clone()
25    }
26}