#partial-eq #traits #dyn #object #macro

dyn_partial_eq

用于特例对象(trait objects)的 PartialEq 宏

3 个版本

0.1.2 2021 年 5 月 16 日
0.1.1 2021 年 5 月 15 日
0.1.0 2021 年 5 月 15 日

#2087 in Rust 模式

Download history 448/week @ 2024-03-13 393/week @ 2024-03-20 376/week @ 2024-03-27 368/week @ 2024-04-03 385/week @ 2024-04-10 345/week @ 2024-04-17 397/week @ 2024-04-24 500/week @ 2024-05-01 397/week @ 2024-05-08 459/week @ 2024-05-15 481/week @ 2024-05-22 493/week @ 2024-05-29 405/week @ 2024-06-05 527/week @ 2024-06-12 458/week @ 2024-06-19 313/week @ 2024-06-26

1,806 个月下载量
用于 2 crates

MIT/Apache

7KB

dyn_partial_eq

特例对象的 PartialEq

此 crate 为 Box<dyn Trait> 提供了生成 PartialEq 的宏,省去了手动实现的过程。受这篇博客启发:https://dev.to/magnusstrale/rust-trait-objects-in-a-vector-non-trivial-4co5

将此 crate 添加到项目中

cargo add dyn_partial_eq
use dyn_partial_eq::*;

// Use this to add a DynPartialEq supertrait and implement PartialEq for your trait.
#[dyn_partial_eq]
trait SomeTrait {}

// Derive DynPartialEq and PartialEq on your types that implement your trait.
#[derive(DynPartialEq, PartialEq)]
struct A(usize);
impl SomeTrait for A {}

#[derive(DynPartialEq, PartialEq)]
struct B((usize, usize));
impl SomeTrait for B {}

就这样

let boxed_a_zero: Box<dyn SomeTrait> = Box::new(A(0));
let boxed_a_one: Box<dyn SomeTrait> = Box::new(A(1));
let boxed_b: Box<dyn SomeTrait> = Box::new(B((1, 2)));

assert_eq!(&boxed_a_zero == &boxed_a_zero, true);
assert_eq!(&boxed_a_zero == &boxed_a_one, false);
assert_eq!(&boxed_a_zero == &boxed_b, false);

assert_eq!(&boxed_a_one == &boxed_a_zero, false);
assert_eq!(&boxed_a_one == &boxed_a_one, true);
assert_eq!(&boxed_a_one == &boxed_b, false);

assert_eq!(&boxed_b == &boxed_a_zero, false);
assert_eq!(&boxed_b == &boxed_a_one, false);
assert_eq!(&boxed_b == &boxed_b, true);

依赖项

~1.5MB
~35K SLoC