#traits #macro #comparison #dyn #object #dyncast #fruit-salad

macro fruit-salad_proc-macro-definitions

Procedural macro definitions for fruit-salad

1 unstable release

0.0.1 Sep 29, 2021

#53 in #dyn

30 downloads per month
Used in fruit-salad

MIT/Apache

21KB
487 lines

fruit-salad

Lib.rs Crates.io Docs.rs

Rust 1.54 CI Crates.io - License

GitHub open issues open pull requests good first issues

crev reviews Zulip Chat

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.

Installation

Please use cargo-edit to always add the latest version of this library:

cargo add fruit-salad --features macros

Example

#[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();

}

License

Licensed under either of

at your option.

Contribution

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.

Code of Conduct

Changelog

Versioning

fruit-salad strictly follows Semantic Versioning 2.0.0 with the following exceptions:

  • The minor version will not reset to 0 on major version changes (except for v1).
    Consider it the global feature level.
  • The patch version will not reset to 0 on major or minor version changes (except for v0.1 and v1).
    Consider it the global patch level.

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.

Dependencies

~3.5MB
~74K SLoC