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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#![warn(missing_docs)]
#![warn(rust_2018_idioms)]
//! # Better Any
//!
//! Rust RFC for `non_static_type_id` feature has been reverted.
//! Which means in foreseeable future there will be no built-in way in rust to get type id for non-static type
//! let alone safely use it to downcast to a particular type.
//!
//! This crate provides tools to do these things safely for types with single lifetime.
//! Although looks like it is technically possible to extend this approach for multiple lifetimes,
//! consistent api and derive macro would be much harder to create and use because of the necessity
//! to properly handle lifetime relations.
//! Feel free to create an issue if you have actual use case where you need this functionality for multiple lifetimes.
//!
//! Also it has better downcasting that allows you do downcast not just from `dyn Tid` (like `dyn Any`) but from
//! any trait object that implements `Tid`.
//! So there is no more need to extend your traits with` fn to_any(&self)-> &dyn Any`
//!
//! MSRV: `1.41.0-stable`
//!
//! ### Usage
//!
//! Basically in places where before you have used `dyn Any` you can use `dyn Tid<'a>`
//!  - If your type is generic you should derive `Tid` implementation for it with `Tid` derive macro.
//! Then to retrieve back concrete type `<dyn Tid>::downcast_*` methods should be used.
//!  - If your type is not generic/implements Any you can create `dyn Any` and convert it to `dyn Tid`.
//! Then to retrieve back concrete type `<dyn Tid>::downcast_any_*` methods should be used
//!  - If your type is not generic and local to your crate you also can derive `Tid` but then you need to be careful
//! to use methods that corresponds to the way you create `dyn Tid` for that particular type.
//! Otherwise downcasting will return `None`.
//!
//! If all your types can implement `Tid` to avoid confusion
//! recommended way is to use first option even if some types implement `Any`.
//! If there are some types that implement `Any` and can't implement `Tid` (i.e. types from other library),
//! recommended way is to use second option for all types that implement `Any` to reduce confusion to minimum.
//!
//! ### Interoperability with Any
//!
//! Unfortunately you can't just use `Tid` everywhere because currently it is impossible
//! to implement `Tid` for `T:Any` since it would conflict with any other possible `Tid` implementation.
//! To overcome this limitation there is a `From` impl to go from `dyn Any` to `dyn Tid`
//! But `Any` and `Tid` deliberately return different type ids because otherwise `Type<'a>` and `Type<'static>`
//! would be indistinguishable and it would allow to go from `Type<'static>` to `Type<'a>` via `dyn Tid`
//! which is obviously unsound for invariant and contravariant structs.
//!
//! Although if you are using `dyn Trait` where `Trait:Tid` all of this wouldn't work,
//! and you are left with `Tid` only.
//!
//! ### Safety
//!
//! It is safe because created trait object preserve lifetime information,
//! thus allowing us to safely downcast with proper lifetime.
//! Otherwise internally it is plain old `Any`.
use std::any::{Any, TypeId};
use std::marker::PhantomData;

/// Attribute macro that makes your implementation of `TidAble` safe
/// Use it when you can't use derive e.g. for trait object.
///
/// ```rust
/// # use better_any::{TidAble,impl_tid};
/// trait Trait<'a>{}
/// #[impl_tid]
/// impl<'a> TidAble<'a> for Box<dyn Trait<'a> + 'a>{}
/// ```
pub use better_typeid_derive::impl_tid;
/// Derive macro to implement traits from this crate
///
/// It checks if it is safe to implement `Tid` for your struct
/// Also it adds `:TidAble<'a>` bound on type parameters
/// unless your type parameter already has **explicit** `'static` bound
pub use better_typeid_derive::Tid;

/// This trait indicates that you can substitute this type as a type parameter to
/// another type so that resulting type could implement `Tid`.
///
/// So if you don't have such generic types, just use `Tid` everywhere,
/// you don't need to use this trait at all.
///
/// Only this trait is actually being implemented on user side.
/// Other traits are mostly just blanket implementations over X:TidAble<'a>
///
/// Note that this trait interfere with object safety, so you shouldn't use it as a super trait
/// if you are going to make a trait object. Formally it is still object safe,
/// but you can't make a trait object from it without specifying internal associate type
/// like: `dyn TidAble<'a,Static=SomeType>` which make this trait object effectively useless.
///
/// Unsafe because safety of this crate relies on correctness of this trait implementation.
/// There are several safe ways to implement it:
///  - `#[derive(Tid)]` derive macro
///  - `type_id` declarative macro
///  - `impl_tid` attribute macro
// we need to have associate type because it allows TypeIdAdjuster to be a private type
// and allows to implement it for generic types
// it has lifetime and depends on Tid because it would be practically useless as a standalone trait
// because even though user would be able to get type id for more types,
// any action based on it would be unsound without checking on lifetimes
pub unsafe trait TidAble<'a>: Tid<'a> {
    /// Implementation detail
    #[doc(hidden)]
    type Static: ?Sized + Any;
}

