Skip to main content

FromQuery

Trait FromQuery 

Source
pub trait FromQuery {
    // Required method
    fn from_query(query: &str) -> Self;
}
Expand description

Something that can be created from an entire query string. This trait must be implemented for any type that is spread into the query segment like #[route("/?:..query")].

This trait is automatically implemented for any types that implement From<&str>.

use dioxus::prelude::*;

#[derive(Routable, Clone, PartialEq, Debug)]
enum Route {
    // FromQuery must be implemented for any types you spread into the query segment
    #[route("/?:..query")]
    Home {
        query: CustomQuery
    },
}

#[derive(Default, Clone, PartialEq, Debug)]
struct CustomQuery {
    count: i32,
}

// We implement From<&str> for CustomQuery so that FromQuery is implemented automatically
impl From<&str> for CustomQuery {
    fn from(query: &str) -> Self {
        CustomQuery {
            count: query.parse().unwrap_or(0),
        }
    }
}

// We also need to implement Display for CustomQuery which will be used to format the query string into the URL
impl std::fmt::Display for CustomQuery {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.count)
    }
}

Required Methods§

Source

fn from_query(query: &str) -> Self

Create an instance of Self from a query string.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<T> FromQuery for T
where T: for<'a> From<&'a str>,