summaryrefslogtreecommitdiffstats
path: root/vendor/erased-serde/src/any.rs
blob: c910fe90956e202a944dafaecf95c91a51968c10 (plain)
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
use crate::alloc::Box;
use core::any::TypeId;
use core::marker::PhantomData;
use core::mem::{self, MaybeUninit};
use core::ptr;

#[cfg(feature = "unstable-debug")]
use core::any;

pub struct Any {
    value: Value,
    drop: unsafe fn(&mut Value),
    type_id: TypeId,

    /// For panic messages only. Not used for comparison.
    #[cfg(feature = "unstable-debug")]
    type_name: &'static str,
}

union Value {
    ptr: *mut (),
    inline: [MaybeUninit<usize>; 2],
}

fn is_small<T>() -> bool {
    mem::size_of::<T>() <= mem::size_of::<Value>()
        && mem::align_of::<T>() <= mem::align_of::<Value>()
}

impl Any {
    // This is unsafe -- caller must not hold on to the Any beyond the lifetime
    // of T.
    //
    // Example of bad code:
    //
    //    let s = "bad".to_owned();
    //    let a = Any::new(&s);
    //    drop(s);
    //
    // Now `a.view()` and `a.take()` return references to a dead String.
    pub(crate) unsafe fn new<T>(t: T) -> Self {
        let value: Value;
        let drop: unsafe fn(&mut Value);
        let type_id = non_static_type_id::<T>();

        if is_small::<T>() {
            let mut inline = [MaybeUninit::uninit(); 2];
            unsafe { ptr::write(inline.as_mut_ptr().cast::<T>(), t) };
            value = Value { inline };
            unsafe fn inline_drop<T>(value: &mut Value) {
                unsafe { ptr::drop_in_place(value.inline.as_mut_ptr().cast::<T>()) }
            }
            drop = inline_drop::<T>;
        } else {
            let ptr = Box::into_raw(Box::new(t)).cast::<()>();
            value = Value { ptr };
            unsafe fn ptr_drop<T>(value: &mut Value) {
                mem::drop(unsafe { Box::from_raw(value.ptr.cast::<T>()) });
            }
            drop = ptr_drop::<T>;
        };

        Any {
            value,
            drop,
            type_id,
            #[cfg(feature = "unstable-debug")]
            type_name: any::type_name::<T>(),
        }
    }

    // This is unsafe -- caller is responsible that T is the correct type.
    pub(crate) unsafe fn view<T>(&mut self) -> &mut T {
        if self.type_id != non_static_type_id::<T>() {
            self.invalid_cast_to::<T>();
        }

        let ptr = if is_small::<T>() {
            unsafe { self.value.inline.as_mut_ptr().cast::<T>() }
        } else {
            unsafe { self.value.ptr.cast::<T>() }
        };

        unsafe { &mut *ptr }
    }

    // This is unsafe -- caller is responsible that T is the correct type.
    pub(crate) unsafe fn take<T>(mut self) -> T {
        if self.type_id != non_static_type_id::<T>() {
            self.invalid_cast_to::<T>();
        }

        if is_small::<T>() {
            let ptr = unsafe { self.value.inline.as_mut_ptr().cast::<T>() };
            let value = unsafe { ptr::read(ptr) };
            mem::forget(self);
            value
        } else {
            let ptr = unsafe { self.value.ptr.cast::<T>() };
            let box_t = unsafe { Box::from_raw(ptr) };
            mem::forget(self);
            *box_t
        }
    }

    #[cfg(not(feature = "unstable-debug"))]
    fn invalid_cast_to<T>(&self) -> ! {
        panic!("invalid cast; enable `unstable-debug` feature to debug");
    }

    #[cfg(feature = "unstable-debug")]
    fn invalid_cast_to<T>(&self) -> ! {
        let from = self.type_name;
        let to = any::type_name::<T>();
        panic!("invalid cast: {} to {}", from, to);
    }
}

impl Drop for Any {
    fn drop(&mut self) {
        unsafe { (self.drop)(&mut self.value) }
    }
}

trait NonStaticAny {
    fn get_type_id(&self) -> TypeId
    where
        Self: 'static;
}

impl<T: ?Sized> NonStaticAny for PhantomData<T> {
    fn get_type_id(&self) -> TypeId
    where
        Self: 'static,
    {
        TypeId::of::<T>()
    }
}

fn non_static_type_id<T>() -> TypeId {
    let non_static_thing = &PhantomData::<T>;
    let thing = unsafe {
        mem::transmute::<&dyn NonStaticAny, &(dyn NonStaticAny + 'static)>(non_static_thing)
    };
    NonStaticAny::get_type_id(thing)
}

#[test]
fn test_non_static_type_id() {
    assert_eq!(non_static_type_id::<usize>(), non_static_type_id::<usize>());
    assert_eq!(
        non_static_type_id::<&str>(),
        non_static_type_id::<&'static str>()
    );

    assert_ne!(non_static_type_id::<u32>(), non_static_type_id::<[u8; 4]>());
    assert_ne!(
        non_static_type_id::<u32>(),
        non_static_type_id::<[u32; 2]>()
    );

    assert_ne!(non_static_type_id::<usize>(), non_static_type_id::<isize>());
    assert_ne!(
        non_static_type_id::<usize>(),
        non_static_type_id::<&usize>()
    );
    assert_ne!(
        non_static_type_id::<&usize>(),
        non_static_type_id::<&&usize>()
    );
    assert_ne!(
        non_static_type_id::<&usize>(),
        non_static_type_id::<&mut usize>()
    );

    struct A;
    struct B;
    assert_ne!(non_static_type_id::<A>(), non_static_type_id::<B>());
}