/// Extension trait that contains actual downcasting methods.
///
/// Use methods from this trait only if `dyn Tid` was created directly from `T` for this particular `T`
pub trait TidExt<'a> {
    /// Returns true if type behind self is equal to the type of T.
    fn is<T: Tid<'a>>(&self) -> bool;

    /// Attempts to downcast self to `T` behind reference
    fn downcast_ref<'b, T: Tid<'a>>(&'b self) -> Option<&'b T>;

    /// Attempts to downcast self to `T` behind mutable reference
    fn downcast_mut<'b, T: Tid<'a>>(&'b mut self) -> Option<&'b mut T>;

    /// Attempts to downcast self to `T` behind `Rc` pointer
    fn downcast_rc<T: Tid<'a>>(self: Rc<Self>) -> Result<Rc<T>, Rc<Self>>;

    /// Attempts to downcast self to `T` behind `Arc` pointer
    fn downcast_arc<T: Tid<'a>>(self: Arc<Self>) -> Result<Arc<T>, Arc<Self>>;

    /// Attempts to downcast self to `T` behind `Box` pointer
    fn downcast_box<T: Tid<'a>>(self: Box<Self>) -> Result<Box<T>, Box<Self>>;
}

/// If X is Sized then any of those calls is optimized to no-op because both T and Self are known statically.
/// Useful if you have generic code that you want to behave differently depending on which
/// concrete type replaces type parameter. Usually there are better ways to do this like specialization,
/// but sometimes it can be the only way.
impl<'a, X: ?Sized + Tid<'a>> TidExt<'a> for X {
    fn is<T: Tid<'a>>(&self) -> bool {
        self.self_id() == T::id()
    }

    #[inline]
    fn downcast_ref<'b, T: Tid<'a>>(&'b self) -> Option<&'b T> {
        // Tid<'a> is implemented only for types with lifetime 'a
        // so we can safely cast type back because lifetime invariant is preserved.
        if self.self_id() == T::id() {
            Some(unsafe { &*(self as *const _ as *const T) })
        } else {
            None
        }
    }

    #[inline]
    fn downcast_mut<'b, T: Tid<'a>>(&'b mut self) -> Option<&'b mut T> {
        // see downcast_ref
        if self.self_id() == T::id() {
            Some(unsafe { &mut *(self as *mut _ as *mut T) })
        } else {
            None
        }
    }

    #[inline]
    fn downcast_rc<T: Tid<'a>>(self: Rc<Self>) -> Result<Rc<T>, Rc<Self>> {
        if T::id() == self.self_id() {
            unsafe { Ok(Rc::from_raw(Rc::into_raw(self) as *const _)) }
        } else {
            Err(self)
        }
    }
    #[inline]
    fn downcast_arc<T: Tid<'a>>(self: Arc<Self>) -> Result<Arc<T>, Arc<Self>> {
        if T::id() == self.self_id() {
            unsafe { Ok(Arc::from_raw(Arc::into_raw(self) as *const _)) }
        } else {
            Err(self)
        }
    }

    #[inline]
    fn downcast_box<T: Tid<'a>>(self: Box<Self>) -> Result<Box<T>, Box<Self>> {
        if T::id() == self.self_id() {
            unsafe { Ok(Box::from_raw(Box::into_raw(self) as *mut _)) }
        } else {
            Err(self)
        }
    }
}

/// This trait indicates that this type can be converted to
/// trait object with typeid while preserving lifetime information.
/// Extends `Any` functionality for types with single lifetime
///
/// Use it only as a `dyn Tid` or as super trait when you need to create trait object.
/// In all other places use `TidAble<'a>`.
///
/// Lifetime here is necessary to make `dyn Tid<'a> + 'a` invariant over `'a`.
pub unsafe trait Tid<'a>: 'a {
    /// Returns type id of the type of `self`
    ///
    /// Note that returned type id is guaranteed to be different from provided by `Any`.
    /// It is necessary for the creation of `dyn Tid` from `dyn Any` to be sound.
    fn self_id(&self) -> TypeId;

