Skip to content

Commit c6c690a

Browse files
committed
Merge rust-bitcoin#4603: units: Add must_use to checked arithmetic functions
afc0ce6 units: Add must_use to checked arithmetic functions (Tobin C. Harding) Pull request description: The checked arithmetic functions all consume self so we use `must_use` to help users not miss this point. Most are done, add the missing ones. ACKs for top commit: apoelstra: ACK afc0ce6; successfully ran local tests Tree-SHA512: 7105affff43827ed47a1c0b6e41a996aa538c7d53b891faf03e79a83164706d7e86db5fb184ac740fdf57bb43f8401a496cc64ea4da0da71eaa8c8cca16444c7
2 parents 17cd382 + afc0ce6 commit c6c690a

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

units/src/block.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,13 @@ impl BlockHeight {
8888
pub const fn to_u32(self) -> u32 { self.0 }
8989

9090
/// Attempt to subtract two [`BlockHeight`]s, returning `None` in case of overflow.
91+
#[must_use]
9192
pub fn checked_sub(self, other: Self) -> Option<BlockHeightInterval> {
9293
self.0.checked_sub(other.0).map(BlockHeightInterval)
9394
}
9495

9596
/// Attempt to add an interval to this [`BlockHeight`], returning `None` in case of overflow.
97+
#[must_use]
9698
pub fn checked_add(self, other: BlockHeightInterval) -> Option<Self> {
9799
self.0.checked_add(other.0).map(Self)
98100
}
@@ -147,9 +149,11 @@ impl BlockHeightInterval {
147149
pub const fn to_u32(self) -> u32 { self.0 }
148150

149151
/// Attempt to subtract two [`BlockHeightInterval`]s, returning `None` in case of overflow.
152+
#[must_use]
150153
pub fn checked_sub(self, other: Self) -> Option<Self> { self.0.checked_sub(other.0).map(Self) }
151154

152155
/// Attempt to add two [`BlockHeightInterval`]s, returning `None` in case of overflow.
156+
#[must_use]
153157
pub fn checked_add(self, other: Self) -> Option<Self> { self.0.checked_add(other.0).map(Self) }
154158
}
155159

@@ -217,11 +221,13 @@ impl BlockMtp {
217221
}
218222

219223
/// Attempt to subtract two [`BlockMtp`]s, returning `None` in case of overflow.
224+
#[must_use]
220225
pub fn checked_sub(self, other: Self) -> Option<BlockMtpInterval> {
221226
self.0.checked_sub(other.0).map(BlockMtpInterval)
222227
}
223228

224229
/// Attempt to add an interval to this [`BlockMtp`], returning `None` in case of overflow.
230+
#[must_use]
225231
pub fn checked_add(self, other: BlockMtpInterval) -> Option<Self> {
226232
self.0.checked_add(other.0).map(Self)
227233
}
@@ -308,9 +314,11 @@ impl BlockMtpInterval {
308314
}
309315

310316
/// Attempt to subtract two [`BlockMtpInterval`]s, returning `None` in case of overflow.
317+
#[must_use]
311318
pub fn checked_sub(self, other: Self) -> Option<Self> { self.0.checked_sub(other.0).map(Self) }
312319

313320
/// Attempt to add two [`BlockMtpInterval`]s, returning `None` in case of overflow.
321+
#[must_use]
314322
pub fn checked_add(self, other: Self) -> Option<Self> { self.0.checked_add(other.0).map(Self) }
315323
}
316324

units/src/fee.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ impl FeeRate {
161161
/// enough instead of falling short if rounded down.
162162
///
163163
/// Returns [`None`] if overflow occurred.
164+
#[must_use]
164165
pub const fn checked_mul_by_weight(self, weight: Weight) -> Option<Amount> {
165166
let wu = weight.to_wu();
166167
if let Some(fee_kwu) = self.to_sat_per_kwu_floor().checked_mul(wu) {
@@ -348,6 +349,7 @@ impl Weight {
348349
/// enough instead of falling short if rounded down.
349350
///
350351
/// Returns [`None`] if overflow occurred.
352+
#[must_use]
351353
pub const fn checked_mul_by_fee_rate(self, fee_rate: FeeRate) -> Option<Amount> {
352354
fee_rate.checked_mul_by_weight(self)
353355
}

0 commit comments

Comments
 (0)