#traits #object #original #any #back #type #parameters

no-std downcast

Trait for downcasting trait objects back to their original types

20 releases (11 breaking)

0.11.0 Nov 9, 2021
0.10.0 Feb 13, 2019
0.9.2 Feb 15, 2018
0.9.1 Sep 16, 2017
0.2.1 Oct 26, 2015

#287 in Rust patterns

Download history 481744/week @ 2024-07-20 454875/week @ 2024-07-27 453890/week @ 2024-08-03 485099/week @ 2024-08-10 498864/week @ 2024-08-17 516050/week @ 2024-08-24 470479/week @ 2024-08-31 516009/week @ 2024-09-07 451515/week @ 2024-09-14 523745/week @ 2024-09-21 517959/week @ 2024-09-28 555387/week @ 2024-10-05 538834/week @ 2024-10-12 582841/week @ 2024-10-19 550557/week @ 2024-10-26 556268/week @ 2024-11-02

2,321,278 downloads per month
Used in 1,007 crates (19 directly)

MIT license

16KB
301 lines

downcast   Latest Version

A trait (& utilities) for downcasting trait objects back to their original types.

example usage

Add to your Cargo.toml:

[dependencies]
downcast = "0.12"

Add to your crate root:

#[macro_use]
extern crate downcast;
  • simple showcases the most simple usage of this library.
  • with_params showcases how to deal with traits who have type parameters.
  • sync_service showcases how to downcast Arc-pointers.

build features

  • std (default) enables all functionality requiring the standard library (Downcast::downcast()).
  • nightly enables all functionality requiring rust nightly (Any::type_name()).

faq

Q: I'm getting the size for values of type XXX cannot be known at compile time errors, what am i doing wrong?

A: Make sure you use the corresponding Any bound along with the Downcast traits. So, Any for Downcast and AnySync for DowncastSync.

Q: Can i cast trait objects to trait objects?

A: No, that is currently no possible in safe rust - and unsafe solutions are very tricky, as well. If you found a solution, feel free to share it!

Q: What is the difference between this and the downcast-rs crate on crates.io?

A: At the moment, there isn't one, really. There was an unfortunate naming clash. You may consider using the other crate, as it is more actively maintained. This one is considered feature-complete and frozen in functionality. Hopefully, one day, the Rust language will make downcasting easier and we will need neither of these crates anymore!

No runtime deps

Features