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
/*!
This library provides some utility traits to make working with [`Any`] smoother.
This crate contains similiar functionality to the `downcast` crate, but simpler.

# Usage example
```
use as_any::{AsAny, Downcast};

struct Test;

trait Custom: AsAny {
    // whatever you like to put inside of your trait
}

// implements the downcasting methods
impl as_any::Downcast for dyn Custom {}
impl as_any::Downcast for dyn Custom + Send {}
impl as_any::Downcast for dyn Custom + Sync {}
impl as_any::Downcast for dyn Custom + Send + Sync {}

impl Custom for Test {}

fn lol() {
    let x = Test;
    let y: &dyn Custom = &x;
    y.downcast_ref::<Test>().unwrap();
}
```
**/

#![no_std]

#[cfg(feature = "alloc")]
extern crate alloc;

use core::{any::Any, fmt};

#[cfg(feature = "alloc")]
use alloc::boxed::Box;

/// This trait is an extension trait to [`Any`], and adds methods to retrieve a `&dyn Any`
pub trait AsAny: Any {
    fn as_any(&self) -> &dyn Any;
    fn as_any_mut(&mut self) -> &mut dyn Any;

    #[doc(hidden)]
    fn as_my_any(&self) -> &dyn AsAny;

    #[doc(hidden)]
    fn as_my_any_mut(&mut self) -> &mut dyn AsAny;

    /// Gets the type name of `self`
    fn type_name(&self) -> &'static str;
}

// source: https://github.com/chris-morgan/anymap/blob/master/src/any.rs
/// This trait is an extension trait to [`AsAny`], and adds methods for unchecked downcasts
pub trait UncheckedAnyExt: AsAny {
    unsafe fn downcast_ref_unchecked<T: AsAny>(&self) -> &T;
    unsafe fn downcast_mut_unchecked<T: AsAny>(&mut self) -> &mut T;
    #[cfg(feature = "alloc")]
    unsafe fn downcast_unchecked<T: AsAny>(self: Box<Self>) -> Box<T>;
}

#[cfg(feature = "alloc")]
/// A trait for the conversion of an object into a boxed trait object.
pub trait IntoBox<A: ?Sized + UncheckedAnyExt>: AsAny {
    /// Convert self into the appropriate boxed form.
    fn into_box(self) -> Box<A>;
}

impl<T: Any> AsAny for T {
    #[inline(always)]
    fn as_any(&self) -> &dyn Any {
        self
    }

    #[inline(always)]
    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }

    #[doc(hidden)]
    #[inline(always)]
    fn as_my_any(&self) -> &dyn AsAny {
        self
    }

    #[doc(hidden)]
    #[inline(always)]
    fn as_my_any_mut(&mut self) -> &mut dyn AsAny {
        self
    }

    #[inline(always)]
    fn type_name(&self) -> &'static str {
        core::any::type_name::<T>()
    }
}

pub trait Downcast: AsAny {
    /// Returns `true` if the boxed type is the same as `T`.
    ///
    /// Forward to the method defined on the type `Any`.
    #[inline]
    fn is<T>(&self) -> bool
    where
        T: AsAny,
    {
        self.as_any().is::<T>()
    }

    /// Forward to the method defined on the type `Any`.
    #[inline]
    fn downcast_ref<T>(&self) -> Option<&T>
    where
        T: AsAny,
    {
        self.as_any().downcast_ref()
    }

    /// Forward to the method defined on the type `Any`.
    #[inline]
    fn downcast_mut<T>(&mut self) -> Option<&mut T>
    where
        T: AsAny,
    {
        self.as_any_mut().downcast_mut()
    }

    #[inline]
    unsafe fn downcast_ref_unchecked<T: Any>(&self) -> &T {
        UncheckedAnyExt::downcast_ref_unchecked(self.as_my_any())
    }

    #[inline]
    unsafe fn downcast_mut_unchecked<T: Any>(&mut self) -> &mut T {
        UncheckedAnyExt::downcast_mut_unchecked(self.as_my_any_mut())
    }
}

macro_rules! implement {
    ($base:ident $(+ $bounds:ident)*) => {
        impl fmt::Debug for dyn $base $(+ $bounds)* {
            #[inline]
            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                f.pad(stringify!($base $(+ $bounds)*))
            }
        }

        impl UncheckedAnyExt for dyn $base $(+ $bounds)* {
            #[inline]
            unsafe fn downcast_ref_unchecked<T: 'static>(&self) -> &T {
                &*(self as *const Self as *const T)
            }

            #[inline]
            unsafe fn downcast_mut_unchecked<T: 'static>(&mut self) -> &mut T {
                &mut *(self as *mut Self as *mut T)
            }

            #[cfg(feature = "alloc")]
            #[inline]
            unsafe fn downcast_unchecked<T: 'static>(self: Box<Self>) -> Box<T> {
                Box::from_raw(Box::into_raw(self) as *mut T)
            }
        }

        #[cfg(feature = "alloc")]
        impl<T: $base $(+ $bounds)*> IntoBox<dyn $base $(+ $bounds)*> for T {
            #[inline]
            fn into_box(self) -> Box<dyn $base $(+ $bounds)*> {
                Box::new(self)
            }
        }

        impl Downcast for dyn $base $(+ $bounds)* {}
    }
}

implement!(AsAny);
implement!(AsAny + Send);
implement!(AsAny + Sync);
implement!(AsAny + Send + Sync);