Created
May 6, 2018 14:41
-
-
Save jlgerber/dd4cae05a3d0515080d8792743f75524 to your computer and use it in GitHub Desktop.
implementing PartialEq for a trait object. Another approach
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Schema { | |
fn eq(&self, other: &Schema) -> bool; | |
} | |
impl<'a> PartialEq<&'a Schema> for &'a Schema { | |
fn eq(&self, other: &&Schema) -> bool { | |
Schema::eq(*self, *other) | |
} | |
} | |
struct Foo; | |
impl Schema for Foo { | |
fn eq(&self, _other: &Schema) -> bool { true } | |
} | |
fn main() { | |
let foo1 = Foo; | |
let foo2 = Foo; | |
if &foo1 as &Schema == &foo2 as &Schema { | |
println!("Hello, world!"); | |
} | |
} |
unfortunately this does not work well with #derive(PartialEq)
:(
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
from
https://users.rust-lang.org/t/testing-equality-with-a-trait-object/5034
author
withoutboats