Skip to content

Commit 2a67cf3

Browse files
committed
factors: Replace some transmute with Any::downcast_*
Signed-off-by: Lann Martin <[email protected]>
1 parent b891d5c commit 2a67cf3

File tree

1 file changed

+5
-5
lines changed
  • crates/factors-derive/src

1 file changed

+5
-5
lines changed

crates/factors-derive/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ fn expand_factors(input: &DeriveInput) -> syn::Result<TokenStream> {
5353
factor_types.push(&field.ty);
5454
}
5555

56+
let Any = quote!(::std::any::Any);
5657
let TypeId = quote!(::std::any::TypeId);
5758
let factors_crate = format_ident!("spin_factors");
5859
let factors_path = quote!(::#factors_crate);
@@ -146,11 +147,10 @@ fn expand_factors(input: &DeriveInput) -> syn::Result<TokenStream> {
146147
type InstanceState = #state_name;
147148

148149
fn app_state<F: #Factor>(app_state: &Self::AppState) -> Option<&F::AppState> {
149-
let type_id = #TypeId::of::<F>();
150150
#(
151-
if type_id == #TypeId::of::<#factor_types>() {
152-
unsafe {
153-
return Some(::std::mem::transmute(&app_state.#factor_names));
151+
if let Some(state) = &app_state.#factor_names {
152+
if let Some(state) = <dyn #Any>::downcast_ref(state) {
153+
return Some(state)
154154
}
155155
}
156156
)*
@@ -165,7 +165,7 @@ fn expand_factors(input: &DeriveInput) -> syn::Result<TokenStream> {
165165
if type_id == #TypeId::of::<#factor_types>() {
166166
return Some(
167167
builders.#factor_names.as_mut().map(|builder| {
168-
unsafe { ::std::mem::transmute(builder) }
168+
<dyn #Any>::downcast_mut(builder).unwrap()
169169
})
170170
);
171171
}

0 commit comments

Comments
 (0)