pub struct TypeHash { /* private fields */ }Expand description
Runtime identity of a type, stored as a hash of its full type name.
Used instead of std::any::TypeId because it can also be built for
types that only exist on the script side, from their name alone (see
TypeHash::raw). Comparison, ordering and hashing all use the hash
only, never the name.
assert_eq!(TypeHash::of::<i32>(), TypeHash::of::<i32>());
assert_ne!(TypeHash::of::<i32>(), TypeHash::of::<f32>());Implementations§
Source§impl TypeHash
impl TypeHash
Sourcepub const INVALID: Self
pub const INVALID: Self
Hash that no real type maps to, used as a null value.
This is also what TypeHash::default returns.
Sourcepub unsafe fn raw(name: &str) -> Self
pub unsafe fn raw(name: &str) -> Self
Builds a hash from a type name given at runtime.
§Safety
The name must be the full, qualified name of the type, the same name
TypeHash::of builds its hash from. A different name makes two views
of one type compare as different types. Type-erased containers use that
comparison to decide if a cast is safe.
Sourcepub unsafe fn raw_static(name: &'static str) -> Self
pub unsafe fn raw_static(name: &'static str) -> Self
Same as TypeHash::raw, but keeps the name for diagnostics when the
typehash_debug_name feature is on.
§Safety
Same as TypeHash::raw.
Sourcepub fn of_runtime(qualified_name: &str) -> Self
pub fn of_runtime(qualified_name: &str) -> Self
Builds the hash of a type that exists only on the script side, from its qualified name.
The name is salted with NUL bytes, which std::any::type_name never
produces. So a runtime type never gets the same hash as a Rust type, and
a runtime value never passes the check that reads it as a Rust type.
Reflection stays the only way to read such a value.
This is safe, unlike TypeHash::raw, because the hash it builds
cannot match a Rust type.
assert_eq!(
TypeHash::of_runtime("game::Player"),
TypeHash::of_runtime("game::Player")
);
assert_ne!(
TypeHash::of_runtime("game::Player"),
TypeHash::of_runtime("game::Vector")
);
assert_ne!(TypeHash::of_runtime("i32"), TypeHash::of::<i32>());Sourcepub fn is_valid(&self) -> bool
pub fn is_valid(&self) -> bool
Returns false only for TypeHash::INVALID.