-impl Hasher<TypeIdState> for TypeIdHasher {
- fn hash<Sized? T: Hash<TypeIdState>>(&self, value: &T) -> u64 {
- let mut state = TypeIdState {
- value: 0,
- };
- value.hash(&mut state);
- state.value
- }
-}
-
-/// An extension of `AnyRefExt` allowing unchecked downcasting of trait objects to `&T`.
-trait UncheckedAnyRefExt<'a> {
- /// Returns a reference to the boxed value, assuming that it is of type `T`. This should only be
- /// called if you are ABSOLUTELY CERTAIN of `T` as you will get really wacky output if it’s not.
- unsafe fn downcast_ref_unchecked<T: 'static>(self) -> &'a T;
-}
-
-impl<'a> UncheckedAnyRefExt<'a> for &'a Any {
- #[inline]
- unsafe fn downcast_ref_unchecked<T: 'static>(self) -> &'a T {
- // Get the raw representation of the trait object
- let to: TraitObject = transmute(self);
-
- // Extract the data pointer
- transmute(to.data)
- }
-}
-
-/// An extension of `AnyMutRefExt` allowing unchecked downcasting of trait objects to `&mut T`.
-trait UncheckedAnyMutRefExt<'a> {
- /// Returns a reference to the boxed value, assuming that it is of type `T`. This should only be
- /// called if you are ABSOLUTELY CERTAIN of `T` as you will get really wacky output if it’s not.
- unsafe fn downcast_mut_unchecked<T: 'static>(self) -> &'a mut T;
-}
-
-impl<'a> UncheckedAnyMutRefExt<'a> for &'a mut Any {
- #[inline]
- unsafe fn downcast_mut_unchecked<T: 'static>(self) -> &'a mut T {
- // Get the raw representation of the trait object
- let to: TraitObject = transmute(self);
-
- // Extract the data pointer
- transmute(to.data)
- }
-}
-
-/// An extension of `BoxAny` allowing unchecked downcasting of trait objects to `Box<T>`.
-trait UncheckedBoxAny {
- /// Returns the boxed value, assuming that it is of type `T`. This should only be called if you
- /// are ABSOLUTELY CERTAIN of `T` as you will get really wacky output if it’s not.
- unsafe fn downcast_unchecked<T: 'static>(self) -> Box<T>;
-}
-
-impl UncheckedBoxAny for Box<Any + 'static> {
- #[inline]
- unsafe fn downcast_unchecked<T: 'static>(self) -> Box<T> {
- // Get the raw representation of the trait object
- let to: TraitObject = *transmute::<&Box<Any>, &TraitObject>(&self);
-
- // Prevent destructor on self being run
- forget(self);
-
- // Extract the data pointer
- transmute(to.data)
- }
-}