Skip to content

Instantly share code, notes, and snippets.

@Lucretiel
Created October 24, 2020 01:41
Show Gist options
  • Save Lucretiel/fe63473997c0fa23f6d41b3ad00c4dda to your computer and use it in GitHub Desktop.
Save Lucretiel/fe63473997c0fa23f6d41b3ad00c4dda to your computer and use it in GitHub Desktop.
Utility traits exposing some common std::mem functions as methods
pub trait Swap {
fn swap(&mut self, other: &mut Self);
fn replace(&mut self, other: Self) -> Self;
}
impl<T: Sized> Swap for T {
fn swap(&mut self, other: &mut Self) {
std::mem::swap(self, other)
}
fn replace(&mut self, other: Self) -> Self {
std::mem::replace(self, other)
}
}
pub trait Take {
fn take(&mut self) -> Self;
}
impl<T: Sized + Default> Take for T {
fn take(&mut self) -> Self {
std::mem::take(self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment