Skip to content

Commit 9b88d87

Browse files
committed
Merge rust-bitcoin#4606: units: Improve docs
7fbe07a Use uniform docs for overflow (Tobin C. Harding) 153a6a2 Make Weight docs uniform with FeeRate (Tobin C. Harding) c87f729 Fix rustdocs on Weight (Tobin C. Harding) 02b523a Remove whitespace from encapsulate module (Tobin C. Harding) Pull request description: Make a sweep of the `units` crate's rustdocs. ACKs for top commit: apoelstra: ACK 7fbe07a; successfully ran local tests Tree-SHA512: ba50f3afb94dda43f89d04eb53c6e85df302292d4647fe81a20e3f7d1ca75e8ee8cdf6548864b2f3c33ed661205d109dbd763db1061ea45a59eab25f134191f8
2 parents b7b9d42 + 7fbe07a commit 9b88d87

File tree

4 files changed

+23
-25
lines changed

4 files changed

+23
-25
lines changed

units/src/block.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,13 @@ impl BlockHeight {
8787
/// Returns block height as a `u32`.
8888
pub const fn to_u32(self) -> u32 { self.0 }
8989

90-
/// Attempt to subtract two [`BlockHeight`]s, returning `None` in case of overflow.
90+
/// Attempt to subtract two [`BlockHeight`]s, returning `None` if overflow occurred.
9191
#[must_use]
9292
pub fn checked_sub(self, other: Self) -> Option<BlockHeightInterval> {
9393
self.to_u32().checked_sub(other.to_u32()).map(BlockHeightInterval)
9494
}
9595

96-
/// Attempt to add an interval to this [`BlockHeight`], returning `None` in case of overflow.
96+
/// Attempt to add an interval to this [`BlockHeight`], returning `None` if overflow occurred.
9797
#[must_use]
9898
pub fn checked_add(self, other: BlockHeightInterval) -> Option<Self> {
9999
self.to_u32().checked_add(other.to_u32()).map(Self)
@@ -148,13 +148,13 @@ impl BlockHeightInterval {
148148
/// Returns block interval as a `u32`.
149149
pub const fn to_u32(self) -> u32 { self.0 }
150150

151-
/// Attempt to subtract two [`BlockHeightInterval`]s, returning `None` in case of overflow.
151+
/// Attempt to subtract two [`BlockHeightInterval`]s, returning `None` if overflow occurred.
152152
#[must_use]
153153
pub fn checked_sub(self, other: Self) -> Option<Self> {
154154
self.to_u32().checked_sub(other.to_u32()).map(Self)
155155
}
156156

157-
/// Attempt to add two [`BlockHeightInterval`]s, returning `None` in case of overflow.
157+
/// Attempt to add two [`BlockHeightInterval`]s, returning `None` if overflow occurred.
158158
#[must_use]
159159
pub fn checked_add(self, other: Self) -> Option<Self> {
160160
self.to_u32().checked_add(other.to_u32()).map(Self)
@@ -224,13 +224,13 @@ impl BlockMtp {
224224
Self::from_u32(u32::from(timestamps[5]))
225225
}
226226

227-
/// Attempt to subtract two [`BlockMtp`]s, returning `None` in case of overflow.
227+
/// Attempt to subtract two [`BlockMtp`]s, returning `None` if overflow occurred.
228228
#[must_use]
229229
pub fn checked_sub(self, other: Self) -> Option<BlockMtpInterval> {
230230
self.to_u32().checked_sub(other.to_u32()).map(BlockMtpInterval)
231231
}
232232

233-
/// Attempt to add an interval to this [`BlockMtp`], returning `None` in case of overflow.
233+
/// Attempt to add an interval to this [`BlockMtp`], returning `None` if overflow occurred.
234234
#[must_use]
235235
pub fn checked_add(self, other: BlockMtpInterval) -> Option<Self> {
236236
self.to_u32().checked_add(other.to_u32()).map(Self)
@@ -317,13 +317,13 @@ impl BlockMtpInterval {
317317
relative::NumberOf512Seconds::from_seconds_ceil(self.to_u32())
318318
}
319319

320-
/// Attempt to subtract two [`BlockMtpInterval`]s, returning `None` in case of overflow.
320+
/// Attempt to subtract two [`BlockMtpInterval`]s, returning `None` if overflow occurred.
321321
#[must_use]
322322
pub fn checked_sub(self, other: Self) -> Option<Self> {
323323
self.to_u32().checked_sub(other.to_u32()).map(Self)
324324
}
325325

326-
/// Attempt to add two [`BlockMtpInterval`]s, returning `None` in case of overflow.
326+
/// Attempt to add two [`BlockMtpInterval`]s, returning `None` if overflow occurred.
327327
#[must_use]
328328
pub fn checked_add(self, other: Self) -> Option<Self> {
329329
self.to_u32().checked_add(other.to_u32()).map(Self)

units/src/fee_rate/mod.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ impl FeeRate {
5252
/// The fee rate used to compute dust amount.
5353
pub const DUST: FeeRate = FeeRate::from_sat_per_vb_u32(3);
5454

55-
/// Constructs a new [`FeeRate`] from satoshis per 1000 weight units.
55+
/// Constructs a new [`FeeRate`] from satoshis per 1000 weight units,
56+
/// returning `None` if overflow occurred.
5657
pub const fn from_sat_per_kwu(sat_kwu: u64) -> Option<Self> {
5758
// No `map()` in const context.
5859
match sat_kwu.checked_mul(4_000) {
@@ -61,11 +62,8 @@ impl FeeRate {
6162
}
6263
}
6364

64-
/// Constructs a new [`FeeRate`] from satoshis per virtual bytes.
65-
///
66-
/// # Errors
67-
///
68-
/// Returns [`None`] on arithmetic overflow.
65+
/// Constructs a new [`FeeRate`] from satoshis per virtual bytes,
66+
/// returning `None` if overflow occurred.
6967
pub const fn from_sat_per_vb(sat_vb: u64) -> Option<Self> {
7068
// No `map()` in const context.
7169
match sat_vb.checked_mul(1_000_000) {
@@ -80,7 +78,8 @@ impl FeeRate {
8078
FeeRate::from_sat_per_mvb(sat_vb * 1_000_000)
8179
}
8280

83-
/// Constructs a new [`FeeRate`] from satoshis per kilo virtual bytes (1,000 vbytes).
81+
/// Constructs a new [`FeeRate`] from satoshis per kilo virtual bytes (1,000 vbytes),
82+
/// returning `None` if overflow occurred.
8483
pub const fn from_sat_per_kvb(sat_kvb: u64) -> Option<Self> {
8584
// No `map()` in const context.
8685
match sat_kvb.checked_mul(1_000) {
@@ -109,7 +108,7 @@ impl FeeRate {
109108

110109
/// Checked multiplication.
111110
///
112-
/// Computes `self * rhs` returning [`None`] if overflow occurred.
111+
/// Computes `self * rhs`, returning [`None`] if overflow occurred.
113112
#[must_use]
114113
pub const fn checked_mul(self, rhs: u64) -> Option<Self> {
115114
// No `map()` in const context.
@@ -133,7 +132,7 @@ impl FeeRate {
133132

134133
/// Checked addition.
135134
///
136-
/// Computes `self + rhs` returning [`None`] if overflow occurred.
135+
/// Computes `self + rhs` returning [`None`] is case of overflow.
137136
#[must_use]
138137
pub const fn checked_add(self, rhs: FeeRate) -> Option<Self> {
139138
// No `map()` in const context.
@@ -145,7 +144,7 @@ impl FeeRate {
145144

146145
/// Checked subtraction.
147146
///
148-
/// Computes `self - rhs` returning [`None`] if overflow occurred.
147+
/// Computes `self - rhs`, returning [`None`] if overflow occurred.
149148
#[must_use]
150149
pub const fn checked_sub(self, rhs: FeeRate) -> Option<Self> {
151150
// No `map()` in const context.

units/src/locktime/absolute.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl fmt::Display for ParseHeightError {
114114

115115
#[cfg(feature = "std")]
116116
impl std::error::Error for ParseHeightError {
117-
// To be consistent with `write_err` we need to **not** return source in case of overflow
117+
// To be consistent with `write_err` we need to **not** return source if overflow occurred
118118
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { self.0.source() }
119119
}
120120

@@ -239,7 +239,7 @@ impl fmt::Display for ParseTimeError {
239239

240240
#[cfg(feature = "std")]
241241
impl std::error::Error for ParseTimeError {
242-
// To be consistent with `write_err` we need to **not** return source in case of overflow
242+
// To be consistent with `write_err` we need to **not** return source if overflow occurred
243243
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { self.0.source() }
244244
}
245245

@@ -398,7 +398,7 @@ impl ParseError {
398398
}
399399
}
400400

401-
// To be consistent with `write_err` we need to **not** return source in case of overflow
401+
// To be consistent with `write_err` we need to **not** return source if overflow occurred
402402
#[cfg(feature = "std")]
403403
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
404404
use core::num::IntErrorKind;

units/src/weight.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@ use crate::CheckedSum;
1616
pub const WITNESS_SCALE_FACTOR: usize = 4;
1717

1818
mod encapsulate {
19-
2019
/// The weight of a transaction or block.
2120
///
22-
/// This is an integer newtype representing [`Weight`] in `wu`. It provides protection
23-
/// against mixing up types as well as basic formatting features.
21+
/// This is an integer newtype representing weight in weight units. It provides protection
22+
/// against mixing up the types, conversion functions, and basic formatting.
2423
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
2524
pub struct Weight(u64);
2625

@@ -38,7 +37,7 @@ mod encapsulate {
3837
pub use encapsulate::Weight;
3938

4039
impl Weight {
41-
/// 0 wu.
40+
/// Zero weight units (wu).
4241
///
4342
/// Equivalent to [`MIN`](Self::MIN), may better express intent in some contexts.
4443
pub const ZERO: Weight = Weight::from_wu(0);

0 commit comments

Comments
 (0)