This is a (mostly) trait object reference casting and comparison crate.
There is no registry, instead targets are engraved directly into the Dyncast
trait implementation by a derive macro.
Concrete types can be targeted too, unsafely through reinterpret casts.
(This is subject to #[deny(unsafe_code)]
.)
It also does mutability and pin projection, while being economical regarding text size…
Basically I needed something that's a bit less fancy than the existing solutions, and it escalated a bit for there.
Originally developed as part of rhizome
but now separate,
this crate also works very well in combination with pinus
.
Please use cargo-edit to always add the latest version of this library:
cmd
cargo add fruit-salad --features macros
```rust
use core::fmt::Debug;
use fruit_salad::Dyncast; // With feature "macros"
.
struct A;
struct B;
let a: &dyn Dyncast = Box::leak(Box::new(A)); let b: &dyn Dyncast = Box::leak(Box::new(B));
assertne!(a, a); // Partial equality isn't exposed. asserteq!(b, b); assertne!(a, b); assertne!(b, a);
asserteq!(a.partialcmp(a), None); // Partial ordering isn't exposed. asserteq!(b.partialcmp(b), Some(core::cmp::Ordering::Equal)); asserteq!(a.partialcmp(b), None); asserteq!(b.partialcmp(a), None);
asserteq!( format!("{:?}", a), "dyn Dyncast = !dyn Debug" ); asserteq!( format!("{:?}", b), "dyn Dyncast = B" );
assert!(a.dyncast::
// Also: …_mut
, …_pinned
, …_box
and combinations thereof, as well as …ptr
.
// …box
methods require the "alloc"
feature.
let _a: &A = a.dyncast().unwrap();
let _b: &B = b.dyncast().unwrap();
```
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
See CONTRIBUTING for more information.
fruit-salad
strictly follows Semantic Versioning 2.0.0 with the following exceptions:
This includes the Rust version requirement specified above.
Earlier Rust versions may be compatible, but this can change with minor or patch releases.
Which versions are affected by features and patches can be determined from the respective headings in CHANGELOG.md.