Skip to content

Instantly share code, notes, and snippets.

@dsp
Created May 29, 2019 02:53
Show Gist options
  • Save dsp/bdb694019e42eeaa0dc4c41309e2b20a to your computer and use it in GitHub Desktop.
Save dsp/bdb694019e42eeaa0dc4c41309e2b20a to your computer and use it in GitHub Desktop.
pub enum Owned<T> {
Empty,
Mine(T),
}
impl<T> Owned<T> {
pub fn take(&mut self) -> T {
match std::mem::replace(self, Owned::Empty) {
Owned::Mine(t) => t,
Owned::Empty => panic!("Can't call take on Empty"),
}
}
pub fn as_ref(&self) -> &T {
match self {
Owned::Empty => panic!("Can't call as_ref on Empty"),
Owned::Mine(t) => &t,
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment