Created
July 29, 2020 19:25
-
-
Save BoxyUwU/064eb1fca27de2ed86f4d4068f0a823a 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
fn main() { | |
// This wont work if the T for Wrap::<T> comes from a generic | |
match (Wrap::<&str>::new()).status() { | |
Status::Owned => println!("Owned"), | |
Status::Shared => println!("Shared"), | |
Status::Exclusive => println!("Exclusive"), | |
} | |
} | |
enum Status { | |
Owned, | |
Shared, | |
Exclusive, | |
} | |
struct Wrap<T: ?Sized>(std::marker::PhantomData<T>); | |
impl<T: ?Sized> Wrap<T> { | |
pub fn new() -> Wrap<T> { | |
Wrap(std::marker::PhantomData) | |
} | |
} | |
trait GetStatus { | |
fn status(self) -> Status; | |
} | |
impl<T: ?Sized> GetStatus for &Wrap<T> { | |
fn status(self) -> Status { | |
Status::Owned | |
} | |
} | |
impl<T: ?Sized> GetStatus for Wrap<&T> { | |
fn status(self) -> Status { | |
Status::Shared | |
} | |
} | |
impl<T: ?Sized> GetStatus for Wrap<&mut T> { | |
fn status(self) -> Status { | |
Status::Exclusive | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment