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; CastTables are obtained and used at runtime, as part of dynamic dispatch, so the type O is erased. We work with TypeIds 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 trait Tr.
  • A function of type fn(&dyn Object) -> &dyn Tr for the same trait Tr. This function must accept a &dyn Object whose concrete type is actually O, 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

source

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.

source

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.

source

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§

source§

impl Default for CastTable

source§

fn default() -> CastTable

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert 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>

Convert 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)

Convert &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)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more