From ce8da2ed71c0d89d2f3ce025b859abff9ff49dde Mon Sep 17 00:00:00 2001 From: Bryan Woods Date: Fri, 13 Dec 2024 00:56:15 -0800 Subject: [PATCH] Allow `ui_id!`s to seed other `ui_id!`s MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The possible `panic` form the `try_from(…).unwrap()` is optimized out for all unsigned types. --- composable-views/src/ui_id/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/composable-views/src/ui_id/src/lib.rs b/composable-views/src/ui_id/src/lib.rs index cf78ad1..cfc5a12 100644 --- a/composable-views/src/ui_id/src/lib.rs +++ b/composable-views/src/ui_id/src/lib.rs @@ -15,8 +15,8 @@ use syn::parse::Parser; /// ``` /// use itertools::Itertools; /// (0..1000) -/// .map(|n| { return ui_id::ui_id!(n) }) -/// .collect::>() +/// .map(|n: usize| { return ui_id::ui_id!(n) }) +/// .collect::>() /// .into_iter() /// .combinations(2) /// .into_iter() @@ -37,7 +37,7 @@ pub fn ui_id(tokens: proc_macro::TokenStream) -> proc_macro::TokenStream { // If runtime parameters where passed into the macro, perform a 128-bit FNV-1a // mix-step to combine them with the current `ui_id` to generate a new one. let prime = 0x0000000001000000000000000000013Bu128; - #( hash = (hash ^ ((#exprs) as u128)).wrapping_mul(prime); )* + #( hash = (hash ^ u128::try_from(#exprs).unwrap()).wrapping_mul(prime); )* unsafe { std::num::NonZeroU128::new_unchecked(hash | 0x1u128) } }