-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneric.rs
48 lines (39 loc) · 1.11 KB
/
generic.rs
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
use cluFullTransmute::transmute_or_panic;
use core::fmt::Display;
/*
For example, let's write some code with a Drop trait that panics when dropped and
holds some data. We then transmute this data to another similar struct and check
that we have effectively overridden the Drop trait and have a different struct
with some data.
We can also remove the Drop trait altogether or do any number of other things.
*/
/// Struct to panic when dropped
#[derive(Debug)]
#[repr(transparent)]
struct PanicWhenDrop<T>(T);
impl<T> Drop for PanicWhenDrop<T> {
fn drop(&mut self) {
panic!("panic, discovered `drop(PanicWhenDrop);`");
}
}
/// Struct to print value when dropped
#[derive(Debug)]
#[repr(transparent)]
struct PrintlnWhenDrop<T: Display>(T)
where
T: Display;
impl<T> Drop for PrintlnWhenDrop<T>
where
T: Display,
{
fn drop(&mut self) {
println!("println: {}", self.0);
}
}
fn main() {
let a: PanicWhenDrop<u16> = PanicWhenDrop(1024);
println!("in a: {:?}", a);
let b: PrintlnWhenDrop<u16> = unsafe { transmute_or_panic(a as PanicWhenDrop<u16>) };
println!("out b: {:?}", b);
drop(b); // <--- drop, PrintlnWhenDrop!
}