I have a plugin system where I pass &dyn Any
to a dynamically loaded rust function, but downcasting the reference fails because the TypeId
s differ (for the same type), although I added rustflags = ["-Cmetadata=12345678"]
to both crates' cargo config. Also it seems as if only types from external crates are affected (I tried ()
and it yielded the same TypeId
in both crates). I am currently casting the raw pointers (unsafe { &*(v as *const dyn Any as *const Type) }
) to work around this issue, but I would prefer a solution without unsafe code.
For example the following code:
println!("CRATE 1: TypeId of `()`: `{:?}`, TypeId of `toml::Value`: `{:?}`",
TypeId::of::<()>(), TypeId::of::<toml::Value>());
produces this output:
CRATE 1: TypeId of `()`: `TypeId { t: 7549865886324542212 }`, TypeId of `toml::Value`: `TypeId { t: 9270396907601429078 }`
CRATE 2: TypeId of `()`: `TypeId { t: 7549865886324542212 }`, TypeId of `toml::Value`: `TypeId { t: 5704635987193303200 }`
EDIT: This does not seem to be a problem with different dependency versions, as crate 2 (which is dynamically loaded) depends on crate 3 (which is also dynamically loaded) and the problem still occurs, although both, crate 2 and crate 3, are local dependencies (so there is only one version). Crate 1 is btw. the crate that loads crate 2 & 3.
EDIT:
I removed the -Cmetadata
option from all 3 crates and now I get the same TypeId
for toml::Value
, however the TypeId
of a type in crate 1 that I want to downcast in crate 2 still differs.