Struct tor_rpcbase::obj::cast::CastTable
source · pub struct CastTable {
table: HashMap<TypeId, Box<dyn Any + Send + Sync>>,
}
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
ObjectRefExt
.
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, Box<dyn Any + Send + Sync>>
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 function of type
fn(&dyn Object) -> &dyn Tr
for the same traitTr
. This function must accept a&dyn Object
whose concrete type is actuallyO
, and it SHOULD panic for other input types.
Note that we use Box
here in order to support generic types: you can’t
get a &'static
reference to a function that takes a generic type in
current rust.
Implementations§
source§impl CastTable
impl CastTable
sourcepub fn insert<T: 'static + ?Sized>(&mut self, func: fn(_: &dyn Object) -> &T)
pub fn insert<T: 'static + ?Sized>(&mut self, func: fn(_: &dyn Object) -> &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.)
func
is a downcaster from &dyn Object
to &dyn Tr
.
func
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, func: Box<dyn Any + Send + Sync>)
fn insert_erased(&mut self, type_id: TypeId, func: Box<dyn Any + Send + Sync>)
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.
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.