Created
October 24, 2020 01:41
-
-
Save Lucretiel/fe63473997c0fa23f6d41b3ad00c4dda to your computer and use it in GitHub Desktop.
Utility traits exposing some common std::mem functions as methods
This file contains hidden or 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
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