d34f8fe2bbaec9cb1fe9d3c960344df5cf3270a5
[anymap] / src / unchecked_any.rs
1 use std::any::Any;
2 use std::mem;
3 use std::raw::TraitObject;
4
5 #[allow(missing_docs)] // Bogus warning (it’s not public outside the crate), ☹
6 pub trait UncheckedAnyExt {
7 unsafe fn downcast_ref_unchecked<T: 'static>(&self) -> &T;
8 unsafe fn downcast_mut_unchecked<T: 'static>(&mut self) -> &mut T;
9 unsafe fn downcast_unchecked<T: 'static>(self: Box<Self>) -> Box<T>;
10 }
11
12 impl UncheckedAnyExt for Any {
13 unsafe fn downcast_ref_unchecked<T: 'static>(&self) -> &T {
14 mem::transmute(mem::transmute::<_, TraitObject>(self).data)
15 }
16
17 unsafe fn downcast_mut_unchecked<T: 'static>(&mut self) -> &mut T {
18 mem::transmute(mem::transmute::<_, TraitObject>(self).data)
19 }
20
21 unsafe fn downcast_unchecked<T: 'static>(self: Box<Any>) -> Box<T> {
22 mem::transmute(mem::transmute::<_, TraitObject>(self).data)
23 }
24 }