Skip to content

Commit 8393083

Browse files
committed
Use fn name to_ instead of into_
Rust convention is to use `to_` for conversion methods that convert from an owned type to an owned `Copy` type. `into_` is for owned to owned non-`Copy` types. Re-name and deprecate conversion methods that use `into_` for `Copy` types to use `to_`.
1 parent 6b71fe6 commit 8393083

File tree

4 files changed

+61
-29
lines changed

4 files changed

+61
-29
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: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -484,33 +484,33 @@ impl Script {
484484
#[inline]
485485
pub fn is_p2sh(&self) -> bool {
486486
self.0.len() == 23
487-
&& self.0[0] == opcodes::all::OP_HASH160.into_u8()
488-
&& self.0[1] == opcodes::all::OP_PUSHBYTES_20.into_u8()
489-
&& self.0[22] == opcodes::all::OP_EQUAL.into_u8()
487+
&& self.0[0] == opcodes::all::OP_HASH160.to_u8()
488+
&& self.0[1] == opcodes::all::OP_PUSHBYTES_20.to_u8()
489+
&& self.0[22] == opcodes::all::OP_EQUAL.to_u8()
490490
}
491491

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

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

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

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

563563
/// Check if this is an OP_RETURN output.
564564
pub fn is_op_return (&self) -> bool {
565565
match self.0.first() {
566-
Some(b) => *b == opcodes::all::OP_RETURN.into_u8(),
566+
Some(b) => *b == opcodes::all::OP_RETURN.to_u8(),
567567
None => false
568568
}
569569
}
@@ -878,7 +878,7 @@ impl Builder {
878878
// We can special-case -1, 1-16
879879
if data == -1 || (data >= 1 && data <= 16) {
880880
let opcode = opcodes::All::from(
881-
(data - 1 + opcodes::OP_TRUE.into_u8() as i64) as u8
881+
(data - 1 + opcodes::OP_TRUE.to_u8() as i64) as u8
882882
);
883883
self.push_opcode(opcode)
884884
}
@@ -902,16 +902,16 @@ impl Builder {
902902
match data.len() as u64 {
903903
n if n < opcodes::Ordinary::OP_PUSHDATA1 as u64 => { self.0.push(n as u8); },
904904
n if n < 0x100 => {
905-
self.0.push(opcodes::Ordinary::OP_PUSHDATA1.into_u8());
905+
self.0.push(opcodes::Ordinary::OP_PUSHDATA1.to_u8());
906906
self.0.push(n as u8);
907907
},
908908
n if n < 0x10000 => {
909-
self.0.push(opcodes::Ordinary::OP_PUSHDATA2.into_u8());
909+
self.0.push(opcodes::Ordinary::OP_PUSHDATA2.to_u8());
910910
self.0.push((n % 0x100) as u8);
911911
self.0.push((n / 0x100) as u8);
912912
},
913913
n if n < 0x100000000 => {
914-
self.0.push(opcodes::Ordinary::OP_PUSHDATA4.into_u8());
914+
self.0.push(opcodes::Ordinary::OP_PUSHDATA4.to_u8());
915915
self.0.push((n % 0x100) as u8);
916916
self.0.push(((n / 0x100) % 0x100) as u8);
917917
self.0.push(((n / 0x10000) % 0x100) as u8);
@@ -941,7 +941,7 @@ impl Builder {
941941

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

src/util/address.rs

Lines changed: 15 additions & 5 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
}

src/util/schnorr.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,16 @@ impl TweakedKeyPair {
187187

188188
/// Returns the underlying key pair
189189
#[inline]
190+
#[deprecated(since = "0.29.0", note = "use to_inner instead")]
190191
pub fn into_inner(self) -> crate::KeyPair {
191192
self.0
192193
}
194+
195+
/// Returns the underlying key pair.
196+
#[inline]
197+
pub fn to_inner(self) -> crate::KeyPair {
198+
self.0
199+
}
193200
}
194201

195202
impl From<TweakedPublicKey> for crate::XOnlyPublicKey {

0 commit comments

Comments
 (0)