Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add PrecDec impl [NTRN-436] #6

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/neutron-std/src/types/neutron/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ pub mod ibcratelimit;
pub mod interchainqueries;
pub mod interchaintxs;
pub mod transfer;
pub mod util;
84 changes: 84 additions & 0 deletions packages/neutron-std/src/types/neutron/util/forward_ref.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/// # ⚠ THIS IS AN INTERNAL IMPLEMENTATION DETAIL. DO NOT USE.
///
/// Given an implementation of `T == U`, implements:
/// - `&T == U`
/// - `T == &U`
///
/// We don't need to add `&T == &U` here because this is implemented automatically.
#[doc(hidden)]
#[macro_export]
macro_rules! forward_ref_partial_eq {
($t:ty, $u:ty) => {
// `&T == U`
impl<'a> PartialEq<$u> for &'a $t {
#[inline]
fn eq(&self, rhs: &$u) -> bool {
**self == *rhs // Implement via T == U
}
}

// `T == &U`
impl PartialEq<&$u> for $t {
#[inline]
fn eq(&self, rhs: &&$u) -> bool {
*self == **rhs // Implement via T == U
}
}
};
}

/// implements binary operators "&T op U", "T op &U", "&T op &U"
/// based on "T op U" where T and U are expected to be `Copy`able
///
/// Copied from `libcore`
macro_rules! forward_ref_binop {
(impl $imp:ident, $method:ident for $t:ty, $u:ty) => {
impl<'a> $imp<$u> for &'a $t {
type Output = <$t as $imp<$u>>::Output;

#[inline]
#[track_caller]
fn $method(self, other: $u) -> <$t as $imp<$u>>::Output {
$imp::$method(*self, other)
}
}

impl $imp<&$u> for $t {
type Output = <$t as $imp<$u>>::Output;

#[inline]
#[track_caller]
fn $method(self, other: &$u) -> <$t as $imp<$u>>::Output {
$imp::$method(self, *other)
}
}

impl $imp<&$u> for &$t {
type Output = <$t as $imp<$u>>::Output;

#[inline]
#[track_caller]
fn $method(self, other: &$u) -> <$t as $imp<$u>>::Output {
$imp::$method(*self, *other)
}
}
};
}

/// implements "T op= &U", based on "T op= U"
/// where U is expected to be `Copy`able
///
/// Copied from `libcore`
macro_rules! forward_ref_op_assign {
(impl $imp:ident, $method:ident for $t:ty, $u:ty) => {
impl $imp<&$u> for $t {
#[inline]
#[track_caller]
fn $method(&mut self, other: &$u) {
$imp::$method(self, *other);
}
}
};
}

pub(crate) use {forward_ref_binop, forward_ref_op_assign, forward_ref_partial_eq};
2 changes: 2 additions & 0 deletions packages/neutron-std/src/types/neutron/util/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod precdec;
mod forward_ref;
Loading