Skip to content

Instantly share code, notes, and snippets.

@jlgerber
Created May 6, 2018 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlgerber/dd4cae05a3d0515080d8792743f75524 to your computer and use it in GitHub Desktop.
Save jlgerber/dd4cae05a3d0515080d8792743f75524 to your computer and use it in GitHub Desktop.
implementing PartialEq for a trait object. Another approach
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!");
}
}
@jlgerber
Copy link
Author

jlgerber commented May 6, 2018

@jorgecarleitao
Copy link

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