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
#![deny(missing_docs, warnings)]
use std::any::Any;
use std::mem;
use std::raw;
pub trait UnsafeAny: Any {}
impl<T: Any> UnsafeAny for T {}
impl UnsafeAny {
pub unsafe fn downcast_ref_unchecked<T: 'static>(&self) -> &T {
mem::transmute(mem::transmute::<&UnsafeAny, raw::TraitObject>(self).data)
}
pub unsafe fn downcast_mut_unchecked<T: 'static>(&mut self) -> &mut T {
mem::transmute(mem::transmute::<&mut UnsafeAny, raw::TraitObject>(self).data)
}
pub unsafe fn downcast_unchecked<T: 'static>(self: Box<UnsafeAny>) -> Box<T> {
mem::transmute(mem::transmute::<Box<UnsafeAny>, raw::TraitObject>(self).data)
}
}
pub trait UnsafeAnyExt {
unsafe fn downcast_ref_unchecked<T: 'static>(&self) -> &T;
unsafe fn downcast_mut_unchecked<T: 'static>(&mut self) -> &mut T;
unsafe fn downcast_unchecked<T: 'static>(self: Box<Self>) -> Box<T>;
}
impl UnsafeAnyExt for Any {
unsafe fn downcast_ref_unchecked<T: 'static>(&self) -> &T {
mem::transmute(mem::transmute::<&Any, raw::TraitObject>(self).data)
}
unsafe fn downcast_mut_unchecked<T: 'static>(&mut self) -> &mut T {
mem::transmute(mem::transmute::<&mut Any, raw::TraitObject>(self).data)
}
unsafe fn downcast_unchecked<T: 'static>(self: Box<Any>) -> Box<T> {
mem::transmute(mem::transmute::<Box<Any>, raw::TraitObject>(self).data)
}
}
#[cfg(test)]
mod test {
use super::{UnsafeAny, UnsafeAnyExt};
use std::any::Any;
#[test] fn test_simple_downcast_ext() {
let a = box 7u as Box<Any>;
unsafe { assert_eq!(*a.downcast_ref_unchecked::<uint>(), 7u); }
let mut a = box 7u as Box<Any>;
unsafe { assert_eq!(*a.downcast_mut_unchecked::<uint>(), 7u); }
let mut a = box 7u as Box<Any>;
unsafe {
*a.downcast_mut_unchecked::<uint>() = 8u;
assert_eq!(*a.downcast_mut_unchecked::<uint>(), 8u);
}
}
#[test] fn test_simple_downcast_inherent() {
let a = box 7u as Box<UnsafeAny>;
unsafe { assert_eq!(*a.downcast_ref_unchecked::<uint>(), 7u); }
let mut a = box 7u as Box<UnsafeAny>;
unsafe { assert_eq!(*a.downcast_mut_unchecked::<uint>(), 7u); }
let mut a = box 7u as Box<UnsafeAny>;
unsafe {
*a.downcast_mut_unchecked::<uint>() = 8u;
assert_eq!(*a.downcast_mut_unchecked::<uint>(), 8u);
}
}
#[test] fn test_box_downcast_no_double_free() {
use std::sync::atomic::{AtomicUint, Ordering};
use std::sync::Arc;
struct Dropper {
x: Arc<AtomicUint>
}
impl Drop for Dropper {
fn drop(&mut self) {
self.x.fetch_add(1, Ordering::SeqCst);
}
}
let x = Arc::new(AtomicUint::new(0));
let a = box Dropper { x: x.clone() } as Box<UnsafeAny>;
let dropper = unsafe { a.downcast_unchecked::<Dropper>() };
drop(dropper);
assert_eq!(x.load(Ordering::SeqCst), 1);
let x = Arc::new(AtomicUint::new(0));
let a = box Dropper { x: x.clone() } as Box<Any>;
let dropper = unsafe { a.downcast_unchecked::<Dropper>() };
drop(dropper);
assert_eq!(x.load(Ordering::SeqCst), 1);
}
}