pub struct AspectMap { /* private fields */ }Expand description
The aspect implementations attached to one element, at most one per aspect, each stored under the aspect’s trait.
An aspect is any object-safe trait, and nothing about an aspect is
registered anywhere: a feature, or a single test, can define an aspect of
its own and attach it to its elements, and code that does not use that
trait is unaffected. An implementation is stored under the TypeId of the
trait object type, such as TypeId::of::<dyn BlockRoleAspect>().
ElementSpec::with is the usual way to attach an aspect;
AspectMap::insert is the fallible form. AspectMap::get,
AspectMap::get_arc and AspectMap::has read the map, and
ElementSpec::aspect reads it from the element.
Implementations§
Source§impl AspectMap
impl AspectMap
Sourcepub fn insert<A: ?Sized + Any + Send + Sync>(
&mut self,
imp: Arc<A>,
) -> Result<(), AspectCollision>
pub fn insert<A: ?Sized + Any + Send + Sync>( &mut self, imp: Arc<A>, ) -> Result<(), AspectCollision>
Attaches imp as the implementation of the aspect A.
A must be written as the trait object type:
map.insert::<dyn BlockRoleAspect>(imp). A concrete Arc<HeadingElement>
can still be passed as imp, because Rust converts it to
Arc<dyn BlockRoleAspect> at the call.
The trait object type matters because the map stores the implementation
under A: stored under a concrete implementation type, it would be
under a key that no reader looks up, and nothing would find it. A check
at compile time therefore rejects a call whose A is a sized type, such
as the implementing struct (a reference to a trait object is twice the
width of a plain pointer, a reference to a sized type is not), so a
forgotten dyn is a build error rather than an aspect missing at run
time.
§Errors
Returns an AspectCollision when an implementation of A is already
attached, and leaves the map unchanged.
Sourcepub fn insert_erased(
&mut self,
id: TypeId,
name: &'static str,
imp: Arc<dyn Any + Send + Sync>,
) -> Result<(), AspectCollision>
pub fn insert_erased( &mut self, id: TypeId, name: &'static str, imp: Arc<dyn Any + Send + Sync>, ) -> Result<(), AspectCollision>
Attaches an implementation whose type has already been erased.
The registry build uses this to attach the aspects that features
declare for other features’ elements. An entry read with
AspectMap::entries can be passed to it unchanged. id is the TypeId of the
aspect’s trait object type dyn A; imp must hold the Arc<dyn A>
value itself, boxed in one more Arc, exactly as AspectMap::insert
stores it, or AspectMap::get will not find it; name is the
aspect’s type name, used only in messages.
§Errors
Returns an AspectCollision when an implementation under id is
already attached, and leaves the map unchanged.
Sourcepub fn get<A: ?Sized + Any>(&self) -> Option<&A>
pub fn get<A: ?Sized + Any>(&self) -> Option<&A>
The implementation of the aspect A, or None when none is attached.
Written map.get::<dyn BlockRoleAspect>(): as for
AspectMap::insert, A is the trait object type. A concrete type
compiles here but finds nothing.
Sourcepub fn get_arc<A: ?Sized + Any>(&self) -> Option<Arc<A>>
pub fn get_arc<A: ?Sized + Any>(&self) -> Option<Arc<A>>
A shared handle to the implementation of the aspect A, or None when
none is attached.
The same lookup as AspectMap::get, returning an owned Arc for a
caller that keeps the implementation after the map is no longer
borrowed.
Sourcepub fn has<A: ?Sized + Any>(&self) -> bool
pub fn has<A: ?Sized + Any>(&self) -> bool
Returns true when an implementation of the aspect A is attached.
Sourcepub fn entries(
&self,
) -> impl Iterator<Item = (TypeId, &'static str, Arc<dyn Any + Send + Sync>)> + '_
pub fn entries( &self, ) -> impl Iterator<Item = (TypeId, &'static str, Arc<dyn Any + Send + Sync>)> + '_
The attached implementations, in the order they were attached, each as
the aspect’s TypeId, the aspect’s type name and the implementation
with its type erased.
The registry reads these to build its tables per aspect and its digest,
and each entry can be passed back to AspectMap::insert_erased
unchanged.
Sourcepub fn aspect_ids(&self) -> impl Iterator<Item = TypeId> + '_
pub fn aspect_ids(&self) -> impl Iterator<Item = TypeId> + '_
The TypeIds of the attached aspects, in the order they were attached.
Each is the TypeId of an aspect’s trait object type.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for AspectMap
impl !UnwindSafe for AspectMap
impl Forget for AspectMap
impl Freeze for AspectMap
impl Move for AspectMap
impl Send for AspectMap
impl Sync for AspectMap
impl Unpin for AspectMap
impl UnsafeUnpin for AspectMap
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more