pub trait Allocative {
    // Required method
    fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>);
}
Expand description

This trait allows traversal of object graph.

§Proc macro

Typically implemented with proc macro. Like this:

use allocative::Allocative;

#[derive(Allocative)]
struct Foo {
    x: u32,
    y: String,
}

Proc macro supports two attributes: #[allocative(skip)] and #[allocative(bound = "...")].

§#[allocative(skip)]

#[allocative(skip)] can be used to skip field from traversal (for example, to skip fields which are not Allocative, and can be skipped because they are cheap).

use allocative::Allocative;

/// This does not implement `Allocative`.
struct Unsupported;

#[derive(Allocative)]
struct Bar {
    #[allocative(skip)]
    unsupported: Unsupported,
}

§#[allocative(bound = "...")]

#[allocative(bound = "...")] can be used to overwrite the bounds that are added to the generics of the implementation.

An empty string (#[allocative(bound = "")]) simply erases all bounds. It adds all type variables found in the type to the list of generics but with an empty bound. As an example

use std::marker::PhantomData;

use allocative::Allocative;

struct Unsupported;

#[derive(Allocative)]
#[allocative(bound = "")]
struct Baz<T> {
    _marker: PhantomData<T>,
}

Would generate an instance

impl<T> Allocative for Baz<T> { ... }

Alternatively you can use the string to provide custom bounds. The string in this case is used verbatim as the bounds, which affords great flexibility, but also necessitates that all type variables must be mentioned or will be unbound (compile error). As an example we may derive a size of a HashMap by ignoring the hasher type.

#[allocative(bound = "K: Allocative, V:Allocative, S")]
struct HashMap<K, V, S = RandomState> {
   ...
}

Which generates

impl<K: Allocative, V:Allocative, S> Allocative for HashMap<K, V, S> {
   ...
}

§#[allocative(visit = ...)]

This annotation is used to provide a custom visit method for a given field. This is especially useful if the type of the field does not implement Allocative.

The annotation takes the path to a method with a signature for<'a, 'b>(&T, &'a mut allocative::Visitor<'b>) where T is the type of the field. The function you provide is basically the same as if you implemented Allocative::visit.

As an example

use allocative::Allocative;
use allocative::Key;
use allocative::Visitor;
// use third_party_lib::Unsupported;

#[derive(Allocative)]
struct Bar {
    #[allocative(visit = visit_unsupported)]
    unsupported: Unsupported<usize>,
}

fn visit_unsupported<'a, 'b>(u: &Unsupported<usize>, visitor: &'a mut Visitor<'b>) {
    const ELEM_KEY: Key = Key::new("elements");
    let mut visitor = visitor.enter_self(u);
    for element in u.iter_elems() {
        visitor.visit_field(ELEM_KEY, element);
    }
    visitor.exit()
}

Required Methods§

source

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

Implementations on Foreign Types§

source§

impl Allocative for Infallible

source§

fn visit<'a, 'b: 'a>(&self, _visitor: &'a mut Visitor<'b>)

source§

impl Allocative for bool

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for f32

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for f64

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for i8

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for i16

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for i32

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for i64

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for isize

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for !

source§

fn visit<'a, 'b: 'a>(&self, _visitor: &'a mut Visitor<'b>)

source§

impl Allocative for str

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for u8

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for u16

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for u32

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for u64

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for ()

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for usize

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for String

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for TypeId

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for AtomicBool

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for AtomicI8

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for AtomicI16

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for AtomicI32

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for AtomicI64

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for AtomicIsize

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for AtomicU8

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for AtomicU16

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for AtomicU32

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for AtomicU64

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for AtomicUsize

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for Duration

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for OsStr

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for PathBuf

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for Instant

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for SystemTime

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for NonZeroI8

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for NonZeroI16

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for NonZeroI32

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for NonZeroI64

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for NonZeroIsize

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for NonZeroU8

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for NonZeroU16

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for NonZeroU32

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for NonZeroU64

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl Allocative for NonZeroUsize

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl<A: Allocative> Allocative for (A,)

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl<A: Allocative, B: Allocative> Allocative for (A, B)

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl<A: Allocative, B: Allocative, C: Allocative> Allocative for (A, B, C)

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl<A: Allocative, B: Allocative, C: Allocative, D: Allocative> Allocative for (A, B, C, D)

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl<A: Allocative, B: Allocative, C: Allocative, D: Allocative, E: Allocative> Allocative for (A, B, C, D, E)

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl<K: Allocative> Allocative for BTreeSet<K>

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl<K: Allocative, S> Allocative for HashSet<K, S>

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl<K: Allocative, V: Allocative> Allocative for BTreeMap<K, V>

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl<K: Allocative, V: Allocative, S> Allocative for HashMap<K, V, S>

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl<R> Allocative for fn() -> R

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl<R, A> Allocative for fn(_: A) -> R

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl<R, A, B> Allocative for fn(_: A, _: B) -> R

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl<R, A, B, C> Allocative for fn(_: A, _: B, _: C) -> R

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl<R, A, B, C, D> Allocative for fn(_: A, _: B, _: C, _: D) -> R

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl<R, A, B, C, D, E> Allocative for fn(_: A, _: B, _: C, _: D, _: E) -> R

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl<T: Allocative + ?Sized> Allocative for &'static T

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl<T: Allocative + ?Sized> Allocative for Box<T>

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl<T: Allocative + ?Sized> Allocative for Arc<T>

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl<T: Allocative + ?Sized> Allocative for Weak<T>

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl<T: Allocative + ?Sized> Allocative for ManuallyDrop<T>

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl<T: Allocative> Allocative for Option<T>

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl<T: Allocative> Allocative for [T]

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl<T: Allocative> Allocative for VecDeque<T>

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl<T: Allocative> Allocative for Rc<T>

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl<T: Allocative> Allocative for Weak<T>

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl<T: Allocative> Allocative for Vec<T>

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl<T: Allocative> Allocative for OnceCell<T>

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl<T: Allocative> Allocative for RefCell<T>

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl<T: Allocative> Allocative for Mutex<T>

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl<T: Allocative> Allocative for OnceLock<T>

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl<T: Allocative> Allocative for RwLock<T>

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl<T: Allocative, E: Allocative> Allocative for Result<T, E>

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl<T: Allocative, const N: usize> Allocative for [T; N]

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

source§

impl<T: ?Sized> Allocative for PhantomData<T>

source§

fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>)

Implementors§