Skip to content

Instantly share code, notes, and snippets.

@aturon
Created March 27, 2015 00:17
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 aturon/43298cf71b4b1de0dae1 to your computer and use it in GitHub Desktop.
Save aturon/43298cf71b4b1de0dae1 to your computer and use it in GitHub Desktop.
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