Expand description
§Better Any
Rust RFC for non_static_type_id
feature has been reverted.
Which means in foreseeable future there will be no built-in way in rust to get type id for non-static type
let alone safely use it to downcast to a particular type.
This crate provides tools to do these things safely for types with single lifetime. Although looks like it is technically possible to extend this approach for multiple lifetimes, consistent api and derive macro would be much harder to create and use because of the necessity to properly handle lifetime relations. Feel free to create an issue if you have actual use case where you need this functionality for multiple lifetimes.
Also it has better downcasting that allows you do downcast not just from dyn Tid
(like dyn Any
) but from
any trait object that implements Tid
.
So there is no more need to extend your traits with fn to_any(&self)-> &dyn Any
MSRV: 1.41.0-stable
(without nightly feature)
§Usage
Basically in places where before you have used dyn Any
you can use dyn Tid<'a>
- If your type is generic you should derive
Tid
implementation for it withtid!
macro orTid
derive macro. Then to retrieve back concrete type<dyn Tid>::downcast_*
methods should be used. - If your type is not generic/implements Any you can create
dyn Tid
from it via any of the availableFrom
implementations. Then to retrieve back concrete type<dyn Tid>::downcast_any_*
methods should be used - If your type is not generic and local to your crate you also can derive
Tid
but then you need to be careful to use methods that corresponds to the way you createdyn Tid
for that particular type. Otherwise downcasting will returnNone
.
If all your types can implement Tid
to avoid confusion
recommended way is to use first option even if some types implement Any
.
If there are some types that implement Any
and can’t implement Tid
(i.e. types from other library),
recommended way is to use second option for all types that implement Any
to reduce confusion to minimum.
§Interoperability with Any
Unfortunately you can’t just use Tid
everywhere because currently it is impossible
to implement Tid
for T:Any
since it would conflict with any other possible Tid
implementation.
To overcome this limitation there is a From
impl to go from Box/&/&mut T where T:Any
to Box/&/&mut dyn Tid
.
Nevertheless if you are using dyn Trait
where Trait:Tid
all of this wouldn’t work,
and you are left with Tid
only.
§Safety
It is safe because created trait object preserves lifetime information,
thus allowing us to safely downcast with proper lifetime.
Otherwise internally it is plain old Any
.
Modules§
- nightly
- unstable features that require nightly, use on your own risk
Macros§
- tid
- Main safe implementation interface of related unsafe traits
- type_id
- Just an alias of
tid!
macro if someone considers that name to be more clear and for compatibility with previous versions.
Traits§
- AnyExt
- Methods here are implemented as an associated functions because otherwise
for one they will conflict with methods defined on
dyn Any
in stdlib, for two they will be available on almost every type in the program causing confusing bugs and error messages For example if you have&Box<dyn Any>
and calldowncast_ref
, instead of failing or working on coerced&dyn Any
it would work with type id ofBox<dyn Any>
itself instead of the type behinddyn Any
. - Tid
- This trait indicates that this type can be converted to
trait object with typeid while preserving lifetime information.
Extends
Any
functionality for types with single lifetime - TidAble
- This trait indicates that you can substitute this type as a type parameter to
another type so that resulting type could implement
Tid
. - TidExt
- Extension trait that contains actual downcasting methods.
Functions§
- typeid_
of - Returns type id of
T
Attribute Macros§
- impl_
tid - Attribute macro that makes your implementation of
TidAble
safe Use it when you can’t use derive e.g. for trait object.
Derive Macros§
- Tid
- Derive macro to implement traits from this crate