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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
use std::ops;
use std::convert;
use std::mem;
use std::marker;
#[macro_export]
macro_rules! containerof_field_offset {
($container:ty : $field:ident) => (unsafe {
&(*(0usize as *const $container)).$field as *const _ as usize
})
}
#[macro_export]
macro_rules! containerof_intrusive {
($nt:ident = $container:ty : $field:ident :: $fieldtype:ty) => (
containerof_intrusive!(_decl $nt);
containerof_intrusive!(_impl $nt = $container : $field :: $fieldtype);
);
(pub $nt:ident = $container:ty : $field:ident :: $fieldtype:ty) => (
containerof_intrusive!(_decl pub $nt);
containerof_intrusive!(_impl $nt = $container : $field :: $fieldtype);
);
(_decl $nt:ident) => (
struct $nt($crate::IntrusiveAlias);
);
(_decl pub $nt:ident) => (
pub struct $nt($crate::IntrusiveAlias);
);
(_impl $nt:ident = $container:ty : $field:ident :: $fieldtype:ty) => (
impl $crate::IntrusiveBase for $nt {
type Container = $container;
type Field = $fieldtype;
#[inline]
fn offset() -> usize {
containerof_field_offset!($container : $field)
}
#[inline]
unsafe fn new(ia: $crate::IntrusiveAlias) -> $nt {
$nt(ia)
}
#[inline]
unsafe fn as_alias<'a>(&'a self) -> &'a IntrusiveAlias {
::std::mem::transmute(self as *const _)
}
}
);
}
#[derive(PartialEq,Eq,Copy,Clone,Debug)]
pub struct IntrusiveAlias(pub usize);
impl IntrusiveAlias {
pub fn new(addr: usize) -> IntrusiveAlias {
IntrusiveAlias(addr)
}
pub unsafe fn new_of<T>(addr: &T) -> IntrusiveAlias {
IntrusiveAlias::new(addr as *const _ as usize)
}
pub fn get_address(&self) -> usize {
self.0
}
}
pub struct OwnBox<T> {
pointer: IntrusiveAlias,
marker: marker::PhantomData<T>,
}
impl<T> OwnBox<T> {
pub fn get_address(&self) -> usize {
self.pointer.0
}
pub unsafe fn from_alias(pointer: IntrusiveAlias) -> OwnBox<T> {
OwnBox { pointer: pointer, marker: marker::PhantomData }
}
pub unsafe fn into_alias(self) -> IntrusiveAlias {
let rval = self.pointer;
mem::forget(self);
rval
}
pub unsafe fn as_alias<'a>(&'a self) -> &'a IntrusiveAlias {
&self.pointer
}
pub fn from_box(b: Box<T>) -> OwnBox<T> {
OwnBox { pointer: IntrusiveAlias::new(unsafe { mem::transmute(b) }), marker: marker::PhantomData }
}
pub unsafe fn into_box(self) -> Box<T> {
mem::transmute(self.into_alias().get_address())
}
}
impl<T> convert::From<Box<T>> for OwnBox<T> {
fn from(t: Box<T>) -> OwnBox<T> {
OwnBox::from_box(t)
}
}
impl<T> ops::Deref for OwnBox<T> {
type Target = T;
fn deref<'a>(&'a self) -> &'a T {
unsafe { mem::transmute(self.get_address()) }
}
}
impl<T> ops::DerefMut for OwnBox<T> {
fn deref_mut<'a>(&'a mut self) -> &'a mut T {
unsafe { mem::transmute(self.get_address()) }
}
}
impl<T> ops::Drop for OwnBox<T> {
fn drop(&mut self) {
}
}
#[derive(Debug)]
pub struct BorrowBox<'a, T: 'a> {
pointer: IntrusiveAlias,
marker: marker::PhantomData<&'a T>,
}
impl<'a, T> BorrowBox<'a, T> where T: Intrusive {
pub fn new(source: &'a T) -> BorrowBox<'a, T> {
unsafe { BorrowBox::new_from(IntrusiveAlias::new_of(source), source) }
}
pub unsafe fn new_from<U>(pointer: IntrusiveAlias, _lifetime: &'a U) -> BorrowBox<'a, T> {
BorrowBox {
pointer: pointer,
marker: marker::PhantomData,
}
}
}
impl<'a, T> ops::Deref for BorrowBox<'a, T> where T: Intrusive {
type Target = T;
fn deref<'b>(&'b self) -> &'b T {
unsafe { Intrusive::of_alias(&self.pointer) }
}
}
#[derive(Debug)]
pub struct BorrowBoxMut<'a, T: 'a> where T: Intrusive {
pointer: IntrusiveAlias,
marker: marker::PhantomData<&'a mut T>,
}
impl<'a, T> BorrowBoxMut<'a, T> where T: Intrusive {
pub fn new(source: &'a mut T) -> BorrowBoxMut<'a, T> {
unsafe { BorrowBoxMut::new_from(IntrusiveAlias::new_of(source), source) }
}
pub unsafe fn new_from<U>(pointer: IntrusiveAlias, _lifetime: &'a mut U) -> BorrowBoxMut<'a, T> {
BorrowBoxMut {
pointer: pointer,
marker: marker::PhantomData,
}
}
}
impl<'a, T> ops::Deref for BorrowBoxMut<'a, T> where T: Intrusive {
type Target = T;
fn deref<'b>(&'b self) -> &'b T {
unsafe { Intrusive::of_alias(&self.pointer) }
}
}
impl<'a, T> ops::DerefMut for BorrowBoxMut<'a, T> where T: Intrusive {
fn deref_mut<'b>(&'b mut self) -> &'b mut T {
unsafe { Intrusive::of_alias_mut(&mut self.pointer) }
}
}
pub trait IntrusiveBase: Sized {
type Container;
type Field;
fn offset() -> usize;
unsafe fn new(IntrusiveAlias) -> Self;
unsafe fn as_alias<'a>(&'a self) -> &'a IntrusiveAlias;
}
pub trait Intrusive: IntrusiveBase {
unsafe fn from_alias(IntrusiveAlias) -> Self;
unsafe fn into_alias(self) -> IntrusiveAlias;
unsafe fn as_alias_mut<'a>(&'a mut self) -> &'a mut IntrusiveAlias;
unsafe fn of_alias<'a>(&'a IntrusiveAlias) -> &'a Self;
unsafe fn of_alias_mut<'a>(ia: &'a mut IntrusiveAlias) -> &'a mut Self;
fn from_container(OwnBox<Self::Container>) -> Self;
fn into_container(self) -> OwnBox<Self::Container>;
fn of_container<'a>(&'a Self::Container) -> BorrowBox<'a, Self>;
fn of_container_mut<'a>(&'a mut Self::Container) -> BorrowBoxMut<'a, Self>;
fn as_container<'a>(&'a self) -> &'a Self::Container;
fn as_container_mut<'a>(&'a mut self) -> &'a mut Self::Container;
unsafe fn from_field(OwnBox<Self::Field>) -> Self;
unsafe fn into_field(self) -> OwnBox<Self::Field>;
unsafe fn of_field<'a>(&'a Self::Field) -> BorrowBox<'a, Self>;
unsafe fn of_field_mut<'a>(&'a mut Self::Field) -> BorrowBoxMut<'a, Self>;
fn as_field<'a>(&'a self) -> &'a Self::Field;
fn as_field_mut<'a>(&'a mut self) -> &'a mut Self::Field;
}
impl<T: IntrusiveBase> Intrusive for T {
#[inline]
unsafe fn from_alias(ia: IntrusiveAlias) -> T {
<T as IntrusiveBase>::new(ia)
}
#[inline]
unsafe fn into_alias(self) -> IntrusiveAlias {
*self.as_alias()
}
#[allow(mutable_transmutes)]
#[inline]
unsafe fn as_alias_mut<'a>(&'a mut self) -> &'a mut IntrusiveAlias {
mem::transmute(self.as_alias())
}
#[inline]
unsafe fn of_alias<'a>(ia: &'a IntrusiveAlias) -> &'a T {
mem::transmute(ia as *const _)
}
#[inline]
unsafe fn of_alias_mut<'a>(ia: &'a mut IntrusiveAlias) -> &'a mut T {
mem::transmute(ia as *const _)
}
#[inline]
fn from_container(c: OwnBox<T::Container>) -> Self {
unsafe {
let addr = c.as_alias().get_address() + <T as IntrusiveBase>::offset();
mem::forget(c);
<T as Intrusive>::from_alias(IntrusiveAlias(addr))
}
}
#[inline]
fn into_container(self) -> OwnBox<T::Container> {
let fieldptr = unsafe { self.as_alias().get_address() };
let containerptr = fieldptr - <T as IntrusiveBase>::offset();
unsafe { OwnBox::from_alias(IntrusiveAlias::new(containerptr)) }
}
#[inline]
fn of_container<'a>(container: &'a T::Container) -> BorrowBox<'a, T> {
let addr = container as *const _ as usize;
let fieldptr = addr + <T as IntrusiveBase>::offset();
unsafe { BorrowBox::new_from(IntrusiveAlias::new(fieldptr), container) }
}
#[inline]
fn of_container_mut<'a>(container: &'a mut T::Container) -> BorrowBoxMut<'a, T> {
let addr = container as *mut _ as usize;
let fieldptr = addr + <T as IntrusiveBase>::offset();
unsafe { BorrowBoxMut::new_from(IntrusiveAlias::new(fieldptr), container) }
}
#[inline]
fn as_container<'a>(&'a self) -> &'a T::Container {
unsafe {
let fieldptr = self.as_alias().get_address();
let containerptr = fieldptr - <T as IntrusiveBase>::offset();
&*(containerptr as *const T::Container)
}
}
#[allow(mutable_transmutes)]
#[inline]
fn as_container_mut<'a>(&'a mut self) -> &'a mut T::Container {
unsafe { mem::transmute(self.as_container()) }
}
#[inline]
unsafe fn from_field(c: OwnBox<T::Field>) -> T {
let addr = c.as_alias().get_address();
mem::forget(c);
<T as Intrusive>::from_alias(IntrusiveAlias::new(addr))
}
#[inline]
unsafe fn into_field(self) -> OwnBox<T::Field> {
OwnBox::from_alias(IntrusiveAlias(self.as_alias().get_address()))
}
#[inline]
unsafe fn of_field<'a>(field: &'a T::Field) -> BorrowBox<'a, T> {
BorrowBox::new_from(IntrusiveAlias::new_of(field), field)
}
#[inline]
unsafe fn of_field_mut<'a>(field: &'a mut T::Field) -> BorrowBoxMut<'a, T> {
BorrowBoxMut::new_from(IntrusiveAlias::new_of(field), field)
}
#[inline]
fn as_field<'a>(&'a self) -> &'a T::Field {
unsafe { &*(self.as_alias().get_address() as *const _) }
}
#[inline]
fn as_field_mut<'a>(&'a mut self) -> &'a mut T::Field {
unsafe { &mut *(self.as_alias().get_address() as *mut _) }
}
}