pub trait FromHashFragment {
// Required method
fn from_hash_fragment(hash: &str) -> Self;
}
Expand description
Something that can be created from an entire hash fragment. This must be implemented for any type that is used as a hash fragment like #[route("/#:hash_fragment")]
.
This trait is automatically implemented for any types that implement FromStr
and Default
.
§Example
use dioxus::prelude::*;
#[derive(Routable, Clone)]
#[rustfmt::skip]
enum Route {
// State is stored in the url hash
#[route("/#:url_hash")]
Home {
url_hash: State,
},
}
#[component]
fn Home(url_hash: State) -> Element {
unimplemented!()
}
#[derive(Clone, PartialEq, Default)]
struct State {
count: usize,
other_count: usize
}
// The hash segment will be displayed as a string (this will be url encoded automatically)
impl std::fmt::Display for State {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}-{}", self.count, self.other_count)
}
}
// We need to parse the hash fragment into a struct from the string (this will be url decoded automatically)
impl FromHashFragment for State {
fn from_hash_fragment(hash: &str) -> Self {
let Some((first, second)) = hash.split_once('-') else {
// URL fragment parsing shouldn't fail. You can return a default value if you want
return Default::default();
};
let first = first.parse().unwrap();
let second = second.parse().unwrap();
State {
count: first,
other_count: second,
}
}
}
Required Methods§
Sourcefn from_hash_fragment(hash: &str) -> Self
fn from_hash_fragment(hash: &str) -> Self
Create an instance of Self
from a hash fragment.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.