default to Params::get() giving an owned value (which you want in a derived signal), but use reference in the macro

This commit is contained in:
Greg Johnston 2024-05-24 08:33:57 -04:00
parent 242d35cc37
commit 1766bfedb9
2 changed files with 11 additions and 3 deletions

View file

@ -20,7 +20,7 @@ pub fn params_impl(ast: &syn::DeriveInput) -> proc_macro::TokenStream {
quote_spanned! {
span=> #ident: <#ty as ::leptos_router::params::IntoParam>::into_param(
map.get(#field_name_string),
map.get_str(#field_name_string),
#field_name_string
)?
}

View file

@ -27,9 +27,17 @@ impl ParamsMap {
}
*/
/// Gets a value from the map.
/// Gets an owned value from the map.
#[inline(always)]
pub fn get(&self, key: &str) -> Option<&str> {
pub fn get(&self, key: &str) -> Option<String> {
self.0
.iter()
.find_map(|(k, v)| (k == key).then_some(v.to_owned()))
}
/// Gets a referenc to a value from the map.
#[inline(always)]
pub fn get_str(&self, key: &str) -> Option<&str> {
self.0
.iter()
.find_map(|(k, v)| (k == key).then_some(v.as_str()))