Take self by value

Turns out by default clippy does not lint certain parts of the public
API. Specifically, by setting

  avoid-breaking-exported-api = false

we can get clippy to lint the public API for various things including
`wrong_self_convention`.

Clippy emits:

 warning: methods with the following characteristics: (`to_*` and `self`
 type is `Copy`) usually take `self` by value

As suggested, take `self` by value for methods named `to_*`.
This commit is contained in:
Tobin C. Harding 2022-08-02 08:33:22 +10:00
parent ef5014f3e8
commit 0786d92a5c
10 changed files with 22 additions and 22 deletions

View File

@ -81,7 +81,7 @@ impl PackedLockTime {
/// Returns the inner `u32`. /// Returns the inner `u32`.
#[inline] #[inline]
pub fn to_u32(&self) -> u32 { pub fn to_u32(self) -> u32 {
self.0 self.0
} }
} }
@ -338,8 +338,8 @@ impl LockTime {
/// // let is_satisfied = n.partial_cmp(&lock_time).expect("invalid comparison").is_le(); /// // let is_satisfied = n.partial_cmp(&lock_time).expect("invalid comparison").is_le();
/// ``` /// ```
#[inline] #[inline]
pub fn to_consensus_u32(&self) -> u32 { pub fn to_consensus_u32(self) -> u32 {
match *self { match self {
LockTime::Blocks(ref h) => h.to_consensus_u32(), LockTime::Blocks(ref h) => h.to_consensus_u32(),
LockTime::Seconds(ref t) => t.to_consensus_u32(), LockTime::Seconds(ref t) => t.to_consensus_u32(),
} }
@ -446,7 +446,7 @@ impl Height {
/// assert!(lock_time.is_block_height()); /// assert!(lock_time.is_block_height());
/// assert_eq!(lock_time.to_consensus_u32(), n_lock_time); /// assert_eq!(lock_time.to_consensus_u32(), n_lock_time);
#[inline] #[inline]
pub fn to_consensus_u32(&self) -> u32 { pub fn to_consensus_u32(self) -> u32 {
self.0 self.0
} }
} }
@ -529,7 +529,7 @@ impl Time {
/// assert_eq!(lock_time.to_consensus_u32(), n_lock_time); /// assert_eq!(lock_time.to_consensus_u32(), n_lock_time);
/// ``` /// ```
#[inline] #[inline]
pub fn to_consensus_u32(&self) -> u32 { pub fn to_consensus_u32(self) -> u32 {
self.0 self.0
} }
} }

View File

@ -379,7 +379,7 @@ impl Sequence {
/// Returns the inner 32bit integer value of Sequence. /// Returns the inner 32bit integer value of Sequence.
#[inline] #[inline]
pub fn to_consensus_u32(&self) -> u32 { pub fn to_consensus_u32(self) -> u32 {
self.0 self.0
} }
} }

View File

@ -78,7 +78,7 @@ macro_rules! impl_array_newtype {
/// Returns the underlying bytes. /// Returns the underlying bytes.
#[inline] #[inline]
pub fn to_bytes(&self) -> [$ty; $len] { self.0.clone() } pub fn to_bytes(self) -> [$ty; $len] { self.0.clone() }
/// Returns the underlying bytes. /// Returns the underlying bytes.
#[inline] #[inline]

View File

@ -545,7 +545,7 @@ impl ExtendedPrivKey {
} }
/// Constructs ECDSA compressed private key matching internal secret key representation. /// Constructs ECDSA compressed private key matching internal secret key representation.
pub fn to_priv(&self) -> PrivateKey { pub fn to_priv(self) -> PrivateKey {
PrivateKey { PrivateKey {
compressed: true, compressed: true,
network: self.network, network: self.network,
@ -555,7 +555,7 @@ impl ExtendedPrivKey {
/// Constructs BIP340 keypair for Schnorr signatures and Taproot use matching the internal /// Constructs BIP340 keypair for Schnorr signatures and Taproot use matching the internal
/// secret key representation. /// secret key representation.
pub fn to_keypair<C: secp256k1::Signing>(&self, secp: &Secp256k1<C>) -> KeyPair { pub fn to_keypair<C: secp256k1::Signing>(self, secp: &Secp256k1<C>) -> KeyPair {
KeyPair::from_seckey_slice(secp, &self.private_key[..]).expect("BIP32 internal private key representation is broken") KeyPair::from_seckey_slice(secp, &self.private_key[..]).expect("BIP32 internal private key representation is broken")
} }
@ -677,7 +677,7 @@ impl ExtendedPubKey {
} }
/// Constructs ECDSA compressed public key matching internal public key representation. /// Constructs ECDSA compressed public key matching internal public key representation.
pub fn to_pub(&self) -> PublicKey { pub fn to_pub(self) -> PublicKey {
PublicKey { PublicKey {
compressed: true, compressed: true,
inner: self.public_key inner: self.public_key
@ -686,7 +686,7 @@ impl ExtendedPubKey {
/// Constructs BIP340 x-only public key for BIP-340 signatures and Taproot use matching /// Constructs BIP340 x-only public key for BIP-340 signatures and Taproot use matching
/// the internal public key representation. /// the internal public key representation.
pub fn to_x_only_pub(&self) -> XOnlyPublicKey { pub fn to_x_only_pub(self) -> XOnlyPublicKey {
XOnlyPublicKey::from(self.public_key) XOnlyPublicKey::from(self.public_key)
} }

View File

@ -46,7 +46,7 @@ impl EcdsaSig {
} }
/// Serializes an ECDSA signature (inner secp256k1 signature in DER format). /// Serializes an ECDSA signature (inner secp256k1 signature in DER format).
pub fn to_vec(&self) -> Vec<u8> { pub fn to_vec(self) -> Vec<u8> {
// TODO: add support to serialize to a writer to SerializedSig // TODO: add support to serialize to a writer to SerializedSig
self.sig.serialize_der() self.sig.serialize_der()
.iter().copied() .iter().copied()

View File

@ -167,7 +167,7 @@ impl PublicKey {
} }
/// Serialize the public key to bytes /// Serialize the public key to bytes
pub fn to_bytes(&self) -> Vec<u8> { pub fn to_bytes(self) -> Vec<u8> {
let mut buf = Vec::new(); let mut buf = Vec::new();
self.write_into(&mut buf).expect("vecs don't error"); self.write_into(&mut buf).expect("vecs don't error");
buf buf
@ -219,11 +219,11 @@ impl PublicKey {
/// pk("04c4b0bbb339aa236bff38dbe6a451e111972a7909a126bc424013cba2ec33bc38e98ac269ffe028345c31ac8d0a365f29c8f7e7cfccac72f84e1acd02bc554f35"), /// pk("04c4b0bbb339aa236bff38dbe6a451e111972a7909a126bc424013cba2ec33bc38e98ac269ffe028345c31ac8d0a365f29c8f7e7cfccac72f84e1acd02bc554f35"),
/// ]; /// ];
/// ///
/// unsorted.sort_unstable_by_key(PublicKey::to_sort_key); /// unsorted.sort_unstable_by_key(|k| PublicKey::to_sort_key(*k));
/// ///
/// assert_eq!(unsorted, sorted); /// assert_eq!(unsorted, sorted);
/// ``` /// ```
pub fn to_sort_key(&self) -> SortKey { pub fn to_sort_key(self) -> SortKey {
if self.compressed { if self.compressed {
let bytes = self.inner.serialize(); let bytes = self.inner.serialize();
let mut res = [0; 32]; let mut res = [0; 32];
@ -337,7 +337,7 @@ impl PrivateKey {
} }
/// Serialize the private key to bytes /// Serialize the private key to bytes
pub fn to_bytes(&self) -> Vec<u8> { pub fn to_bytes(self) -> Vec<u8> {
self.inner[..].to_vec() self.inner[..].to_vec()
} }
@ -364,7 +364,7 @@ impl PrivateKey {
} }
/// Get WIF encoding of this private key. /// Get WIF encoding of this private key.
pub fn to_wif(&self) -> String { pub fn to_wif(self) -> String {
let mut buf = String::new(); let mut buf = String::new();
buf.write_fmt(format_args!("{}", self)).unwrap(); buf.write_fmt(format_args!("{}", self)).unwrap();
buf.shrink_to_fit(); buf.shrink_to_fit();
@ -833,7 +833,7 @@ mod tests {
}, },
]; ];
for mut vector in vectors { for mut vector in vectors {
vector.input.sort_by_cached_key(PublicKey::to_sort_key); vector.input.sort_by_cached_key(|k| PublicKey::to_sort_key(*k));
assert_eq!(vector.input, vector.expect); assert_eq!(vector.input, vector.expect);
} }
} }

View File

@ -179,7 +179,7 @@ mod message_signing {
/// Convert to base64 encoding. /// Convert to base64 encoding.
#[cfg(feature = "base64")] #[cfg(feature = "base64")]
#[cfg_attr(docsrs, doc(cfg(feature = "base64")))] #[cfg_attr(docsrs, doc(cfg(feature = "base64")))]
pub fn to_base64(&self) -> String { pub fn to_base64(self) -> String {
base64::encode(&self.serialize()[..]) base64::encode(&self.serialize()[..])
} }
} }

View File

@ -241,7 +241,7 @@ impl SchnorrSig {
} }
/// Serialize SchnorrSig /// Serialize SchnorrSig
pub fn to_vec(&self) -> Vec<u8> { pub fn to_vec(self) -> Vec<u8> {
// TODO: add support to serialize to a writer to SerializedSig // TODO: add support to serialize to a writer to SerializedSig
let mut ser_sig = self.sig.as_ref().to_vec(); let mut ser_sig = self.sig.as_ref().to_vec();
if self.hash_ty == SchnorrSighashType::Default { if self.hash_ty == SchnorrSighashType::Default {

View File

@ -87,7 +87,7 @@ impl TapTweakHash {
} }
/// Converts a `TapTweakHash` into a `Scalar` ready for use with key tweaking API. /// Converts a `TapTweakHash` into a `Scalar` ready for use with key tweaking API.
pub fn to_scalar(&self) -> Scalar { pub fn to_scalar(self) -> Scalar {
// This is statistically extremely unlikely to panic. // This is statistically extremely unlikely to panic.
Scalar::from_be_bytes(self.into_inner()).expect("hash value greater than curve order") Scalar::from_be_bytes(self.into_inner()).expect("hash value greater than curve order")
} }

View File

@ -106,7 +106,7 @@ macro_rules! construct_uint {
} }
/// Convert a big integer into a byte array using big-endian encoding /// Convert a big integer into a byte array using big-endian encoding
pub fn to_be_bytes(&self) -> [u8; $n_words * 8] { pub fn to_be_bytes(self) -> [u8; $n_words * 8] {
use super::endian::u64_to_array_be; use super::endian::u64_to_array_be;
let mut res = [0; $n_words * 8]; let mut res = [0; $n_words * 8];
for i in 0..$n_words { for i in 0..$n_words {