-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_file.rs
More file actions
147 lines (127 loc) · 3.33 KB
/
example_file.rs
File metadata and controls
147 lines (127 loc) · 3.33 KB
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
use std::{
alloc::{self, Layout},
marker::PhantomData,
mem::MaybeUninit,
panic, ptr,
};
pub trait HeapConstruct {
type Constructor: HeapConstructConstructor<Target = Self>;
type ConstructorFinishedToken;
}
pub trait HeapConstructConstructor {
type Target;
fn new(ptr: *mut Self::Target) -> Self;
}
#[derive(Debug)]
struct Bosono {
x: i32,
y: i32,
}
impl HeapConstruct for Bosono {
type Constructor = BosonoConstructor<HeapConstructBosonox, HeapConstructBosonoy>;
type ConstructorFinishedToken = BosonoConstructor<(), ()>;
}
struct BosonoConstructor<T0, T1> {
ptr: *mut Bosono,
boo_scary: PhantomData<(T0, T1)>,
}
impl HeapConstructConstructor for BosonoConstructor<HeapConstructBosonox, HeapConstructBosonoy> {
type Target = Bosono;
fn new(ptr: *mut Self::Target) -> Self {
Self {
ptr,
boo_scary: PhantomData::default(),
}
}
}
impl<T0> BosonoConstructor<HeapConstructBosonox, T0> {
fn set_x(self, x: i32) -> BosonoConstructor<(), T0> {
unsafe {
ptr::addr_of_mut!((*self.ptr).x).write(x);
}
BosonoConstructor::<(), T0> {
ptr: self.ptr,
boo_scary: PhantomData::default(),
}
}
}
impl<T0> BosonoConstructor<(), T0> {
fn set_x(self, x: i32) -> Self {
unsafe {
ptr::addr_of_mut!((*self.ptr).x).write(x);
}
self
}
}
impl<T0> BosonoConstructor<T0, HeapConstructBosonoy> {
fn set_y(self, y: i32) -> BosonoConstructor<T0, ()> {
unsafe {
ptr::addr_of_mut!((*self.ptr).y).write(y);
}
BosonoConstructor::<T0, ()> {
ptr: self.ptr,
boo_scary: PhantomData::default(),
}
}
}
impl<T0> BosonoConstructor<T0, ()> {
fn set_y(self, y: i32) -> Self {
unsafe {
ptr::addr_of_mut!((*self.ptr).y).write(y);
}
self
}
}
struct HeapConstructBosonox;
struct HeapConstructBosonoy;
pub unsafe fn init_ptr<
T: HeapConstruct,
F: FnOnce(T::Constructor) -> T::ConstructorFinishedToken,
>(
ptr: *mut T,
func: F,
) {
func(T::Constructor::new(ptr));
}
pub fn init_maybe_uninit<
T: HeapConstruct,
F: FnOnce(T::Constructor) -> T::ConstructorFinishedToken,
>(
uninit: &mut MaybeUninit<T>,
func: F,
) {
unsafe { init_ptr(uninit.as_mut_ptr(), func) }
}
pub fn create_box<
T: HeapConstruct + panic::RefUnwindSafe,
F: FnOnce(T::Constructor) -> T::ConstructorFinishedToken + panic::UnwindSafe,
>(
func: F,
) -> Box<T> {
let layout = Layout::new::<T>();
let ptr = unsafe { alloc::alloc(layout) as *mut T };
let res = panic::catch_unwind(move || {
unsafe { init_ptr(ptr, func) };
});
match res {
Ok(_) => unsafe { Box::from_raw(ptr) },
Err(e) => {
unsafe { alloc::dealloc(ptr as *mut u8, layout) };
panic::resume_unwind(e)
}
}
}
pub fn create_box_uncaught<
T: HeapConstruct + panic::RefUnwindSafe,
F: FnOnce(T::Constructor) -> T::ConstructorFinishedToken + panic::UnwindSafe,
>(
func: F,
) -> Box<T> {
let ptr = unsafe { alloc::alloc(Layout::new::<T>()) as *mut T };
unsafe { init_ptr(ptr, func); }
unsafe { Box::from_raw(ptr) }
}
fn main() {
let b = create_box::<Bosono, _>(|ctor| ctor.set_x(10).set_y(2));
println!("{:?}", b);
}