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
// Copyright 2016 Amanieu d'Antras
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

use IntrusivePointer;

/// Trait for a adapter which allows a type to be inserted into an intrusive
/// collection. The `Link` type contains the collection-specific metadata which
/// allows an object to be inserted into an intrusive collection. This type
/// needs to match the collection type (eg. `LinkedListLink` for inserting
/// in a `LinkedList`).
///
/// `Value` is the actual object type managed by the collection. This type will
/// typically have an instance of `Link` as a struct field.
///
/// `Pointer` is a pointer type which "owns" an object of type `Value`.
/// Operations which insert an element into an intrusive collection will accept
/// such a pointer and operations which remove an element will return this type.
///
/// A single object type may have multiple adapters, which allows it to be part
/// of multiple intrusive collections simultaneously.
///
/// In most cases you do not need to implement this trait manually: the
/// `intrusive_adapter!` macro will generate the necessary implementation for a
/// given type and its link field. However it is possible to implement it
/// manually if the intrusive link is not a direct field of the object type.
///
/// It is also possible to create stateful adapters. This allows links and
/// containers to be separated and avoids the need for objects to be modified to
/// contain a link.
///
/// # Safety
///
/// It must be possible to get back a reference to the container by passing a
/// pointer returned by `get_link` to `get_container`.
pub unsafe trait Adapter {
    /// Collection-specific link type which allows an object to be inserted in
    /// an intrusive collection.
    type Link;

    /// Object type which is inserted in an intrusive collection.
    type Value: ?Sized;

    /// Pointer type which owns an instance of a value.
    type Pointer: IntrusivePointer<Self::Value>;

    /// Gets a reference to an object from a reference to a link in that object.
    unsafe fn get_value(&self, link: *const Self::Link) -> *const Self::Value;

    /// Gets a reference to the link for the given object.
    unsafe fn get_link(&self, value: *const Self::Value) -> *const Self::Link;
}

/// Macro to get the offset of a struct field in bytes from the address of the
/// struct.
///
/// This macro is identical to `offset_of!` but doesn't give a warning about
/// unnecessary unsafe blocks when invoked from unsafe code.
#[macro_export]
macro_rules! offset_of_unsafe {
    ($container:path, $field:ident) => {{
        // Make sure the field actually exists. This line ensures that a
        // compile-time error is generated if $field is accessed through a
        // Deref impl.
        let $container { $field : _, .. };

        // Create an instance of the container and calculate the offset to its
        // field. Although we are creating references to uninitialized data this
        // is fine since we are not dereferencing them.
        let val: $container = $crate::__core::mem::uninitialized();
        let result = &val.$field as *const _ as usize - &val as *const _ as usize;
        $crate::__core::mem::forget(val);
        result as isize
    }};
}

/// Macro to get the offset of a struct field in bytes from the address of the
/// struct.
///
/// This macro will cause a warning if it is invoked in an unsafe block. Use the
/// `offset_of_unsafe` macro instead to avoid this warning.
#[macro_export]
macro_rules! offset_of {
    ($container:path, $field:ident) => {
        unsafe { offset_of_unsafe!($container, $field) }
    };
}

/// Unsafe macro to get a raw pointer to an outer object from a pointer to one
/// of its fields.
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate intrusive_collections;
/// # fn main() {
/// struct S { x: u32, y: u32 };
/// let container = S { x: 1, y: 2 };
/// let field = &container.x;
/// let container2: *const S = unsafe { container_of!(field, S, x) };
/// assert_eq!(&container as *const S, container2);
/// # }
/// ```
///
/// # Safety
///
/// This is unsafe because it assumes that the given expression is a valid
/// pointer to the specified field of some container type.
#[macro_export]
macro_rules! container_of {
    ($ptr:expr, $container:path, $field:ident) => {
        ($ptr as *const _ as *const u8).offset(-offset_of_unsafe!($container, $field)) as *mut $container
    };
}

// Note that we define the macro twice here. The nightly version of the macro
// defines the new() constructor for adapters as a const fn, while the normal
// version defines it as a normal function. Unfortunately, this is necessary
// because #[cfg] in a macro are evaluated in crates using the macro rather
// than the crate which defines the macro.

