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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*!

## 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, TraitcastTo, 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; 
}

// Invoking `traitcast_to_trait!` implements TraitcastTo for this Foo, allowing 
// other trait objects to be cast into Foo trait objects.
traitcast::traitcast_to_trait!(Foo, Foo_Traitcast);

trait Bar: TraitcastFrom {
    fn bar(&mut self) -> i32;
}

traitcast::traitcast_to_trait!(Bar, Bar_Traitcast);

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_to_impl!(Foo, A);
traitcast::traitcast_to_impl!(Bar, A);

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);
    }
}
```

*/

use std::any::Any;

pub mod private;
#[cfg(test)]
pub mod tests;

use private::get_impl_table;

/// Subtraits of `TraitcastFrom` may be cast into `dyn Any`, and thus may be 
/// cast into any other castable dynamic trait object, too. This is blanket 
/// implemented for all sized types with static lifetimes.
pub trait TraitcastFrom {
    /// Cast to an immutable reference to a trait object.
    fn as_any_ref(&self) -> &dyn Any;

    /// Cast to a mutable reference to a trait object.
    fn as_any_mut(&mut self) -> &mut dyn Any;

    /// Cast to a boxed reference to a trait object.
    fn as_any_box(self: Box<Self>) -> Box<dyn Any>;

    /// Get the trait object's dynamic type id.
    fn type_id(&self) -> std::any::TypeId {
        self.as_any_ref().type_id()
    }
}

impl<T> TraitcastFrom for T where T: Sized + 'static {
    fn as_any_ref(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }

    fn as_any_box(self: Box<Self>) -> Box<dyn Any> {
        self
    }
}

impl TraitcastFrom for dyn Any {
    fn as_any_ref(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }

    fn as_any_box(self: Box<Self>) -> Box<dyn Any> {
        self
    }
}

/// A convenience trait with a blanket implementation that adds methods to cast
/// from any trait that implements TraitcastFrom, to any trait that implements
/// TraitcastTo.
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: TraitcastTo + ?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_to_impl!`.
    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_to_impl!`.
    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_to_impl!`.
    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_to_impl!`.
pub fn implements_trait<From, To>(x: &From) -> bool
    where From: TraitcastFrom + ?Sized,
          To: TraitcastTo + ?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_to_impl!`.
pub fn cast_box<From, To>(x: Box<From>) 
    -> Result<Box<To>, Box<dyn Any>>
    where From: TraitcastFrom + ?Sized,
          To: TraitcastTo + ?Sized + 'static
{
    let trait_map = get_impl_table::<To>().expect(
        "Calling cast_box to cast into an unregistered trait object");

    let x = x.as_any_box();

    // Must ensure we take the type id of what's in the box, not the type id of 
    // the box itself.
    let tid = (*x).type_id();

    let s = match trait_map.map.get(&tid) {
        Some(s) => s,
        None => return Err(x)
    };

    (s.cast_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_to_impl!`.
pub fn cast_mut<'a, From, To>(x: &'a mut From) -> Option<&'a mut To> 
    where From: TraitcastFrom + ?Sized,
          To: TraitcastTo + ?Sized + 'static
{
    let trait_map = get_impl_table::<To>().expect(
        "Calling cast_mut to cast into an unregistered trait object");
    let x = (*x).as_any_mut();
    let tid = (x as &dyn Any).type_id();
    let s = trait_map.map.get(&tid)?;
    (s.cast_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_to_impl!`.
pub fn cast_ref<'a, From, To>(x: &'a From) -> Option<&'a To> 
    where From: TraitcastFrom + ?Sized,
          To: TraitcastTo + ?Sized + 'static
{
    let trait_map = get_impl_table::<To>().expect(
        "Calling cast_ref to cast into an unregistered trait object");
    let x = (*x).as_any_ref();
    let tid = x.type_id();
    let s = trait_map.map.get(&tid)?;
    (s.cast_ref)(x)
}

/// Trait objects that can be cast into implement this trait.
pub unsafe trait TraitcastTo {
    type ImplEntryWrapper: From<private::ImplEntry<Self>>;
}

/// Register a trait to allow it to be cast into. Cannot cast from implementing 
/// structs unless `traitcast_to_impl!` is also invoked for that struct.
///
/// This macro may only be used on traits defined in the same module.
#[macro_export]
macro_rules! traitcast_to_trait {
    ($trait:ident, $wrapper:ident) => {

        #[allow(non_camel_case_types)]
        pub struct $wrapper(pub $crate::private::ImplEntry<dyn $trait>);

        inventory::collect!($wrapper);

        impl std::convert::From<$crate::private::ImplEntry<dyn $trait>> 
            for $wrapper 
        {
            fn from(x: $crate::private::ImplEntry<dyn $trait>) -> Self {
                $wrapper(x)
            }
        }

        unsafe impl $crate::TraitcastTo for dyn $trait {
            type ImplEntryWrapper = $wrapper;
        }

        inventory::submit! {
            $crate::private::TraitEntryBuilder {
                insert: |master| {
                    master.insert::<$crate::private::TraitImplTable<dyn $trait>>(
                        $crate::private::TraitImplTable {
                            map: inventory::iter::<$wrapper>
                                .into_iter()
                                .map(|x| (x.0.tid, &x.0))
                                .collect()
                        });
                }
            }
        }

    }
}

/// Register an implementation of a castable trait for a particular struct. The 
/// struct must implement the trait. This enables objects of this type to be
/// cast into dynamic trait references of this trait, via an Any pointer.
/// It is best not to invoke traitcast_to_impl! multiple times for the same 
/// implementation. This will have no effect but to slightly slow down program 
/// load time.
///
/// This macro should only be used on structs defined in the same module.
#[macro_export]
macro_rules! traitcast_to_impl {
    ($trait:ident, $struct:ident) => {
        inventory::submit! {
            let imp = $crate::private::ImplEntry::<dyn $trait> {
                cast_box: |x| {
                    let x: Box<$struct> = x.downcast()?;
                    let x: Box<dyn $trait> = x;
                    Ok(x)
                },
                cast_mut: |x| {
                    let x: &mut $struct = x.downcast_mut()?;
                    let x: &mut dyn $trait = x;
                    Some(x)
                },
                cast_ref: |x| {
                    let x: &$struct = x.downcast_ref()?;
                    let x: &dyn $trait = x;
                    Some(x)
                },
                tid: std::any::TypeId::of::<$struct>()
            };
            type IEW = <dyn $trait as $crate::TraitcastTo>::ImplEntryWrapper;
            IEW::from(imp)
        }
    }
}