pub struct CastTable {
table: HashMap<TypeId, Caster>,
}
Expand description
A collection of functions to downcast &dyn Object
references for some
particular concrete object type O
into various &dyn Trait
references.
You shouldn’t construct this on your own: instead use
derive_deftly(Object)
.
You shouldn’t use this directly; instead use
ObjectArcExt
.
Note that the concrete object type O
is not represented in the type of CastTable
;
CastTable
s are obtained and used at runtime, as part of dynamic dispatch,
so the type O
is erased. We work with TypeId
s and various &dyn ...
.
Fields§
§table: HashMap<TypeId, Caster>
A mapping from target TypeId for some trait to a function that can convert this table’s type into a trait pointer to that trait.
Every entry in this table must contain:
- A key that is
typeid::of::<&'static dyn Tr>()
for some traitTr
. - A
Caster
whose functions are suitable for casting objects from this table’s type todyn Tr
.
Implementations§
Source§impl CastTable
impl CastTable
Sourcepub fn insert<T: 'static + ?Sized>(
&mut self,
cast_to_ref: fn(_: &dyn Object) -> &T,
cast_to_arc: fn(_: Arc<dyn Object>) -> Arc<T>,
)
pub fn insert<T: 'static + ?Sized>( &mut self, cast_to_ref: fn(_: &dyn Object) -> &T, cast_to_arc: fn(_: Arc<dyn Object>) -> Arc<T>, )
Add a new entry to this CastTable
for downcasting to TypeId.
You should not call this yourself; instead use
derive_deftly(Object)
§Requirements
T
must be dyn Tr
for some trait Tr
.
(Not checked by the compiler.)
cast_to_ref
is a downcaster from &dyn Object
to &dyn Tr
.
cast_to_arc
is a downcaster from Arc<dyn Object>
to Arc<dyn Tr>
.
These functions SHOULD
panic if the concrete type of its argument is not the concrete type O
associated with this CastTable
.
O
must be 'static
.
(Checked by the compiler.)
§Panics
Panics if called twice on the same CastTable
with the same Tr
.
Sourcefn insert_erased(&mut self, type_id: TypeId, caster: Caster)
fn insert_erased(&mut self, type_id: TypeId, caster: Caster)
Implementation for adding an entry to the CastTable
Broken out for clarity and to reduce monomorphisation.
§Requirements
Like insert
, but less compile-time checking.
type_id
is the identity of &'static dyn Tr
,
and func
has been boxed and type-erased.
Sourcepub fn cast_object_to<'a, T: 'static + ?Sized>(
&self,
obj: &'a dyn Object,
) -> Option<&'a T>
pub fn cast_object_to<'a, T: 'static + ?Sized>( &self, obj: &'a dyn Object, ) -> Option<&'a T>
Try to downcast a reference to an object whose concrete type is
O
(the type associated with this CastTable
)
to some target type T
.
T
should be dyn Tr
.
If T
is not one of the dyn Tr
for which insert
was called,
returns None
.
§Panics
Panics if the concrete type of obj
does not match O
.
May panic if any of the Requirements for CastTable::insert
were
violated.
Sourcepub fn cast_object_to_arc<T: 'static + ?Sized>(
&self,
obj: Arc<dyn Object>,
) -> Result<Arc<T>, Arc<dyn Object>>
pub fn cast_object_to_arc<T: 'static + ?Sized>( &self, obj: Arc<dyn Object>, ) -> Result<Arc<T>, Arc<dyn Object>>
As cast_object_to
, but returns an Arc<dyn Tr>
.
If T
is not one of the dyn Tr
types for which insert_arc
was called,
return Err(obj)
.
§Panics
Panics if the concrete type of obj
does not match O
.
May panic if any of the Requirements for CastTable::insert
were
violated.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for CastTable
impl !RefUnwindSafe for CastTable
impl Send for CastTable
impl Sync for CastTable
impl Unpin for CastTable
impl !UnwindSafe for CastTable
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<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.