1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
/*! ## Casting from `Any` In the standard library, the std::any::Any trait comes with downcast methods which let you cast from an `Any` trait object to a concrete type. ```rust let x: i32 = 7; let y: &dyn std::any::Any = &x; // Cast to i32 succeeds because x: i32 assert_eq!(y.downcast_ref::<i32>(), Some(&7)); // Cast to f32 fails assert_eq!(y.downcast_ref::<f32>(), None); ``` However, it is not possible to downcast to a trait object. ```compile_fail trait Foo { fn foo(&self) -> i32; } struct A { x: i32 } impl Foo for A { fn foo(&self) -> i32 { self.x } } let x = A { x: 7 }; let y: &dyn std::any::Any = &x; // This cast is not possible, because it is only possible to cast to types that // are Sized. Among other things, this precludes trait objects. let z: Option<&dyn Foo> = y.downcast_ref(); ``` ## Traitcast This library provides a way of casting between different trait objects. ```rust use traitcast::{TraitcastFrom, Traitcast}; // Extending `TraitcastFrom` is optional. This allows `Foo` objects themselves // to be cast to other trait objects. If you do not extend `TraitcastFrom`, // then Foo may only be cast into, not out of. trait Foo: TraitcastFrom { fn foo(&self) -> i32; } trait Bar: TraitcastFrom { fn bar(&mut self) -> i32; } struct A { x: i32 } // No implementation of TraitcastFrom is necessary, because it is covered by // the blanket impl for any sized type with a static lifetime. impl Foo for A { fn foo(&self) -> i32 { self.x } } impl Bar for A { fn bar(&mut self) -> i32 { self.x *= 2; self.x } } // Register the traits. // For each struct that implements each trait, register the implementation. traitcast::traitcast!(struct A: Foo, Bar); fn main() { let mut x = A { x: 7 }; { let x: &dyn Foo = &x; // Test whether x is of a type that implements Bar. assert!(traitcast::implements_trait::<dyn Foo, dyn Bar>(x)); } { let x: &dyn Bar = &x; // Cast an immutable reference using the `cast_ref` method (via the // `Traitcast` trait, which is blanket implemented for all pairs of // traits that may be cast between). let x: &dyn Foo = x.cast_ref().unwrap(); assert_eq!(x.foo(), 7); // We can also cast using the top-level `cast_ref` function, which can // be more convenient when type arguments cannot be inferred. assert!(traitcast::cast_ref::<dyn Foo, dyn Bar>(x).is_some()); } { let x: &mut dyn Foo = &mut x; // Cast a mutable reference using the `cast_mut` method let x: &mut dyn Bar = x.cast_mut().unwrap(); assert_eq!(x.bar(), 14); } { // We can cast from `Any` too! let y: Box<dyn std::any::Any> = Box::new(x); // Cast a boxed reference let z: Box<dyn Foo> = y.cast_box().unwrap(); assert_eq!(z.foo(), 14); } } ``` */ #[cfg(test)] pub mod tests; use std::any::Any; pub use traitcast_core::TraitcastFrom; use traitcast_core::inventory::build_registry; use traitcast_core::Registry; lazy_static::lazy_static! { /// This is a global table of all the trait objects that can be cast into. /// Each entry is a CastIntoTrait, i.e. a table of the implementations of a /// castable trait. static ref GLOBAL_REGISTRY: Registry = build_registry(); } /// A convenience trait with a blanket implementation that adds methods to cast /// from any trait that implements TraitcastFrom, to target with a static /// lifetime. pub trait Traitcast<To: ?Sized> { /// A convenience method that wraps the top-level `cast_ref` function. fn cast_ref(&self) -> Option<&To>; /// A convenience method that wraps the top-level `cast_mut` function. fn cast_mut(&mut self) -> Option<&mut To>; /// A convenience method that wraps the top-level `cast_box` function. fn cast_box(self: Box<Self>) -> Result<Box<To>, Box<dyn Any>>; } impl<From, To> Traitcast<To> for From where From: TraitcastFrom + ?Sized, To: ?Sized + 'static, { /// Tries to cast self to a different dynamic trait object. This will /// always return None if the implementation of the target trait, for the /// concrete type of self, has not been registered via /// `traitcast!`. fn cast_ref(&self) -> Option<&To> { cast_ref(self) } /// Tries to cast the self to a different dynamic trait object. This will /// always return None if the implementation of the target trait, for the /// concrete type of self, has not been registered via /// `traitcast!`. fn cast_mut(&mut self) -> Option<&mut To> { cast_mut(self) } /// Tries to cast self to a boxed dynamic trait object. This will always /// return Err if the implementation of the target trait, for the concrete /// type of self, has not been registered via `traitcast!`. fn cast_box(self: Box<Self>) -> Result<Box<To>, Box<dyn Any>> { cast_box(self) } } /// Tests whether the given value is castable to some trait object. This will /// always return `false` if the implementation of the target trait, for the /// concrete type of x, has not been registered via `traitcast!`. pub fn implements_trait<From, To>(x: &From) -> bool where From: TraitcastFrom + ?Sized, To: ?Sized + 'static, { cast_ref::<From, To>(x).is_some() } /// Tries to cast the given pointer to a dynamic trait object. This will always /// return Err if the implementation of the target trait, for the concrete type /// of x, has not been registered via `traitcast!`. pub fn cast_box<From, To>(x: Box<From>) -> Result<Box<To>, Box<dyn Any>> where From: TraitcastFrom + ?Sized, To: ?Sized + 'static, { GLOBAL_REGISTRY .cast_into::<To>() .expect("Calling cast_box to cast into an unregistered trait object") .from_box(x) } /// Tries to cast the given mutable reference to a dynamic trait object. This /// will always return None if the implementation of the target trait, for the /// concrete type of x, has not been registered via `traitcast!`. pub fn cast_mut<'a, From, To>(x: &'a mut From) -> Option<&'a mut To> where From: TraitcastFrom + ?Sized, To: ?Sized + 'static, { GLOBAL_REGISTRY .cast_into::<To>() .expect("Calling cast_mut to cast into an unregistered trait object") .from_mut(x) } /// Tries to cast the given reference to a dynamic trait object. This will /// always return None if the implementation of the target trait, for the /// concrete type of x, has not been registered via `traitcast!`. pub fn cast_ref<'a, From, To>(x: &'a From) -> Option<&'a To> where From: TraitcastFrom + ?Sized, To: ?Sized + 'static, { GLOBAL_REGISTRY .cast_into::<To>() .expect("Calling cast_ref to cast into an unregistered trait object") .from_ref(x) } /// `traitcast!(struct Bar)` registers a struct to allow it to be cast into. /// /// `traitcast!(impl Foo for Bar)` allows casting into dynamic `Foo` trait /// objects, from objects whose concrete type is `Bar`. /// /// `traitcast!(struct Bar: Foo1, Foo2)` registers a struct to allow it to be /// cast into, and further allows casting into dynamic `Foo1` or `Foo2` trait /// objects, from objects whose concrete type is `Bar`. #[macro_export] macro_rules! traitcast { (struct $type:ty) => { $crate::traitcast!($type => $type); }; (struct $type:ty : $($trait:ident),+) => { $crate::traitcast!(struct $type); $( $crate::traitcast!(impl $trait for $type); )+ }; (impl $trait:ident for $source:ty) => { $crate::traitcast!($source => dyn $trait); }; ($source:ty => $target:ty) => { inventory::submit! { traitcast_core::inventory::EntryBuilder::inserting_entry( traitcast_core::impl_entry!($target, $source)) } }; }