-
-
Save stevenblenkinsop/0f4c5d881c1f871ec21e to your computer and use it in GitHub Desktop.
Shared via Rust Playground
This file contains 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
use std::any::Any; | |
trait Schema { | |
fn eq(&self, other: &Schema) -> bool; | |
fn as_any(&self) -> &Any; | |
} | |
impl PartialEq<Schema> for Schema { | |
fn eq(&self, other: &Schema) -> bool { | |
Schema::eq(self, other) | |
} | |
} | |
#[derive(PartialEq)] | |
struct Foo(i32); | |
impl Schema for Foo { | |
fn eq(&self, other: &Schema) -> bool { | |
other.as_any().downcast_ref::<Self>().map_or(false, |x| x == self) | |
} | |
fn as_any(&self) -> &Any { self } | |
} | |
#[derive(PartialEq)] | |
struct Bar; | |
impl Schema for Bar { | |
fn eq(&self, other: &Schema) -> bool { | |
other.as_any().downcast_ref::<Self>().map_or(false, |x| x == self) | |
} | |
fn as_any(&self) -> &Any { self } | |
} | |
fn main() { | |
let foo1: &(Schema+'static) = &Foo(1); | |
if PartialEq::eq(foo1, foo1) { | |
println!("Hello, world!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment