Skip to content

Instantly share code, notes, and snippets.

@Stebalien
Created August 26, 2016 02:39
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 Stebalien/9300b28e0aedd2096d9a233bd89e4edc to your computer and use it in GitHub Desktop.
Save Stebalien/9300b28e0aedd2096d9a233bd89e4edc to your computer and use it in GitHub Desktop.
#![feature(specialization, never_type)]
use std::rc::Rc;
use std::sync::Arc;
pub trait Identity {
type Type: ?Sized;
fn identity(&self) -> *const ();
}
impl<T: ?Sized> Identity for T {
default type Type = Self;
default fn identity(&self) -> *const () {
self as *const Self as *const ()
}
}
impl<T: ?Sized> Identity for Box<T> {
type Type = <T as Identity>::Type;
fn identity(&self) -> *const () {
(**self).identity()
}
}
impl<'a, T: ?Sized> Identity for &'a T {
type Type = <T as Identity>::Type;
fn identity(&self) -> *const () {
(**self).identity()
}
}
impl<'a, T: ?Sized> Identity for &'a mut T {
type Type = <T as Identity>::Type;
fn identity(&self) -> *const () {
(**self).identity()
}
}
impl<T: ?Sized> Identity for Rc<T> {
type Type = <T as Identity>::Type;
fn identity(&self) -> *const () {
(**self).identity()
}
}
impl<T: ?Sized> Identity for Arc<T> {
type Type = <T as Identity>::Type;
fn identity(&self) -> *const () {
(**self).identity()
}
}
pub fn same<A: Identity, B: Identity<Type=A::Type>>(a: &A, b: &B) -> bool {
a.identity() == b.identity()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment