Crates.io | fruit-salad |
lib.rs | fruit-salad |
version | 0.0.2 |
source | src |
created_at | 2021-09-29 02:48:21.811729 |
updated_at | 2021-11-16 21:03:43.669734 |
description | Compare apples and oranges (and more). Trait object reference casting and trait object comparisons. |
homepage | https://github.com/Tamschi/fruit-salad/tree/v0.0.2 |
repository | https://github.com/Tamschi/fruit-salad |
max_upload_size | |
id | 457865 |
size | 61,922 |
This is a (mostly) trait object 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 from 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:
cargo add fruit-salad --features macros
#[cfg(feature = "macros")]
{
#![allow(clippy::eq_op)] // Identical args are intentional.
use core::fmt::Debug;
use fruit_salad::Dyncast; // With feature `"macros"`.
#[derive(PartialEq, Dyncast, Hash)]
#[dyncast(Self, impl dyn DynHash)]
struct A;
#[derive(Debug, PartialEq, PartialOrd, Dyncast)]
#[dyncast(Self, dyn Debug)]
#[dyncast(impl dyn PartialEq<dyn Dyncast>, impl dyn PartialOrd<dyn Dyncast>)]
struct B;
let a: &dyn Dyncast = &A;
let b: &dyn Dyncast = &B;
assert_ne!(a, a); // Partial equality isn't exposed.
assert_eq!(b, b);
assert_ne!(a, b);
assert_ne!(b, a);
assert_eq!(a.partial_cmp(a), None); // Partial order isn't exposed.
assert_eq!(b.partial_cmp(b), Some(core::cmp::Ordering::Equal));
assert_eq!(a.partial_cmp(b), None);
assert_eq!(b.partial_cmp(a), None);
assert_eq!(format!("{:?}", a), "dyn Dyncast = !dyn Debug");
assert_eq!(format!("{:?}", b), "dyn Dyncast = B");
assert!(a.dyncast::<dyn Debug>().is_none());
assert!(b.dyncast::<dyn Debug>().is_some());
// 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.
Note that dependencies of this crate may have a more lenient MSRV policy!
Please use cargo +nightly update -Z minimal-versions
in your automation if you don't generate Cargo.lock manually (or as necessary) and require support for a compiler older than current stable.