mirror of
https://github.com/tokio-rs/axum.git
synced 2024-11-21 22:56:46 +01:00
Make blanket implementation of FromRef more general
This commit is contained in:
parent
b94248191e
commit
7d33853391
3 changed files with 8 additions and 5 deletions
|
@ -1,3 +1,5 @@
|
|||
use std::borrow::Borrow;
|
||||
|
||||
/// Used to do reference-to-value conversions thus not consuming the input value.
|
||||
///
|
||||
/// This is mainly used with [`State`] to extract "substates" from a reference to main application
|
||||
|
@ -13,11 +15,12 @@ pub trait FromRef<T> {
|
|||
fn from_ref(input: &T) -> Self;
|
||||
}
|
||||
|
||||
impl<T> FromRef<T> for T
|
||||
impl<T, U> FromRef<T> for U
|
||||
where
|
||||
T: Clone,
|
||||
T: Borrow<U>,
|
||||
U: Clone,
|
||||
{
|
||||
fn from_ref(input: &T) -> Self {
|
||||
input.clone()
|
||||
input.borrow().clone()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -469,7 +469,7 @@ async fn merging_routes_different_paths_different_states() {
|
|||
|
||||
#[tokio::test]
|
||||
async fn inherit_state_via_merge() {
|
||||
let foo = Router::inherit_state().route(
|
||||
let foo = Router::<&'static str>::inherit_state().route(
|
||||
"/foo",
|
||||
get(|State(state): State<&'static str>| async move { state }),
|
||||
);
|
||||
|
|
|
@ -473,7 +473,7 @@ async fn nesting_with_different_state() {
|
|||
|
||||
#[tokio::test]
|
||||
async fn inherit_state_via_nest() {
|
||||
let foo = Router::inherit_state().route(
|
||||
let foo = Router::<&'static str>::inherit_state().route(
|
||||
"/foo",
|
||||
get(|State(state): State<&'static str>| async move { state }),
|
||||
);
|
||||
|
|
Loading…
Reference in a new issue