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
#![deny(missing_docs, warnings)]

//! Traits for unsafe downcasting from trait objects to & or &mut references of
//! concrete types. These should only be used if you are absolutely certain of the
//! type of the data in said trait object - there be dragons etc.
//!
//! Originally inspired by https://github.com/chris-morgan/anymap
//! and the implementation of `std::any::Any`.

extern crate traitobject;

use std::any::Any;
use std::mem;

/// A trait providing unchecked downcasting to its contents when stored
/// in a trait object.
pub trait UnsafeAny: Any {}
impl<T: Any> UnsafeAny for T {}

impl UnsafeAny {
    /// Returns a reference to the contained value, assuming that it is of type `T`.
    ///
    /// ## Warning
    ///
    /// If you are not _absolutely certain_ of `T` you should _not_ call this!
    pub unsafe fn downcast_ref_unchecked<T: Any>(&self) -> &T {
        mem::transmute(traitobject::data(self))
    }

    /// Returns a mutable reference to the contained value, assuming that it is of type `T`.
    ///
    /// ## Warning
    ///
    /// If you are not _absolutely certain_ of `T` you should _not_ call this!
    pub unsafe fn downcast_mut_unchecked<T: Any>(&mut self) -> &mut T {
        mem::transmute(traitobject::data_mut(self))
    }

    /// Returns a the contained value, assuming that it is of type `T`.
    ///
    /// ## Warning
    ///
    /// If you are not _absolutely certain_ of `T` you should _not_ call this!
    pub unsafe fn downcast_unchecked<T: Any>(self: Box<UnsafeAny>) -> Box<T> {
        let raw: *mut UnsafeAny = mem::transmute(self);
        mem::transmute(traitobject::data_mut(raw))
    }
}

/// An extension trait for unchecked downcasting of trait objects.
pub unsafe trait UnsafeAnyExt {
    /// Returns a reference to the contained value, assuming that it is of type `T`.
    ///
    /// ## Warning
    ///
    /// If you are not _absolutely certain_ of `T` you should _not_ call this!
    unsafe fn downcast_ref_unchecked<T: Any>(&self) -> &T {
        mem::transmute(traitobject::data(self))
    }

    /// Returns a mutable reference to the contained value, assuming that it is of type `T`.
    ///
    /// ## Warning
    ///
    /// If you are not _absolutely certain_ of `T` you should _not_ call this!
    unsafe fn downcast_mut_unchecked<T: Any>(&mut self) -> &mut T {
        mem::transmute(traitobject::data_mut(self))
    }

    /// Returns a the contained value, assuming that it is of type `T`.
    ///
    /// ## Warning
    ///
    /// If you are not _absolutely certain_ of `T` you should _not_ call this!
    unsafe fn downcast_unchecked<T: Any>(self: Box<Self>) -> Box<T> {
        let raw: *mut Self = mem::transmute(self);
        mem::transmute(traitobject::data_mut(raw))
    }
}

unsafe impl UnsafeAnyExt for Any { }
unsafe impl UnsafeAnyExt for UnsafeAny { }
unsafe impl UnsafeAnyExt for Any + Send { }
unsafe impl UnsafeAnyExt for Any + Sync { }
unsafe impl UnsafeAnyExt for Any + Send + Sync { }
unsafe impl UnsafeAnyExt for UnsafeAny + Send { }
unsafe impl UnsafeAnyExt for UnsafeAny + Sync { }
unsafe impl UnsafeAnyExt for UnsafeAny + Send + Sync { }

#[cfg(test)]
mod test {
    use super::{UnsafeAny, UnsafeAnyExt};
    use std::any::Any;

    #[test] fn test_simple_downcast_ext() {
        let a = Box::new(7) as Box<Any>;
        unsafe { assert_eq!(*a.downcast_ref_unchecked::<usize>(), 7); }

        let mut a = Box::new(7) as Box<Any>;
        unsafe { assert_eq!(*a.downcast_mut_unchecked::<usize>(), 7); }

        let mut a = Box::new(7) as Box<Any>;
        unsafe {
            *a.downcast_mut_unchecked::<usize>() = 8;
            assert_eq!(*a.downcast_mut_unchecked::<usize>(), 8);
        }
    }

    #[test] fn test_simple_downcast_inherent() {
        let a = Box::new(7) as Box<UnsafeAny>;
        unsafe { assert_eq!(*a.downcast_ref_unchecked::<usize>(), 7); }

        let mut a = Box::new(7) as Box<UnsafeAny>;
        unsafe { assert_eq!(*a.downcast_mut_unchecked::<usize>(), 7); }

        let mut a = Box::new(7) as Box<UnsafeAny>;
        unsafe {
            *a.downcast_mut_unchecked::<usize>() = 8;
            assert_eq!(*a.downcast_mut_unchecked::<usize>(), 8);
        }
    }

    #[test] fn test_box_downcast_no_double_free() {
        use std::sync::atomic::{AtomicUsize, Ordering};
        use std::sync::Arc;

        struct Dropper {
            x: Arc<AtomicUsize>
        }

        impl Drop for Dropper {
            fn drop(&mut self) {
                self.x.fetch_add(1, Ordering::SeqCst);
            }
        }

        let x = Arc::new(AtomicUsize::new(0));
        let a = Box::new(Dropper { x: x.clone() }) as Box<UnsafeAny>;

        let dropper = unsafe { a.downcast_unchecked::<Dropper>() };
        drop(dropper);

        assert_eq!(x.load(Ordering::SeqCst), 1);

        // Test the UnsafeAnyExt implementation.
        let x = Arc::new(AtomicUsize::new(0));
        let a = Box::new(Dropper { x: x.clone() }) as Box<Any>;

        let dropper = unsafe { a.downcast_unchecked::<Dropper>() };
        drop(dropper);

        assert_eq!(x.load(Ordering::SeqCst), 1);
    }
}