Skip to content

Commit c8ae04e

Browse files
committed
Merge rust-bitcoin/rust-bitcoin#798: Audit conversion methods
5fbb211 Use fn name to_ instead of as_ (Tobin Harding) 8ffa323 Use fn name to_ instead of into_ (Tobin Harding) 6874ce9 Remove as_inner (Tobin C. Harding) Pull request description: Rust has naming conventions surrounding conversion functions We have a handful of methods that are not following convention. This PR is done as three patches, separated by incorrect function name (`into_` or `as_`) and by whether or not the original method needs deprecating. Can be squashed if folks prefer. From the docs: https://rust-lang.github.io/api-guidelines/naming.html <h2><a class="header" href="https://rust-lang.github.io/api-guidelines/naming.html#ad-hoc-conversions-follow-as_-to_-into_-conventions-c-conv" id="ad-hoc-conversions-follow-as_-to_-into_-conventions-c-conv">Ad-hoc conversions follow <code>as_</code>, <code>to_</code>, <code>into_</code> conventions (C-CONV)</a></h2> <p>Conversions should be provided as methods, with names prefixed as follows:</p> Prefix | Cost | Ownership -- | -- | -- as_ | Free | borrowed -> borrowed to_ | Expensive | borrowed -> borrowed | | | borrowed -> owned (non-Copy types) | | | owned -> owned (Copy types) into_ | Variable | owned -> owned (non-Copy types) EDIT: I did actually audit all uses of `to_` when I first did this, I did this by grepping for `fn to_` and checking the output against the table. ACKs for top commit: apoelstra: ACK 5fbb211 Kixunil: ACK 5fbb211 Tree-SHA512: f750b2d1a10bc1d4bb030d8528a582701cc3d615aa8a8ab391324dae639544bb3629a19b372784e1e274a8ddcc613c621c7aae21a3ea54fde356a6aa5e611ac0
2 parents a94cf9d + af63b80 commit c8ae04e

File tree

7 files changed

+122
-54
lines changed

7 files changed

+122
-54
lines changed

