Skip to content

Commit 886c666

Browse files
committed
Merge rust-bitcoin#4627: units: Make error constructor private
f6dea36 units: Make error constructor private (Tobin C. Harding) Pull request description: We typically do not want to have public constructors on error types. Currently we do on the `TimeOverflowError` so that we can call it in `primitives` but it turns out we can just use `NumberOf512Seconds` constructors instead. ACKs for top commit: apoelstra: ACK f6dea36; successfully ran local tests Tree-SHA512: f8d4615f2adb506278f20352c97e2b6698d3f81fe27d5204f215faeb8fe208fc849ad6c82cd40963736e85e669287d2f0f0c2cbc0a0d5858c2677e693c1582ae
2 parents c36d295 + f6dea36 commit 886c666

File tree

2 files changed

+5
-23
lines changed

2 files changed

+5
-23
lines changed

primitives/src/sequence.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use core::fmt;
2020
use arbitrary::{Arbitrary, Unstructured};
2121
#[cfg(feature = "serde")]
2222
use serde::{Deserialize, Serialize};
23-
use units::locktime::relative::TimeOverflowError;
23+
use units::locktime::relative::{NumberOf512Seconds, TimeOverflowError};
2424
use units::parse::{self, PrefixedHexError, UnprefixedHexError};
2525

2626
use crate::locktime::relative;
@@ -156,11 +156,8 @@ impl Sequence {
156156
/// Will return an error if the input cannot be encoded in 16 bits.
157157
#[inline]
158158
pub fn from_seconds_floor(seconds: u32) -> Result<Self, TimeOverflowError> {
159-
if let Ok(interval) = u16::try_from(seconds / 512) {
160-
Ok(Sequence::from_512_second_intervals(interval))
161-
} else {
162-
Err(TimeOverflowError::new(seconds))
163-
}
159+
let intervals = NumberOf512Seconds::from_seconds_floor(seconds)?;
160+
Ok(Sequence::from_512_second_intervals(intervals.to_512_second_intervals()))
164161
}
165162

166163
/// Constructs a new relative lock-time from seconds, converting the seconds into 512 second
@@ -169,11 +166,8 @@ impl Sequence {
169166
/// Will return an error if the input cannot be encoded in 16 bits.
170167
#[inline]
171168
pub fn from_seconds_ceil(seconds: u32) -> Result<Self, TimeOverflowError> {
172-
if let Ok(interval) = u16::try_from((seconds + 511) / 512) {
173-
Ok(Sequence::from_512_second_intervals(interval))
174-
} else {
175-
Err(TimeOverflowError::new(seconds))
176-
}
169+
let intervals = NumberOf512Seconds::from_seconds_ceil(seconds)?;
170+
Ok(Sequence::from_512_second_intervals(intervals.to_512_second_intervals()))
177171
}
178172

179173
/// Constructs a new sequence from a u32 value.

units/src/locktime/relative.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -211,18 +211,6 @@ pub struct TimeOverflowError {
211211
pub(crate) seconds: u32,
212212
}
213213

214-
impl TimeOverflowError {
215-
/// Constructs a new `TimeOverflowError` using `seconds`.
216-
///
217-
/// # Panics
218-
///
219-
/// If `seconds` would not actually overflow a `u16`.
220-
pub fn new(seconds: u32) -> Self {
221-
assert!(u16::try_from((seconds + 511) / 512).is_err());
222-
Self { seconds }
223-
}
224-
}
225-
226214
impl fmt::Display for TimeOverflowError {
227215
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
228216
write!(

0 commit comments

Comments
 (0)