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_`.
This commit is contained in:
Tobin Harding 2022-01-19 13:41:45 +11:00 committed by Tobin C. Harding
parent 6874ce91e2
commit 8ffa32315d
4 changed files with 61 additions and 29 deletions

View File

@ -736,7 +736,14 @@ impl All {
/// Encode as a byte /// Encode as a byte
#[inline] #[inline]
#[deprecated(since = "0.29.0", note = "use to_u8 instead")]
pub fn into_u8(self) -> u8 { pub fn into_u8(self) -> u8 {
self.to_u8()
}
/// Encodes [`All`] as a byte.
#[inline]
pub fn to_u8(self) -> u8 {
self.code self.code
} }
} }
@ -859,9 +866,17 @@ ordinary_opcode! {
impl Ordinary { impl Ordinary {
/// Encode as a byte /// Encode as a byte
#[inline] #[inline]
#[deprecated(since = "0.29.0", note = "use to_u8 instead")]
pub fn into_u8(self) -> u8 { pub fn into_u8(self) -> u8 {
self.to_u8()
}
/// Encodes [`All`] as a byte.
#[inline]
pub fn to_u8(self) -> u8 {
self as u8 self as u8
} }
} }
#[cfg(test)] #[cfg(test)]
@ -872,7 +887,7 @@ mod tests {
macro_rules! roundtrip { macro_rules! roundtrip {
($unique:expr, $op:ident) => { ($unique:expr, $op:ident) => {
assert_eq!(all::$op, All::from(all::$op.into_u8())); assert_eq!(all::$op, All::from(all::$op.to_u8()));
let s1 = format!("{}", all::$op); let s1 = format!("{}", all::$op);
let s2 = format!("{:?}", all::$op); let s2 = format!("{:?}", all::$op);

View File

@ -484,20 +484,20 @@ impl Script {
#[inline] #[inline]
pub fn is_p2sh(&self) -> bool { pub fn is_p2sh(&self) -> bool {
self.0.len() == 23 self.0.len() == 23
&& self.0[0] == opcodes::all::OP_HASH160.into_u8() && self.0[0] == opcodes::all::OP_HASH160.to_u8()
&& self.0[1] == opcodes::all::OP_PUSHBYTES_20.into_u8() && self.0[1] == opcodes::all::OP_PUSHBYTES_20.to_u8()
&& self.0[22] == opcodes::all::OP_EQUAL.into_u8() && self.0[22] == opcodes::all::OP_EQUAL.to_u8()
} }
/// Checks whether a script pubkey is a P2PKH output. /// Checks whether a script pubkey is a P2PKH output.
#[inline] #[inline]
pub fn is_p2pkh(&self) -> bool { pub fn is_p2pkh(&self) -> bool {
self.0.len() == 25 self.0.len() == 25
&& self.0[0] == opcodes::all::OP_DUP.into_u8() && self.0[0] == opcodes::all::OP_DUP.to_u8()
&& self.0[1] == opcodes::all::OP_HASH160.into_u8() && self.0[1] == opcodes::all::OP_HASH160.to_u8()
&& self.0[2] == opcodes::all::OP_PUSHBYTES_20.into_u8() && self.0[2] == opcodes::all::OP_PUSHBYTES_20.to_u8()
&& self.0[23] == opcodes::all::OP_EQUALVERIFY.into_u8() && self.0[23] == opcodes::all::OP_EQUALVERIFY.to_u8()
&& self.0[24] == opcodes::all::OP_CHECKSIG.into_u8() && self.0[24] == opcodes::all::OP_CHECKSIG.to_u8()
} }
/// Checks whether a script pubkey is a P2PK output. /// Checks whether a script pubkey is a P2PK output.
@ -505,12 +505,12 @@ impl Script {
pub fn is_p2pk(&self) -> bool { pub fn is_p2pk(&self) -> bool {
match self.len() { match self.len() {
67 => { 67 => {
self.0[0] == opcodes::all::OP_PUSHBYTES_65.into_u8() self.0[0] == opcodes::all::OP_PUSHBYTES_65.to_u8()
&& self.0[66] == opcodes::all::OP_CHECKSIG.into_u8() && self.0[66] == opcodes::all::OP_CHECKSIG.to_u8()
} }
35 => { 35 => {
self.0[0] == opcodes::all::OP_PUSHBYTES_33.into_u8() self.0[0] == opcodes::all::OP_PUSHBYTES_33.to_u8()
&& self.0[34] == opcodes::all::OP_CHECKSIG.into_u8() && self.0[34] == opcodes::all::OP_CHECKSIG.to_u8()
} }
_ => false _ => false
} }
@ -530,8 +530,8 @@ impl Script {
let ver_opcode = opcodes::All::from(self.0[0]); // Version 0 or PUSHNUM_1-PUSHNUM_16 let ver_opcode = opcodes::All::from(self.0[0]); // Version 0 or PUSHNUM_1-PUSHNUM_16
let push_opbyte = self.0[1]; // Second byte push opcode 2-40 bytes let push_opbyte = self.0[1]; // Second byte push opcode 2-40 bytes
WitnessVersion::from_opcode(ver_opcode).is_ok() WitnessVersion::from_opcode(ver_opcode).is_ok()
&& push_opbyte >= opcodes::all::OP_PUSHBYTES_2.into_u8() && push_opbyte >= opcodes::all::OP_PUSHBYTES_2.to_u8()
&& push_opbyte <= opcodes::all::OP_PUSHBYTES_40.into_u8() && push_opbyte <= opcodes::all::OP_PUSHBYTES_40.to_u8()
// Check that the rest of the script has the correct size // Check that the rest of the script has the correct size
&& script_len - 2 == push_opbyte as usize && script_len - 2 == push_opbyte as usize
} }
@ -541,7 +541,7 @@ impl Script {
pub fn is_v0_p2wsh(&self) -> bool { pub fn is_v0_p2wsh(&self) -> bool {
self.0.len() == 34 self.0.len() == 34
&& self.witness_version() == Some(WitnessVersion::V0) && self.witness_version() == Some(WitnessVersion::V0)
&& self.0[1] == opcodes::all::OP_PUSHBYTES_32.into_u8() && self.0[1] == opcodes::all::OP_PUSHBYTES_32.to_u8()
} }
/// Checks whether a script pubkey is a P2WPKH output. /// Checks whether a script pubkey is a P2WPKH output.
@ -549,7 +549,7 @@ impl Script {
pub fn is_v0_p2wpkh(&self) -> bool { pub fn is_v0_p2wpkh(&self) -> bool {
self.0.len() == 22 self.0.len() == 22
&& self.witness_version() == Some(WitnessVersion::V0) && self.witness_version() == Some(WitnessVersion::V0)
&& self.0[1] == opcodes::all::OP_PUSHBYTES_20.into_u8() && self.0[1] == opcodes::all::OP_PUSHBYTES_20.to_u8()
} }
/// Checks whether a script pubkey is a P2TR output. /// Checks whether a script pubkey is a P2TR output.
@ -557,13 +557,13 @@ impl Script {
pub fn is_v1_p2tr(&self) -> bool { pub fn is_v1_p2tr(&self) -> bool {
self.0.len() == 34 self.0.len() == 34
&& self.witness_version() == Some(WitnessVersion::V1) && self.witness_version() == Some(WitnessVersion::V1)
&& self.0[1] == opcodes::all::OP_PUSHBYTES_32.into_u8() && self.0[1] == opcodes::all::OP_PUSHBYTES_32.to_u8()
} }
/// Check if this is an OP_RETURN output. /// Check if this is an OP_RETURN output.
pub fn is_op_return (&self) -> bool { pub fn is_op_return (&self) -> bool {
match self.0.first() { match self.0.first() {
Some(b) => *b == opcodes::all::OP_RETURN.into_u8(), Some(b) => *b == opcodes::all::OP_RETURN.to_u8(),
None => false None => false
} }
} }
@ -878,7 +878,7 @@ impl Builder {
// We can special-case -1, 1-16 // We can special-case -1, 1-16
if data == -1 || (data >= 1 && data <= 16) { if data == -1 || (data >= 1 && data <= 16) {
let opcode = opcodes::All::from( let opcode = opcodes::All::from(
(data - 1 + opcodes::OP_TRUE.into_u8() as i64) as u8 (data - 1 + opcodes::OP_TRUE.to_u8() as i64) as u8
); );
self.push_opcode(opcode) self.push_opcode(opcode)
} }
@ -902,16 +902,16 @@ impl Builder {
match data.len() as u64 { match data.len() as u64 {
n if n < opcodes::Ordinary::OP_PUSHDATA1 as u64 => { self.0.push(n as u8); }, n if n < opcodes::Ordinary::OP_PUSHDATA1 as u64 => { self.0.push(n as u8); },
n if n < 0x100 => { n if n < 0x100 => {
self.0.push(opcodes::Ordinary::OP_PUSHDATA1.into_u8()); self.0.push(opcodes::Ordinary::OP_PUSHDATA1.to_u8());
self.0.push(n as u8); self.0.push(n as u8);
}, },
n if n < 0x10000 => { n if n < 0x10000 => {
self.0.push(opcodes::Ordinary::OP_PUSHDATA2.into_u8()); self.0.push(opcodes::Ordinary::OP_PUSHDATA2.to_u8());
self.0.push((n % 0x100) as u8); self.0.push((n % 0x100) as u8);
self.0.push((n / 0x100) as u8); self.0.push((n / 0x100) as u8);
}, },
n if n < 0x100000000 => { n if n < 0x100000000 => {
self.0.push(opcodes::Ordinary::OP_PUSHDATA4.into_u8()); self.0.push(opcodes::Ordinary::OP_PUSHDATA4.to_u8());
self.0.push((n % 0x100) as u8); self.0.push((n % 0x100) as u8);
self.0.push(((n / 0x100) % 0x100) as u8); self.0.push(((n / 0x100) % 0x100) as u8);
self.0.push(((n / 0x10000) % 0x100) as u8); self.0.push(((n / 0x10000) % 0x100) as u8);
@ -941,7 +941,7 @@ impl Builder {
/// Adds a single opcode to the script. /// Adds a single opcode to the script.
pub fn push_opcode(mut self, data: opcodes::All) -> Builder { pub fn push_opcode(mut self, data: opcodes::All) -> Builder {
self.0.push(data.into_u8()); self.0.push(data.to_u8());
self.1 = Some(data); self.1 = Some(data);
self self
} }

View File

@ -296,10 +296,10 @@ impl WitnessVersion {
/// If the opcode does not correspond to any witness version, errors with /// If the opcode does not correspond to any witness version, errors with
/// [`Error::MalformedWitnessVersion`]. /// [`Error::MalformedWitnessVersion`].
pub fn from_opcode(opcode: opcodes::All) -> Result<Self, Error> { pub fn from_opcode(opcode: opcodes::All) -> Result<Self, Error> {
match opcode.into_u8() { match opcode.to_u8() {
0 => Ok(WitnessVersion::V0), 0 => Ok(WitnessVersion::V0),
version if version >= opcodes::all::OP_PUSHNUM_1.into_u8() && version <= opcodes::all::OP_PUSHNUM_16.into_u8() => version if version >= opcodes::all::OP_PUSHNUM_1.to_u8() && version <= opcodes::all::OP_PUSHNUM_16.to_u8() =>
WitnessVersion::from_num(version - opcodes::all::OP_PUSHNUM_1.into_u8() + 1), WitnessVersion::from_num(version - opcodes::all::OP_PUSHNUM_1.to_u8() + 1),
_ => Err(Error::MalformedWitnessVersion) _ => Err(Error::MalformedWitnessVersion)
} }
} }
@ -326,7 +326,17 @@ impl WitnessVersion {
/// NB: this is not the same as an integer representation of the opcode signifying witness /// NB: this is not the same as an integer representation of the opcode signifying witness
/// version in bitcoin script. Thus, there is no function to directly convert witness version /// version in bitcoin script. Thus, there is no function to directly convert witness version
/// into a byte since the conversion requires context (bitcoin script or just a version number). /// into a byte since the conversion requires context (bitcoin script or just a version number).
#[deprecated(since = "0.29.0", note = "use to_num instead")]
pub fn into_num(self) -> u8 { pub fn into_num(self) -> u8 {
self.to_num()
}
/// Returns integer version number representation for a given [`WitnessVersion`] value.
///
/// NB: this is not the same as an integer representation of the opcode signifying witness
/// version in bitcoin script. Thus, there is no function to directly convert witness version
/// into a byte since the conversion requires context (bitcoin script or just a version number).
pub fn to_num(self) -> u8 {
self as u8 self as u8
} }
@ -342,7 +352,7 @@ impl WitnessVersion {
impl From<WitnessVersion> for ::bech32::u5 { impl From<WitnessVersion> for ::bech32::u5 {
/// Converts [`WitnessVersion`] instance into corresponding Bech32(m) u5-value ([`bech32::u5`]). /// Converts [`WitnessVersion`] instance into corresponding Bech32(m) u5-value ([`bech32::u5`]).
fn from(version: WitnessVersion) -> Self { fn from(version: WitnessVersion) -> Self {
::bech32::u5::try_from_u8(version.into_num()).expect("WitnessVersion must be 0..=16") ::bech32::u5::try_from_u8(version.to_num()).expect("WitnessVersion must be 0..=16")
} }
} }
@ -351,7 +361,7 @@ impl From<WitnessVersion> for opcodes::All {
fn from(version: WitnessVersion) -> opcodes::All { fn from(version: WitnessVersion) -> opcodes::All {
match version { match version {
WitnessVersion::V0 => opcodes::all::OP_PUSHBYTES_0, WitnessVersion::V0 => opcodes::all::OP_PUSHBYTES_0,
no => opcodes::All::from(opcodes::all::OP_PUSHNUM_1.into_u8() + no.into_num() - 1) no => opcodes::All::from(opcodes::all::OP_PUSHNUM_1.to_u8() + no.to_num() - 1)
} }
} }
} }

View File

@ -187,9 +187,16 @@ impl TweakedKeyPair {
/// Returns the underlying key pair /// Returns the underlying key pair
#[inline] #[inline]
#[deprecated(since = "0.29.0", note = "use to_inner instead")]
pub fn into_inner(self) -> crate::KeyPair { pub fn into_inner(self) -> crate::KeyPair {
self.0 self.0
} }
/// Returns the underlying key pair.
#[inline]
pub fn to_inner(self) -> crate::KeyPair {
self.0
}
} }
impl From<TweakedPublicKey> for crate::XOnlyPublicKey { impl From<TweakedPublicKey> for crate::XOnlyPublicKey {