/// Macro to generate an implementation of `Adapter` for a given set of types.
/// In particular this will automatically generate implementations of the
/// `get_value` and `get_link` methods for a given named field in a struct.
///
/// The basic syntax to create an adapter is:
///
/// ```rust,ignore
/// intrusive_adapter!(Adapter = Pointer: Value { link_field: LinkType });
/// ```
///
/// # Generics
///
/// This macro supports generic arguments:
///
/// ```rust,ignore
/// intrusive_adapter!(
///     Adapter<'lifetime, Type, Type2: ?Sized> =
///         Pointer: Value {
///             link_field: LinkType
///         }
///         where
///             Type: Copy,
///             Type2: 'lifetime
///     );
/// ```
///
/// Note that due to macro parsing limitations, only `?Trait` style bounds are
/// allowed in the generic argument list. In most cases this is only needed for
/// `?Sized`. Other bounds can be specified in the `where` clause at the end
/// the macro.
///
/// # Examples
///
/// ```
/// #[macro_use]
/// extern crate intrusive_collections;
/// use intrusive_collections::{LinkedListLink, RBTreeLink};
///
/// pub struct Test {
///     link: LinkedListLink,
///     link2: RBTreeLink,
/// }
/// intrusive_adapter!(MyAdapter = Box<Test>: Test { link: LinkedListLink });
/// intrusive_adapter!(pub MyAdapter2 = Box<Test>: Test { link2: RBTreeLink });
///
/// pub struct Test2<T: ?Sized>
///     where T: Clone
/// {
///     link: LinkedListLink,
///     val: T,
/// }
/// intrusive_adapter!(MyAdapter3<'a, T: ?Sized> = &'a Test2<T>: Test2<T> { link: LinkedListLink } where T: Clone + 'a);
/// # fn main() {}
/// ```
#[cfg(feature = "nightly")]
#[macro_export]
#[allow_internal_unstable]
macro_rules! intrusive_adapter {
    (@impl
        ($($privacy:tt)*) $name:ident ($($args:tt $(: ?$bound:tt)*),*)
        = $pointer:ty: $value:path { $field:ident: $link:ty } $($where_:tt)*
    ) => {
        #[derive(Clone, Default)]
        $($privacy)* struct $name<$($args),*>($crate::__core::marker::PhantomData<$pointer>) $($where_)*;
        unsafe impl<$($args $(: ?$bound)*),*> Send for $name<$($args),*> $($where_)* {}
        unsafe impl<$($args $(: ?$bound)*),*> Sync for $name<$($args),*> $($where_)* {}
        #[allow(dead_code)]
        impl<$($args $(: ?$bound)*),*> $name<$($args),*> $($where_)* {
            pub const fn new() -> Self {
                $name($crate::__core::marker::PhantomData)
            }
        }
        #[allow(dead_code, unsafe_code)]
        unsafe impl<$($args $(: ?$bound)*),*> $crate::Adapter for $name<$($args),*> $($where_)* {
            type Link = $link;
            type Value = $value;
            type Pointer = $pointer;
            #[inline]
            unsafe fn get_value(&self, link: *const $link) -> *const $value {
                container_of!(link, $value, $field)
            }
            #[inline]
            unsafe fn get_link(&self, value: *const $value) -> *const $link {
                &(*value).$field
            }
        }
    };
    (@find_generic
        ($($privacy:tt)*) $name:tt ($($prev:tt)*) > $($rest:tt)*
    ) => {
        intrusive_adapter!(@impl
            ($($privacy)*) $name ($($prev)*) $($rest)*
        );
    };
    (@find_generic
        ($($privacy:tt)*) $name:tt ($($prev:tt)*) $cur:tt $($rest:tt)*
    ) => {
        intrusive_adapter!(@find_generic
            ($($privacy)*) $name ($($prev)* $cur) $($rest)*
        );
    };
    (@find_if_generic
        ($($privacy:tt)*) $name:tt < $($rest:tt)*
    ) => {
        intrusive_adapter!(@find_generic
            ($($privacy)*) $name () $($rest)*
        );
    };
    (@find_if_generic
        ($($privacy:tt)*) $name:tt $($rest:tt)*
    ) => {
        intrusive_adapter!(@impl
            ($($privacy)*) $name () $($rest)*
        );
    };
    (pub $name:tt $($rest:tt)*) => {
        intrusive_adapter!(@find_if_generic
            (pub) $name $($rest)*
        );
    };
    ($name:tt $($rest:tt)*) => {
        intrusive_adapter!(@find_if_generic
            () $name $($rest)*
        );
    };
}

