Trait TraitcastableAny

Source
pub unsafe trait TraitcastableAny: Any {
    // Required method
    fn traitcast_targets(&self) -> &[TraitcastTarget];

    // Provided methods
    fn find_traitcast_target(&self, target: TypeId) -> Option<&TraitcastTarget> { ... }
    fn type_id(&self) -> TypeId { ... }
}
Expand description

A trait marking a type as being potentially able to traitcast from dyn TraitcastableAny to another dyn Trait. Use this trait instead of the Any trait throughout your program.

This should generally not be manually implemented, but generated by the make_trait_castable attribute macro.

§Safety

The function traitcast_targets must only produce valid TraitcastTarget (That use the metadata associated the the correct source struct). The function find_traitcast_target must not return Some unless contained value has the correct target TypeId.

Required Methods§

Source

fn traitcast_targets(&self) -> &[TraitcastTarget]

This function returns a list of all the TraitcastTarget’s to which a trait object can be cast, this is then used by the implementations of TraitcastableAnyInfra to accomplish the traitcast. The function is used to generate debug output for TraitcastableAny. The default implementation of find_traitcast_target uses this function by default.

This should generally not be manually implemented, but generated by the make_trait_castable attribute macro.

Provided Methods§

Source

fn find_traitcast_target(&self, target: TypeId) -> Option<&TraitcastTarget>

This function can be implemented to support custom TypeId lookup algorithms. This may be desired when there are lots of TraitcastTargets (30 or more).

Possible strategies:

  • Unsorted Vec<TraitcastTarget> lookup. Hot traits first. - Used by the default implementation.
  • HashMap
  • Temporarily removed without replacement: Vec<TraitcastTarget> sorted by the TypeId and performing a binary search on it - Used if feature const_sort is used.
Source

fn type_id(&self) -> TypeId

Returns the TypeId of the concrete type.

Trait Implementations§

Source§

impl Debug for dyn TraitcastableAny

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<Target: ?Sized + 'static> TraitcastableAnyInfra<Target> for dyn TraitcastableAny

Source§

default fn is(&self) -> bool

Returns true if Target is the exact same type as Self.
Source§

default fn can_be(&self) -> bool

Returns true if Self can be converted to a Target.
Source§

default fn downcast_ref(&self) -> Option<&Target>

Returns some reference to the inner value if it is downcastable to Target, or None if it isn’t. Read more
Source§

default unsafe fn downcast_ref_unchecked(&self) -> &Target

Available on crate feature downcast_unchecked only.
Unchecked variant of downcast_ref Read more
Source§

default fn downcast_mut(&mut self) -> Option<&mut Target>

Returns some mutable reference to the inner value if it is downcastable to Target, or None if it isn’t. Read more
Source§

default unsafe fn downcast_mut_unchecked(&mut self) -> &mut Target

Available on crate feature downcast_unchecked only.
Unchecked variant of downcast_ref Read more
Source§

impl<Target: ?Sized + 'static + Send> TraitcastableAnyInfra<Target> for dyn TraitcastableAny + Send

Source§

default fn is(&self) -> bool

Returns true if Target is the exact same type as Self.
Source§

default fn can_be(&self) -> bool

Returns true if Self can be converted to a Target.
Source§

default fn downcast_ref(&self) -> Option<&Target>

Returns some reference to the inner value if it is downcastable to Target, or None if it isn’t. Read more
Source§

default unsafe fn downcast_ref_unchecked(&self) -> &Target

Available on crate feature downcast_unchecked only.
Unchecked variant of downcast_ref Read more
Source§

default fn downcast_mut(&mut self) -> Option<&mut Target>

Returns some mutable reference to the inner value if it is downcastable to Target, or None if it isn’t. Read more
Source§

default unsafe fn downcast_mut_unchecked(&mut self) -> &mut Target

Available on crate feature downcast_unchecked only.
Unchecked variant of downcast_ref Read more
Source§