    /// Returns type id of this type
    fn id() -> TypeId
    where
        Self: Sized;
}

unsafe impl<'a, T: ?Sized + TidAble<'a>> Tid<'a> for T {
    #[inline]
    fn self_id(&self) -> TypeId {
        adjust_id::<T::Static>()
    }

    #[inline]
    fn id() -> TypeId
    where
        Self: Sized,
    {
        adjust_id::<T::Static>()
    }
}

// this exists just to make TypeIdAdjuster private so type id difference between
// `dyn Any` and `dyn Tid` would be guaranteed
#[inline(always)]
fn adjust_id<T: ?Sized + Any>() -> TypeId {
    TypeId::of::<TypeIdAdjuster<T>>()
}

/// Returns type id of `T`
///
/// Use it only if `Tid::id()` is not enough when `T` is not sized.
#[inline]
pub fn typeid_of<'a, T: ?Sized + TidAble<'a>>() -> TypeId {
    adjust_id::<T::Static>()
}

impl<'a> From<Box<dyn Any>> for Box<dyn Tid<'a> + 'a> {
    #[inline]
    fn from(f: Box<dyn Any>) -> Self {
        // it should be safe because both Any and Tid have single entry in vtable
        // so there is no need to rely on the function order stability of the different traits
        // also, despite particular trait object layout is not stable, it still should be
        // the same for different trait objects in the same compilation pass.
        //todo find out more:
        // technically i think vtable also has a drop entry so different order still can happen
        // in theory but practically it is either always first or always last so it
        // shouldn't influence order
        unsafe { core::mem::transmute(f) }
    }
}

impl<'a: 'b, 'b> From<&'b dyn Any> for &'b (dyn Tid<'a> + 'a) {
    #[inline]
    fn from(f: &'b dyn Any) -> Self {
        unsafe { core::mem::transmute(f) }
    }
}

impl<'a: 'b, 'b> From<&'b mut dyn Any> for &'b mut (dyn Tid<'a> + 'a) {
    #[inline]
    fn from(f: &'b mut dyn Any) -> Self {
        unsafe { core::mem::transmute(f) }
    }
}

// Reverse is possible only for 'static
// because otherwise even though user can't access type with lifetime because of different type id
// drop still can be called after the end of lifetime.
impl Into<Box<dyn Any>> for Box<dyn Tid<'static>> {
    fn into(self) -> Box<dyn Any> {
        unsafe { core::mem::transmute(self) }
    }
}

//wrapper to distinguish type ids coming from `dyn Any` and `dyn Tid`
struct TypeIdAdjuster<T: ?Sized>(PhantomData<T>);

impl<'a> dyn Tid<'a> + 'a {
    /// Tries to downcast Self to `T`
    ///
    /// Use it only if `dyn Tid` was created from `dyn Any` for this particular `T`
    ///
    /// ```rust
    /// # use std::any::Any;
    /// # use better_any::{Tid, TidAble, TidExt};
    /// #[derive(Tid)]
    /// struct S;
    ///
    /// let a = &S as &dyn Any;
    /// let from_any: &dyn Tid = a.into();
    /// assert!(from_any.downcast_any_ref::<S>().is_some());
    /// assert!(from_any.downcast_ref::<S>().is_none());
    ///
    /// let direct = &S as &dyn Tid;
    /// assert!(direct.downcast_any_ref::<S>().is_none());
    /// assert!(direct.downcast_ref::<S>().is_some());
    /// ```
    #[inline]
    pub fn downcast_any_ref<T: Any>(&self) -> Option<&T> {
        // this condition can be true if and only if dyn Tid was created from dyn Any
        // because otherwise TypeId would be from TypeIdAdjuster<T> which cant be equal to the one of T
        // thus it is safe to increase lifetime back to 'static
        if self.self_id() == TypeId::of::<T>() {
            Some(unsafe { &*(self as *const _ as *const T) })
        } else {
            None
        }
    }

    /// Use it only if `dyn Tid` was created from `dyn Any` for this particular `T`
    #[inline]
    pub fn downcast_any_mut<T: Any>(&mut self) -> Option<&mut T> {
        // see downcast_any_ref
        if self.self_id() == TypeId::of::<T>() {
            Some(unsafe { &mut *(self as *mut _ as *mut T) })
        } else {
            None
        }
    }
}

