#type-id #downcast #any

better_any

Type id and Any for non static types

4 releases

0.2.0 Oct 12, 2022
0.2.0-dev.1 Aug 12, 2021
0.1.1 Jan 8, 2021
0.1.0 Oct 19, 2020

#1220 in Rust patterns

Download history 15020/week @ 2024-07-20 15155/week @ 2024-07-27 12379/week @ 2024-08-03 15284/week @ 2024-08-10 17139/week @ 2024-08-17 16503/week @ 2024-08-24 16957/week @ 2024-08-31 18508/week @ 2024-09-07 18101/week @ 2024-09-14 18791/week @ 2024-09-21 20580/week @ 2024-09-28 21647/week @ 2024-10-05 19932/week @ 2024-10-12 21688/week @ 2024-10-19 19132/week @ 2024-10-26 16604/week @ 2024-11-02

80,983 downloads per month
Used in 151 crates (15 directly)

MIT/Apache

36KB
455 lines

Better_Any

docs Crate

Provides a workaround for absence of non_static_type_id feature in Rust and extended version of Any trait for types with single lifetime.

For more details see documentation.

MSRV: 1.41.0-stable

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this repository by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

lib.rs:

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 with tid! macro or Tid 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 available From 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 create dyn Tid for that particular type. Otherwise downcasting will return None.

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.

Dependencies

~235KB