macro_rules! intrusive_adapter { (@impl $(#[$attr:meta])* $vis:vis $name:ident ($($args:tt),*) = $pointer:ty: $value:path { $field:ident: $link:ty } $($where_:tt)* ) => { ... }; (@find_generic $(#[$attr:meta])* $vis:vis $name:ident ($($prev:tt)*) > $($rest:tt)* ) => { ... }; (@find_generic $(#[$attr:meta])* $vis:vis $name:ident ($($prev:tt)*) $cur:tt $($rest:tt)* ) => { ... }; (@find_if_generic $(#[$attr:meta])* $vis:vis $name:ident < $($rest:tt)* ) => { ... }; (@find_if_generic $(#[$attr:meta])* $vis:vis $name:ident $($rest:tt)* ) => { ... }; ($(#[$attr:meta])* $vis:vis $name:ident $($rest:tt)*) => { ... }; }
Expand description
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:
ⓘ
intrusive_adapter!(Adapter = Pointer: Value { link_field: LinkType });
You can create a new instance of an adapter using the new
method or the
NEW
associated constant. The adapter also implements the Default
trait.
§Generics
This macro supports generic arguments:
ⓘ
intrusive_adapter!(
Adapter<'lifetime, Type, Type2> =
Pointer: Value {
link_field: LinkType
}
where
Type: Copy,
Type2: ?Sized + 'lifetime
);
Note that due to macro parsing limitations, T: Trait
bounds are not
supported in the generic argument list. You must list any trait bounds in
a separate where
clause at the end of the macro.
§Examples
use intrusive_collections::{LinkedListLink, RBTreeLink};
use intrusive_collections::intrusive_adapter;
pub struct Test {
link: LinkedListLink,
link2: RBTreeLink,
}
intrusive_adapter!(MyAdapter = Box<Test>: Test { link: LinkedListLink });
intrusive_adapter!(pub MyAdapter2 = Box<Test>: Test { link2: RBTreeLink });
intrusive_adapter!(pub(crate) MyAdapter3 = Box<Test>: Test { link2: RBTreeLink });
pub struct Test2<T>
where T: Clone + ?Sized
{
link: LinkedListLink,
val: T,
}
intrusive_adapter!(MyAdapter4<'a, T> = &'a Test2<T>: Test2<T> { link: LinkedListLink } where T: ?Sized + Clone + 'a);