Make blanket implementation of FromRef more general

This commit is contained in:
Jonas Platte 2022-09-30 11:13:46 +02:00
parent b94248191e
commit 7d33853391
No known key found for this signature in database
GPG key ID: CC154DE0E30B7C67
3 changed files with 8 additions and 5 deletions

View file

@ -1,3 +1,5 @@
use std::borrow::Borrow;
/// Used to do reference-to-value conversions thus not consuming the input value. /// 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 /// 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; fn from_ref(input: &T) -> Self;
} }
impl<T> FromRef<T> for T impl<T, U> FromRef<T> for U
where where
T: Clone, T: Borrow<U>,
U: Clone,
{ {
fn from_ref(input: &T) -> Self { fn from_ref(input: &T) -> Self {
input.clone() input.borrow().clone()
} }
} }

View file

@ -469,7 +469,7 @@ async fn merging_routes_different_paths_different_states() {
#[tokio::test] #[tokio::test]
async fn inherit_state_via_merge() { async fn inherit_state_via_merge() {
let foo = Router::inherit_state().route( let foo = Router::<&'static str>::inherit_state().route(
"/foo", "/foo",
get(|State(state): State<&'static str>| async move { state }), get(|State(state): State<&'static str>| async move { state }),
); );

View file

@ -473,7 +473,7 @@ async fn nesting_with_different_state() {
#[tokio::test] #[tokio::test]
async fn inherit_state_via_nest() { async fn inherit_state_via_nest() {
let foo = Router::inherit_state().route( let foo = Router::<&'static str>::inherit_state().route(
"/foo", "/foo",
get(|State(state): State<&'static str>| async move { state }), get(|State(state): State<&'static str>| async move { state }),
); );