/// Macro to generate an implementation of `Adapter` for a given set of types.
/// In particular this will automatically generate implementations of the
/// `get_value` and `get_link` methods for a given named field in a struct.
///
/// The basic syntax to create an adapter is:
/// ```rust,ignore
/// intrusive_adapter!(Adapter = Pointer: Value { link_field: LinkType });
/// ```
///
/// # Generics
///
/// This macro supports generic arguments:
/// ```rust,ignore
/// intrusive_adapter!(Adapter<'lifetime, Type, Type2: ?Sized> = Pointer: Value { link_field: LinkType } where Type: Copy, Type2: 'lifetiem);
/// ```
///
/// Note that due to macro parsing limitations, only `?Trait` style bounds are
/// allowed in the generic argument list. In most cases this is only needed for
/// `?Sized`. Other bounds can be specified in the `where` clause at the end
/// the macro.
///
/// # Examples
///
/// ```
/// #[macro_use]
/// extern crate intrusive_collections;
/// use intrusive_collections::{LinkedListLink, RBTreeLink};
///
/// pub struct Test {
///     link: LinkedListLink,
///     link2: RBTreeLink,
/// }
/// intrusive_adapter!(MyAdapter = Box<Test>: Test { link: LinkedListLink });
/// intrusive_adapter!(pub MyAdapter2 = Box<Test>: Test { link2: RBTreeLink });
///
/// pub struct Test2<T: ?Sized>
///     where T: Clone
/// {
///     link: LinkedListLink,
///     val: T,
/// }
/// intrusive_adapter!(MyAdapter3<'a, T: ?Sized> = &'a Test2<T>: Test2<T> { link: LinkedListLink } where T: Clone + 'a);
/// # fn main() {}
/// ```
#[cfg(not(feature = "nightly"))]
#[macro_export]
macro_rules! intrusive_adapter {
    (@impl
        ($($privacy:tt)*) $name:ident ($($args:tt $(: ?$bound:tt)*),*)
        = $pointer:ty: $value:path { $field:ident: $link:ty } $($where_:tt)*
    ) => {
        #[derive(Clone, Default)]
        $($privacy)* struct $name<$($args),*>($crate::__core::marker::PhantomData<$pointer>) $($where_)*;
        unsafe impl<$($args $(: ?$bound)*),*> Send for $name<$($args),*> $($where_)* {}
        unsafe impl<$($args $(: ?$bound)*),*> Sync for $name<$($args),*> $($where_)* {}
        #[allow(dead_code)]
        impl<$($args $(: ?$bound)*),*> $name<$($args),*> $($where_)* {
            pub fn new() -> Self {
                $name($crate::__core::marker::PhantomData)
            }
        }
        #[allow(dead_code, unsafe_code)]
        unsafe impl<$($args $(: ?$bound)*),*> $crate::Adapter for $name<$($args),*> $($where_)* {
            type Link = $link;
            type Value = $value;
            type Pointer = $pointer;
            #[inline]
            unsafe fn get_value(&self, link: *const $link) -> *const $value {
                container_of!(link, $value, $field)
            }
            #[inline]
            unsafe fn get_link(&self, value: *const $value) -> *const $link {
                &(*value).$field
            }
        }
    };
    (@find_generic
        ($($privacy:tt)*) $name:tt ($($prev:tt)*) > $($rest:tt)*
    ) => {
        intrusive_adapter!(@impl
            ($($privacy)*) $name ($($prev)*) $($rest)*
        );
    };
    (@find_generic
        ($($privacy:tt)*) $name:tt ($($prev:tt)*) $cur:tt $($rest:tt)*
    ) => {
        intrusive_adapter!(@find_generic
            ($($privacy)*) $name ($($prev)* $cur) $($rest)*
        );
    };
    (@find_if_generic
        ($($privacy:tt)*) $name:tt < $($rest:tt)*
    ) => {
        intrusive_adapter!(@find_generic
            ($($privacy)*) $name () $($rest)*
        );
    };
    (@find_if_generic
        ($($privacy:tt)*) $name:tt $($rest:tt)*
    ) => {
        intrusive_adapter!(@impl
            ($($privacy)*) $name () $($rest)*
        );
    };
    (pub $name:tt $($rest:tt)*) => {
        intrusive_adapter!(@find_if_generic
            (pub) $name $($rest)*
        );
    };
    ($name:tt $($rest:tt)*) => {
        intrusive_adapter!(@find_if_generic
            () $name $($rest)*
        );
    };
}