Skip to content

Instantly share code, notes, and snippets.

@BoxyUwU
Created July 29, 2020 19:25
Show Gist options
  • Save BoxyUwU/064eb1fca27de2ed86f4d4068f0a823a to your computer and use it in GitHub Desktop.
Save BoxyUwU/064eb1fca27de2ed86f4d4068f0a823a to your computer and use it in GitHub Desktop.
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