pub trait Referrer: 'static {
    fn visit_type(arg: &mut VisitTypeArg<'_>);
    fn visit_mut<V: VisitMutArg>(&mut self, arg: &mut V);
}
Expand description

A type that may own entity references (no matter strong or weak).

The parameters passed in this trait are abstracted by opaque types/traits. Implementors should only forward the arg reference to implementors in its member fields, where the actual logic is eventually implemented by owned Entity and Weak fields.

Non-implementors

UnclonableRef

This trait is deliberately not implemented for UnclonableRef, because this trait should only be used in global states and components, while UnclonableRef should only be used in temporary variables in systems.

Rc/Arc

Each entity reference must be visited only exactly once. Therefore, it is not possible to implement Referrer for ref-counted types, because multiple references would be visited multiple times. If sharing an entity reference is ever necessary, consider refactoring to store the underlying type in a separate unique global state.

Example implementation

use dynec::entity::referrer;

struct MyCollection<T: referrer::Referrer> {
    data: [T; 10],
};

impl<T: referrer::Referrer> referrer::Referrer for MyCollection<T> {
    fn visit_type(arg: &mut referrer::VisitTypeArg) {
        if arg.mark::<Self>().is_continue() {
            <T as referrer::Referrer>::visit_type(arg);
        }
    }

    fn visit_mut<V: referrer::VisitMutArg>(&mut self, arg: &mut V) {
        for value in &mut self.data {
            <T as referrer::Referrer>::visit_mut(value, arg);
        }
    }
}

Required Methods

Visit all types that may appear under this referrer.

It is OK to visit the same type twice. arg contains an internal hash set that avoids recursion.

Execute the given function on every strong and weak entity reference exactly once.

Implementors are recommended to mark the implementation as #[inline] since this function is a no-op for most implementors.

Implementations on Foreign Types

Implementors