macro_rules! stdimpl {
    ($struct: tt) => {
        unsafe impl<'a, T: ?Sized + TidAble<'a>> TidAble<'a> for $struct<T> {
            type Static = $struct<T::Static>;
        }
    };
}
use std::cell::*;
use std::rc::*;
use std::sync::*;
stdimpl!(Box);
stdimpl!(Rc);
stdimpl!(RefCell);
stdimpl!(Cell);
stdimpl!(Arc);
stdimpl!(Mutex);
stdimpl!(RwLock);

#[impl_tid]
impl<'a, T> TidAble<'a> for Option<T> {}

#[impl_tid]
impl<'a, T> TidAble<'a> for Vec<T> {}

#[impl_tid]
impl<'a, T, E> TidAble<'a> for Result<T, E> {}

#[impl_tid]
impl<'a> TidAble<'a> for dyn Tid<'a> + 'a {}

// the logic behind this implementations is to connect Any with Tid somehow
// I would say that if T:Any there is no much need to implement Tid<'a> for T.
// because Any functionality already exists and `dyn Any` can be converted to `dyn Tid`.
// unfortunately there is no way to implement Tid<'a> for T:Any,
// which make impl<'a, T: Tid<'a>> Tid<'a> for &'a T {} almost useless
// because it wouldn't work even for &'a i32
// This way we don't require user to newtype wrapping simple references.
// And more complex types are usually not used as a type parameters directly.

unsafe impl<'a, T: Any> TidAble<'a> for &'a T {
    type Static = &'static T;
}

unsafe impl<'a, T: Any> TidAble<'a> for &'a mut T {
    type Static = &'static mut T;
}

// whole impl can be via this macro but it would require to use `paste` crate
// which is already a proc macro, so there is no much reason to do force everything to declarative macro
//
/// Simple version of derive macro to not pull all proc macro dependencies in simple cases
/// when all structs are not generic
///
/// ```rust
/// use better_any::type_id;
/// struct S;
/// type_id!(S);
/// struct F<'a>(&'a str);
/// type_id!(F<'a>);
/// ```
#[macro_export]
macro_rules! type_id {
    ($struct: ident) => {
        unsafe impl<'a> $crate::TidAble<'a> for $struct {
            type Static = $struct;
        }
    };
    ($struct: ident < $lt: lifetime >) => {
        unsafe impl<'a> $crate::TidAble<'a> for $struct<'a> {
            type Static = $struct<'static>;
        }
    }; // ($struct: ident < $($type:ident),* >) => {
       //     unsafe impl<'a,$($type:$crate::TidAble<'a>),*> $crate::TidAble<'a> for $struct<$($type,)*> {
       //         type Static = HygieneUnique<$($type::Static),*>;
       //     }
       //
       //     pub struct HygieneUnique<$($type),*>($(core::marker::PhantomData<$type>),*);
       // };
}

//todo: not sure if worth it.
// If only both T and X could be ?Sized it would DRY From impl as well
// on the other hand it might have problems with type inference
//
// unsafe trait Cast<T: Deref>: Deref {
//     unsafe fn cast(self) -> T;
// }
//
// unsafe impl<T: ?Sized, X> Cast<Rc<X>> for Rc<T> {
//     unsafe fn cast(self) -> Rc<X> {
//         Rc::from_raw(Rc::into_raw(self) as *const _)
//     }
// }
//
// unsafe impl<T: ?Sized, X> Cast<Arc<X>> for Arc<T> {
//     unsafe fn cast(self) -> Arc<X> {
//         Arc::from_raw(Arc::into_raw(self) as *const _)
//     }
// }
//
// unsafe impl<T: ?Sized, X> Cast<Box<X>> for Box<T> {
//     unsafe fn cast(self) -> Box<X> {
//         Box::from_raw(Box::into_raw(self) as *mut _)
//     }
// }
//
// // impl<'a, F: Cast<T, Target = dyn Any>, T: Deref<Target = dyn Tid<'a> + 'a>> From<F> for T {
// //     fn from(f: F) -> Self {
// //         unsafe { f.cast() }
// //     }
// // }
//
// fn downcast<'a, F: Cast<T>, T:Deref>(f: F) -> Result<T, F>
// where
//     F::Target: Tid<'a>,
//     T::Target: Tid<'a>,
// {
//     if <F::Target>::id() == f.self_id() {
//         unsafe { Ok(f.cast()) }
//     } else {
//         Err(f)
//     }
// }