forked from rust-bitcoin/rust-bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge rust-bitcoin#3728: units: Unify and flesh out ops impls
68f3c3e api: Run just check-api (Tobin C. Harding) b956940 Add ops unit tests for amount types (Tobin C. Harding) d88306f units: Implement ops for amount types (Tobin C. Harding) 4a3aa4a Add ops unit tests for Weight (Tobin C. Harding) 43ef9a6 units: Implement ops for Weight (Tobin C. Harding) c5fbc7e units: Add assign macros (Tobin C. Harding) 20a79da units: Use one level of path for ops traits (Tobin C. Harding) 6ce5385 units: Add macros for Add and Sub (Tobin C. Harding) b31ca72 units: Use rhs instead of other (Tobin C. Harding) cd0fa2d Make FeeRate ops impls more terse (Tobin C. Harding) Pull request description: Sort out `Add`, `AddAssign`, `Sub`, and `SubAssign` for the following types: - `Amount` - `SignedAmount` - `FeeRate` - `Weight` And clean up as we go. Note that `BlockInterval` isn't touched in this PR - it looks correct already. The unit tests in the two patches "Add ops tests ..." can be moved if you want to verify that they don't build without the patch they follow. ACKs for top commit: apoelstra: ACK 68f3c3e; successfully ran local tests Tree-SHA512: a82d3bf288f61b169b8cff498e81bd2cd123c8dcbf534413233ff03f06102a42508e09b2f7e5b268b21f82d4bf2b3612cd88dea1231b4d3e6455c7e99f82e729
- Loading branch information
Showing
10 changed files
with
465 additions
and
135 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// SPDX-License-Identifier: CC0-1.0 | ||
|
||
//! Internal macros. | ||
//! | ||
//! Macros meant to be used inside the `bitcoin-units` library. | ||
/// Implements `ops::Add` for various references. | ||
/// | ||
/// Requires `$ty` it implement `Add` e.g. 'impl Add<T> for T'. Adds impls of: | ||
/// | ||
/// - Add<T> for &T | ||
/// - Add<&T> for T | ||
/// - Add<&T> for &T | ||
macro_rules! impl_add_for_references { | ||
($ty:ident) => { | ||
impl core::ops::Add<$ty> for &$ty { | ||
type Output = $ty; | ||
|
||
fn add(self, rhs: $ty) -> Self::Output { *self + rhs } | ||
} | ||
|
||
impl core::ops::Add<&$ty> for $ty { | ||
type Output = $ty; | ||
|
||
fn add(self, rhs: &$ty) -> Self::Output { self + *rhs } | ||
} | ||
|
||
impl<'a> core::ops::Add<&'a $ty> for &$ty { | ||
type Output = $ty; | ||
|
||
fn add(self, rhs: &'a $ty) -> Self::Output { *self + *rhs } | ||
} | ||
} | ||
} | ||
pub(crate) use impl_add_for_references; | ||
|
||
/// Implement `ops::AddAssign` for `$ty` and `&$ty`. | ||
macro_rules! impl_add_assign { | ||
($ty:ident) => { | ||
impl core::ops::AddAssign<$ty> for $ty { | ||
fn add_assign(&mut self, rhs: $ty) { *self = *self + rhs } | ||
} | ||
|
||
impl core::ops::AddAssign<&$ty> for $ty { | ||
fn add_assign(&mut self, rhs: &$ty) { *self = *self + *rhs } | ||
} | ||
} | ||
} | ||
pub(crate) use impl_add_assign; | ||
|
||
/// Implement `ops::Sub` for various references. | ||
/// | ||
/// Requires `$ty` it implement `Sub` e.g. 'impl Sub<T> for T'. Adds impls of: | ||
/// | ||
/// - Sub<T> for &T | ||
/// - Sub<&T> for T | ||
/// - Sub<&T> for &T | ||
macro_rules! impl_sub_for_references { | ||
($ty:ident) => { | ||
impl core::ops::Sub<$ty> for &$ty { | ||
type Output = $ty; | ||
|
||
fn sub(self, rhs: $ty) -> Self::Output { *self - rhs } | ||
} | ||
|
||
impl core::ops::Sub<&$ty> for $ty { | ||
type Output = $ty; | ||
|
||
fn sub(self, rhs: &$ty) -> Self::Output { self - *rhs } | ||
} | ||
|
||
impl<'a> core::ops::Sub<&'a $ty> for &$ty { | ||
type Output = $ty; | ||
|
||
fn sub(self, rhs: &'a $ty) -> Self::Output { *self - *rhs } | ||
} | ||
} | ||
} | ||
pub(crate) use impl_sub_for_references; | ||
|
||
/// Implement `ops::SubAssign` for `$ty` and `&$ty`. | ||
macro_rules! impl_sub_assign { | ||
($ty:ident) => { | ||
impl core::ops::SubAssign<$ty> for $ty { | ||
fn sub_assign(&mut self, rhs: $ty) { *self = *self - rhs } | ||
} | ||
|
||
impl core::ops::SubAssign<&$ty> for $ty { | ||
fn sub_assign(&mut self, rhs: &$ty) { *self = *self - *rhs } | ||
} | ||
} | ||
} | ||
pub(crate) use impl_sub_assign; |
This file contains 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
Oops, something went wrong.