Skip to content

Commit 1dc7aa3

Browse files
committed
Implement string parsing for Sequence
`Sequence` didn't have `FromStr` nor `TryFrom<{stringly type}>` implemented by accident. This moves a macro for implementing them from `locktime` module to the `parse` module, renames it for clarity and uses it to implement parsing for `Sequence`.
1 parent 4fd8155 commit 1dc7aa3

File tree

3 files changed

+41
-33
lines changed

3 files changed

+41
-33
lines changed

src/blockdata/locktime.rs

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use crate::consensus::encode::{self, Decodable, Encodable};
2828
use crate::io::{self, Read, Write};
2929
use crate::prelude::*;
3030
use crate::internal_macros::write_err;
31+
use crate::impl_parse_str_through_int;
3132

3233
/// The Threshold for deciding whether a lock time value is a height or a time (see [Bitcoin Core]).
3334
///
@@ -135,38 +136,7 @@ impl From<PackedLockTime> for u32 {
135136
}
136137
}
137138

138-
/// Implements `TryFrom<$from> for $to` using `parse::int`, mapping the output using `fn`
139-
macro_rules! impl_tryfrom_str_single {
140-
($($from:ty, $to:ident $(, $fn:ident)?);*) => {
141-
$(
142-
impl TryFrom<$from> for $to {
143-
type Error = ParseIntError;
144-
145-
fn try_from(s: $from) -> Result<Self, Self::Error> {
146-
parse::int(s).map($to $(:: $fn)?)
147-
}
148-
}
149-
)*
150-
}
151-
}
152-
153-
/// Implements `TryFrom<{&str, String, Box<str>}> for $to` using `parse::int`, mapping the output using `fn`
154-
macro_rules! impl_tryfrom_str {
155-
($to:ident $(, $fn:ident)?) => {
156-
impl_tryfrom_str_single!(&str, $to $(, $fn)?; String, $to $(, $fn)?; Box<str>, $to $(, $fn)?);
157-
158-
impl FromStr for $to {
159-
type Err = ParseIntError;
160-
161-
fn from_str(s: &str) -> Result<Self, Self::Err> {
162-
parse::int(s).map($to $(:: $fn)?)
163-
}
164-
}
165-
166-
}
167-
}
168-
169-
impl_tryfrom_str!(PackedLockTime);
139+
impl_parse_str_through_int!(PackedLockTime);
170140

171141
impl fmt::LowerHex for PackedLockTime {
172142
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -376,7 +346,7 @@ impl LockTime {
376346
}
377347
}
378348

379-
impl_tryfrom_str!(LockTime, from_consensus);
349+
impl_parse_str_through_int!(LockTime, from_consensus);
380350

381351
impl From<Height> for LockTime {
382352
fn from(h: Height) -> Self {

src/blockdata/transaction.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use crate::hash_types::{Sighash, Txid, Wtxid};
3232
use crate::VarInt;
3333
use crate::util::sighash::UINT256_ONE;
3434
use crate::internal_macros::{impl_consensus_encoding, serde_string_impl, serde_struct_human_string_impl, write_err};
35+
use crate::impl_parse_str_through_int;
3536

3637
#[cfg(doc)]
3738
use crate::util::sighash::SchnorrSighashType;
@@ -422,6 +423,8 @@ impl fmt::Display for RelativeLockTimeError {
422423
}
423424
}
424425

426+
impl_parse_str_through_int!(Sequence);
427+
425428
#[cfg(feature = "std")]
426429
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
427430
impl std::error::Error for RelativeLockTimeError {

src/parse.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,38 @@ pub(crate) fn int<T: Integer, S: AsRef<str> + Into<String>>(s: S) -> Result<T, P
8686
}
8787

8888
impl_std_error!(ParseIntError, source);
89+
90+
/// Implements `TryFrom<$from> for $to` using `parse::int`, mapping the output using `fn`
91+
#[macro_export]
92+
macro_rules! impl_tryfrom_str_through_int_single {
93+
($($from:ty, $to:ident $(, $fn:ident)?);*) => {
94+
$(
95+
impl core::convert::TryFrom<$from> for $to {
96+
type Error = $crate::error::ParseIntError;
97+
98+
fn try_from(s: $from) -> Result<Self, Self::Error> {
99+
$crate::parse::int(s).map($to $(:: $fn)?)
100+
}
101+
}
102+
)*
103+
}
104+
}
105+
106+
/// Implements `FromStr` and `TryFrom<{&str, String, Box<str>}> for $to` using `parse::int`, mapping the output using `fn`
107+
///
108+
/// The `Error` type is `ParseIntError`
109+
#[macro_export]
110+
macro_rules! impl_parse_str_through_int {
111+
($to:ident $(, $fn:ident)?) => {
112+
$crate::impl_tryfrom_str_through_int_single!(&str, $to $(, $fn)?; String, $to $(, $fn)?; Box<str>, $to $(, $fn)?);
113+
114+
impl core::str::FromStr for $to {
115+
type Err = $crate::error::ParseIntError;
116+
117+
fn from_str(s: &str) -> Result<Self, Self::Err> {
118+
$crate::parse::int(s).map($to $(:: $fn)?)
119+
}
120+
}
121+
122+
}
123+
}

0 commit comments

Comments
 (0)