Created
March 27, 2015 00:17
-
-
Save aturon/43298cf71b4b1de0dae1 to your computer and use it in GitHub Desktop.
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
trait AsRef<T: ?Sized> { | |
fn convert_as_ref(&self) -> &T; | |
} | |
trait Into<T> { | |
fn convert_into(self) -> T; | |
} | |
trait From<T> { | |
fn from(T) -> Self; | |
} | |
trait Convert { | |
fn as_ref<T>(&self) -> &T where Self: AsRef<T>; | |
fn into<T>(self) -> T where Self: Into<T>; | |
} | |
impl<T> Convert for T { | |
fn as_ref<T>(&self) -> &T where Self: AsRef<T> { | |
self.convert_as_ref() | |
} | |
fn into<T>(self) -> T where Self: Into<T> { | |
self.convert_into() | |
} | |
} | |
// Now you can do: | |
foo.into::<Bar>() | |
baz.as_ref::<str>() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment