tor_rpcbase::obj::cast

Struct CastTable

Source
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; 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, 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 trait Tr.
  • A Caster whose functions are suitable for casting objects from this table’s type to dyn Tr.

Implementations§

Source§

impl CastTable

Source

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.

Source

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.

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.

Source

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§

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 + Send + Sync>

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

Source§

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

Source§

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