impl<Target: ?Sized + 'static + Send + Sync> TraitcastableAnyInfra<Target> for dyn TraitcastableAny + Send + Sync

Source§

default fn is(&self) -> bool

Returns true if Target is the exact same type as Self.
Source§

default fn can_be(&self) -> bool

Returns true if Self can be converted to a Target.
Source§

default fn downcast_ref(&self) -> Option<&Target>

Returns some reference to the inner value if it is downcastable to Target, or None if it isn’t. Read more
Source§

default unsafe fn downcast_ref_unchecked(&self) -> &Target

Available on crate feature downcast_unchecked only.
Unchecked variant of downcast_ref Read more
Source§

default fn downcast_mut(&mut self) -> Option<&mut Target>

Returns some mutable reference to the inner value if it is downcastable to Target, or None if it isn’t. Read more
Source§

default unsafe fn downcast_mut_unchecked(&mut self) -> &mut Target

Available on crate feature downcast_unchecked only.
Unchecked variant of downcast_ref Read more
Source§

impl<Target: Sized + 'static + Send + Sync> TraitcastableAnyInfra<Target> for dyn TraitcastableAny + Send + Sync

Source§

fn is(&self) -> bool

Returns true if Target is the exact same type as Self.
Source§

fn can_be(&self) -> bool

Returns true if Self can be converted to a Target.
Source§

fn downcast_ref(&self) -> Option<&Target>

Returns some reference to the inner value if it is downcastable to Target, or None if it isn’t. Read more
Source§

unsafe fn downcast_ref_unchecked(&self) -> &Target

Available on crate feature downcast_unchecked only.
Unchecked variant of downcast_ref Read more
Source§

fn downcast_mut(&mut self) -> Option<&mut Target>

Returns some mutable reference to the inner value if it is downcastable to Target, or None if it isn’t. Read more
Source§

unsafe fn downcast_mut_unchecked(&mut self) -> &mut Target

Available on crate feature downcast_unchecked only.
Unchecked variant of downcast_ref Read more
Source§

impl<Target: Sized + 'static + Send> TraitcastableAnyInfra<Target> for dyn TraitcastableAny + Send

Source§

fn is(&self) -> bool

Returns true if Target is the exact same type as Self.
Source§

fn can_be(&self) -> bool

Returns true if Self can be converted to a Target.
Source§

fn downcast_ref(&self) -> Option<&Target>

Returns some reference to the inner value if it is downcastable to Target, or None if it isn’t. Read more
Source§

unsafe fn downcast_ref_unchecked(&self) -> &Target

Available on crate feature downcast_unchecked only.
Unchecked variant of downcast_ref Read more
Source§

fn downcast_mut(&mut self) -> Option<&mut Target>

Returns some mutable reference to the inner value if it is downcastable to Target, or None if it isn’t. Read more
Source§

unsafe fn downcast_mut_unchecked(&mut self) -> &mut Target

Available on crate feature downcast_unchecked only.
Unchecked variant of downcast_ref Read more
Source§

impl<Target: Sized + 'static> TraitcastableAnyInfra<Target> for dyn TraitcastableAny

Source§

fn is(&self) -> bool

Returns true if Target is the exact same type as Self.
Source§

fn can_be(&self) -> bool

Returns true if Self can be converted to a Target.
Source§

fn downcast_ref(&self) -> Option<&Target>

Returns some reference to the inner value if it is downcastable to Target, or None if it isn’t. Read more
Source§

unsafe fn downcast_ref_unchecked(&self) -> &Target

Available on crate feature downcast_unchecked only.
Unchecked variant of downcast_ref Read more
Source§

fn downcast_mut(&mut self) -> Option<&mut Target>

Returns some mutable reference to the inner value if it is downcastable to Target, or None if it isn’t. Read more
Source§

unsafe fn downcast_mut_unchecked(&mut self) -> &mut Target

Available on crate feature downcast_unchecked only.
Unchecked variant of downcast_ref Read more

Implementors§

Source§

impl<T: 'static> TraitcastableAny for T