src/blockdata/opcodes.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,14 @@ impl All {
736736

737737
/// Encode as a byte
738738
#[inline]
739+
#[deprecated(since = "0.29.0", note = "use to_u8 instead")]
739740
pub fn into_u8(self) -> u8 {
741+
self.to_u8()
742+
}
743+
744+
/// Encodes [`All`] as a byte.
745+
#[inline]
746+
pub fn to_u8(self) -> u8 {
740747
self.code
741748
}
742749
}
@@ -859,9 +866,17 @@ ordinary_opcode! {
859866
impl Ordinary {
860867
/// Encode as a byte
861868
#[inline]
869+
#[deprecated(since = "0.29.0", note = "use to_u8 instead")]
862870
pub fn into_u8(self) -> u8 {
871+
self.to_u8()
872+
}
873+
874+
/// Encodes [`All`] as a byte.
875+
#[inline]
876+
pub fn to_u8(self) -> u8 {
863877
self as u8
864878
}
879+
865880
}
866881

867882
#[cfg(test)]
@@ -872,7 +887,7 @@ mod tests {
872887

873888
macro_rules! roundtrip {
874889
($unique:expr, $op:ident) => {
875-
assert_eq!(all::$op, All::from(all::$op.into_u8()));
890+
assert_eq!(all::$op, All::from(all::$op.to_u8()));
876891

877892
let s1 = format!("{}", all::$op);
878893
let s2 = format!("{:?}", all::$op);

src/blockdata/script.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -485,33 +485,33 @@ impl Script {
485485
#[inline]
486486
pub fn is_p2sh(&self) -> bool {
487487
self.0.len() == 23
488-
&& self.0[0] == opcodes::all::OP_HASH160.into_u8()
489-
&& self.0[1] == opcodes::all::OP_PUSHBYTES_20.into_u8()
490-
&& self.0[22] == opcodes::all::OP_EQUAL.into_u8()
488+
&& self.0[0] == opcodes::all::OP_HASH160.to_u8()
489+
&& self.0[1] == opcodes::all::OP_PUSHBYTES_20.to_u8()
490+
&& self.0[22] == opcodes::all::OP_EQUAL.to_u8()
491491
}
492492

493493
/// Checks whether a script pubkey is a P2PKH output.
494494
#[inline]
495495
pub fn is_p2pkh(&self) -> bool {
496496
self.0.len() == 25
497-
&& self.0[0] == opcodes::all::OP_DUP.into_u8()
498-
&& self.0[1] == opcodes::all::OP_HASH160.into_u8()
499-
&& self.0[2] == opcodes::all::OP_PUSHBYTES_20.into_u8()
500-
&& self.0[23] == opcodes::all::OP_EQUALVERIFY.into_u8()
501-
&& self.0[24] == opcodes::all::OP_CHECKSIG.into_u8()
497+
&& self.0[0] == opcodes::all::OP_DUP.to_u8()
498+
&& self.0[1] == opcodes::all::OP_HASH160.to_u8()
499+
&& self.0[2] == opcodes::all::OP_PUSHBYTES_20.to_u8()
500+
&& self.0[23] == opcodes::all::OP_EQUALVERIFY.to_u8()
501+
&& self.0[24] == opcodes::all::OP_CHECKSIG.to_u8()
502502
}
503503

504504
/// Checks whether a script pubkey is a P2PK output.
505505
#[inline]
506506
pub fn is_p2pk(&self) -> bool {
507507
match self.len() {
508508
67 => {
509-
self.0[0] == opcodes::all::OP_PUSHBYTES_65.into_u8()
510-
&& self.0[66] == opcodes::all::OP_CHECKSIG.into_u8()
509+
self.0[0] == opcodes::all::OP_PUSHBYTES_65.to_u8()
510+
&& self.0[66] == opcodes::all::OP_CHECKSIG.to_u8()
511511
}
512512
35 => {
513-
self.0[0] == opcodes::all::OP_PUSHBYTES_33.into_u8()
514-
&& self.0[34] == opcodes::all::OP_CHECKSIG.into_u8()
513+
self.0[0] == opcodes::all::OP_PUSHBYTES_33.to_u8()
514+
&& self.0[34] == opcodes::all::OP_CHECKSIG.to_u8()
515515
}
516516
_ => false
517517
}
@@ -531,8 +531,8 @@ impl Script {
531531
let ver_opcode = opcodes::All::from(self.0[0]); // Version 0 or PUSHNUM_1-PUSHNUM_16
532532
let push_opbyte = self.0[1]; // Second byte push opcode 2-40 bytes
533533
WitnessVersion::from_opcode(ver_opcode).is_ok()
534-
&& push_opbyte >= opcodes::all::OP_PUSHBYTES_2.into_u8()
535-
&& push_opbyte <= opcodes::all::OP_PUSHBYTES_40.into_u8()
534+
&& push_opbyte >= opcodes::all::OP_PUSHBYTES_2.to_u8()
535+
&& push_opbyte <= opcodes::all::OP_PUSHBYTES_40.to_u8()
536536
// Check that the rest of the script has the correct size
537537
&& script_len - 2 == push_opbyte as usize
538538
}
@@ -542,29 +542,29 @@ impl Script {
542542
pub fn is_v0_p2wsh(&self) -> bool {
543543
self.0.len() == 34
544544
&& self.witness_version() == Some(WitnessVersion::V0)
545-
&& self.0[1] == opcodes::all::OP_PUSHBYTES_32.into_u8()
545+
&& self.0[1] == opcodes::all::OP_PUSHBYTES_32.to_u8()
546546
}
547547

548548
/// Checks whether a script pubkey is a P2WPKH output.
549549
#[inline]
550550
pub fn is_v0_p2wpkh(&self) -> bool {
551551
self.0.len() == 22
552552
&& self.witness_version() == Some(WitnessVersion::V0)
553-
&& self.0[1] == opcodes::all::OP_PUSHBYTES_20.into_u8()
553+
&& self.0[1] == opcodes::all::OP_PUSHBYTES_20.to_u8()
554554
}
555555

556556
/// Checks whether a script pubkey is a P2TR output.
557557
#[inline]
558558
pub fn is_v1_p2tr(&self) -> bool {
559559
self.0.len() == 34
560560
&& self.witness_version() == Some(WitnessVersion::V1)
561-
&& self.0[1] == opcodes::all::OP_PUSHBYTES_32.into_u8()
561+
&& self.0[1] == opcodes::all::OP_PUSHBYTES_32.to_u8()
562562
}
563563

564564
/// Check if this is an OP_RETURN output.
565565
pub fn is_op_return (&self) -> bool {
566566
match self.0.first() {
567-
Some(b) => *b == opcodes::all::OP_RETURN.into_u8(),
567+
Some(b) => *b == opcodes::all::OP_RETURN.to_u8(),
568568
None => false
569569
}
570570
}
@@ -645,7 +645,7 @@ impl Script {
645645
#[cfg(feature="bitcoinconsensus")]
646646
#[cfg_attr(docsrs, doc(cfg(feature = "bitcoinconsensus")))]
647647
pub fn verify_with_flags<F: Into<u32>>(&self, index: usize, amount: crate::Amount, spending: &[u8], flags: F) -> Result<(), Error> {
648-
Ok(bitcoinconsensus::verify_with_flags (&self.0[..], amount.as_sat(), spending, index, flags.into())?)
648+
Ok(bitcoinconsensus::verify_with_flags (&self.0[..], amount.to_sat(), spending, index, flags.into())?)
649649
}
650650

651651
/// Writes the assembly decoding of the script bytes to the formatter.
@@ -879,7 +879,7 @@ impl Builder {
879879
// We can special-case -1, 1-16
880880
if data == -1 || (1..=16).contains(&data) {
881881
let opcode = opcodes::All::from(
882-
(data - 1 + opcodes::OP_TRUE.into_u8() as i64) as u8
882+
(data - 1 + opcodes::OP_TRUE.to_u8() as i64) as u8
883883
);
884884
self.push_opcode(opcode)
885885
}
@@ -903,16 +903,16 @@ impl Builder {
903903
match data.len() as u64 {
904904
n if n < opcodes::Ordinary::OP_PUSHDATA1 as u64 => { self.0.push(n as u8); },
905905
n if n < 0x100 => {
906-
self.0.push(opcodes::Ordinary::OP_PUSHDATA1.into_u8());
906+
self.0.push(opcodes::Ordinary::OP_PUSHDATA1.to_u8());
907907
self.0.push(n as u8);
908908
},
909909
n if n < 0x10000 => {
910-
self.0.push(opcodes::Ordinary::OP_PUSHDATA2.into_u8());
910+
self.0.push(opcodes::Ordinary::OP_PUSHDATA2.to_u8());
911911
self.0.push((n % 0x100) as u8);
912912
self.0.push((n / 0x100) as u8);
913913
},
914914
n if n < 0x100000000 => {
915-
self.0.push(opcodes::Ordinary::OP_PUSHDATA4.into_u8());
915+
self.0.push(opcodes::Ordinary::OP_PUSHDATA4.to_u8());
916916
self.0.push((n % 0x100) as u8);
917917
self.0.push(((n / 0x100) % 0x100) as u8);
918918
self.0.push(((n / 0x10000) % 0x100) as u8);
@@ -942,7 +942,7 @@ impl Builder {
942942

943943
/// Adds a single opcode to the script.
944944
pub fn push_opcode(mut self, data: opcodes::All) -> Builder {
945-
self.0.push(data.into_u8());
945+
self.0.push(data.to_u8());
946946
self.1 = Some(data);
947947
self
948948
}

src/network/address.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ impl Encodable for AddrV2Message {
267267
fn consensus_encode<W: io::Write>(&self, mut e: W) -> Result<usize, io::Error> {
268268
let mut len = 0;
269269
len += self.time.consensus_encode(&mut e)?;
270-
len += VarInt(self.services.as_u64()).consensus_encode(&mut e)?;
270+
len += VarInt(self.services.to_u64()).consensus_encode(&mut e)?;
271271
len += self.addr.consensus_encode(&mut e)?;
272272

273273
// consensus_encode always encodes in LE, and we want to encode in BE.

src/network/constants.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,13 @@ impl ServiceFlags {
178178
}
179179

180180
/// Get the integer representation of this [ServiceFlags].
181+
#[deprecated(since = "0.29.0", note = "use to_u64 instead")]
181182
pub fn as_u64(self) -> u64 {
183+
self.to_u64()
184+
}
185+
186+
/// Gets the integer representation of this [`ServiceFlags`].
187+
pub fn to_u64(self) -> u64 {
182188
self.0
183189
}
184190
}

src/util/address.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,10 @@ impl WitnessVersion {
296296
/// If the opcode does not correspond to any witness version, errors with
297297
/// [`Error::MalformedWitnessVersion`].
298298
pub fn from_opcode(opcode: opcodes::All) -> Result<Self, Error> {
299-
match opcode.into_u8() {
299+
match opcode.to_u8() {
300300
0 => Ok(WitnessVersion::V0),
301-
version if version >= opcodes::all::OP_PUSHNUM_1.into_u8() && version <= opcodes::all::OP_PUSHNUM_16.into_u8() =>
302-
WitnessVersion::from_num(version - opcodes::all::OP_PUSHNUM_1.into_u8() + 1),
301+
version if version >= opcodes::all::OP_PUSHNUM_1.to_u8() && version <= opcodes::all::OP_PUSHNUM_16.to_u8() =>
302+
WitnessVersion::from_num(version - opcodes::all::OP_PUSHNUM_1.to_u8() + 1),
303303
_ => Err(Error::MalformedWitnessVersion)
304304
}
305305
}
@@ -326,7 +326,17 @@ impl WitnessVersion {
326326
/// NB: this is not the same as an integer representation of the opcode signifying witness
327327
/// version in bitcoin script. Thus, there is no function to directly convert witness version
328328
/// into a byte since the conversion requires context (bitcoin script or just a version number).
329+
#[deprecated(since = "0.29.0", note = "use to_num instead")]
329330
pub fn into_num(self) -> u8 {
331+
self.to_num()
332+
}
333+
334+
/// Returns integer version number representation for a given [`WitnessVersion`] value.
335+
///
336+
/// NB: this is not the same as an integer representation of the opcode signifying witness
337+
/// version in bitcoin script. Thus, there is no function to directly convert witness version
338+
/// into a byte since the conversion requires context (bitcoin script or just a version number).
339+
pub fn to_num(self) -> u8 {
330340
self as u8
331341
}
332342

@@ -342,7 +352,7 @@ impl WitnessVersion {
342352
impl From<WitnessVersion> for ::bech32::u5 {
343353
/// Converts [`WitnessVersion`] instance into corresponding Bech32(m) u5-value ([`bech32::u5`]).
344354
fn from(version: WitnessVersion) -> Self {
345-
::bech32::u5::try_from_u8(version.into_num()).expect("WitnessVersion must be 0..=16")
355+
::bech32::u5::try_from_u8(version.to_num()).expect("WitnessVersion must be 0..=16")
346356
}
347357
}
348358

@@ -351,7 +361,7 @@ impl From<WitnessVersion> for opcodes::All {
351361
fn from(version: WitnessVersion) -> opcodes::All {
352362
match version {
353363
WitnessVersion::V0 => opcodes::all::OP_PUSHBYTES_0,
354-
no => opcodes::All::from(opcodes::all::OP_PUSHNUM_1.into_u8() + no.into_num() - 1)
364+
no => opcodes::All::from(opcodes::all::OP_PUSHNUM_1.to_u8() + no.to_num() - 1)
355365
}
356366
}
357367
}
@@ -473,7 +483,7 @@ impl Payload {
473483
pub fn p2tr_tweaked(output_key: TweakedPublicKey) -> Payload {
474484
Payload::WitnessProgram {
475485
version: WitnessVersion::V1,
476-
program: output_key.as_inner().serialize().to_vec(),
486+
program: output_key.to_inner().serialize().to_vec(),
477487
}
478488
}
479489

0 commit comments

Comments
 (0)