Skip to content

Instantly share code, notes, and snippets.

@Dessix
Created June 26, 2019 02:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dessix/93e522405f09e3d72a66d6c7284d5a35 to your computer and use it in GitHub Desktop.
Save Dessix/93e522405f09e3d72a66d6c7284d5a35 to your computer and use it in GitHub Desktop.
TransferOwnership Ignore Wrapper - NoTransfer
use std::ops::{Deref, DerefMut, Not};
use tungsten::{TransferOwnership, TransferToken};
pub struct NoTransfer<T: Sized>(T);
impl<T> TransferOwnership for NoTransfer<T> {
fn transfer_to(&self, _: &TransferToken) {
// Do nothing
}
}
impl<T> Deref for NoTransfer<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> DerefMut for NoTransfer<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<T> ::std::fmt::Display for NoTransfer<T>
where
T: ::std::fmt::Display,
{
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
::std::fmt::Display::fmt(&self.0, f)
}
}
impl<T> ::std::fmt::Debug for NoTransfer<T>
where
T: ::std::fmt::Debug,
{
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
::std::fmt::Debug::fmt(&self.0, f)
}
}
impl<T> Copy for NoTransfer<T> where T: Copy {}
impl<T> Clone for NoTransfer<T>
where
T: Clone,
{
fn clone(&self) -> Self {
NoTransfer(self.0.clone())
}
}
impl<T> PartialEq for NoTransfer<T>
where
T: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
PartialEq::eq(&self.0, &other.0)
}
}
impl<T> Eq for NoTransfer<T> where T: Eq {}
impl<T> PartialOrd for NoTransfer<T>
where
T: PartialOrd,
{
fn partial_cmp(&self, other: &Self) -> Option<::std::cmp::Ordering> {
::std::cmp::PartialOrd::partial_cmp(&self.0, &other.0)
}
fn lt(&self, other: &Self) -> bool {
::std::cmp::PartialOrd::lt(&self.0, &other.0)
}
fn le(&self, other: &Self) -> bool {
::std::cmp::PartialOrd::le(&self.0, &other.0)
}
fn gt(&self, other: &Self) -> bool {
::std::cmp::PartialOrd::gt(&self.0, &other.0)
}
fn ge(&self, other: &Self) -> bool {
::std::cmp::PartialOrd::ge(&self.0, &other.0)
}
}
impl<T> Ord for NoTransfer<T>
where
T: Ord,
{
fn cmp(&self, other: &Self) -> ::std::cmp::Ordering {
::std::cmp::Ord::cmp(&self.0, &other.0)
}
fn max(self, other: Self) -> Self
where
Self: Sized,
{
NoTransfer(::std::cmp::Ord::max(self.0, other.0))
}
fn min(self, other: Self) -> Self
where
Self: Sized,
{
NoTransfer(::std::cmp::Ord::min(self.0, other.0))
}
#[cfg(feature = "clamp")]
fn clamp(self, min: Self, max: Self) -> Self
where
Self: Sized,
{
::std::cmp::Ord::clamp(&self.0, min, max)
}
}
impl<T> Default for NoTransfer<T>
where
T: Default,
{
fn default() -> Self {
NoTransfer(Default::default())
}
}
impl<T> ::serde::Serialize for NoTransfer<T>
where
T: ::serde::Serialize,
{
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as ::serde::ser::Serializer>::Ok, <S as ::serde::ser::Serializer>::Error>
where
S: ::serde::ser::Serializer,
{
::serde::ser::Serialize::serialize(&self.0, serializer)
}
}
impl<'de, T> ::serde::Deserialize<'de> for NoTransfer<T>
where
T: ::serde::Deserialize<'de>,
{
fn deserialize<D>(deserializer: D) -> Result<Self, <D as ::serde::de::Deserializer<'de>>::Error>
where
D: ::serde::de::Deserializer<'de>,
{
::serde::de::Deserialize::deserialize(deserializer).map(move |res| NoTransfer(res))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment