Merge rust-bitcoin/rust-bitcoin#2768: Deprecate `to_vec` in favour of `to_bytes`

eda61ddfef Deprecate to_vec in favour of to_bytes (Tobin C. Harding)

Pull request description:

  Currently we have to method names for the same thing "copy this object into a vector". The library is easier to use if we are uniform and just use one.

  Elect to use `to_bytes`, for context see discussion in PR #2585.

ACKs for top commit:
  apoelstra:
    ACK eda61ddfef Nice. IMO we should start deprecating stuff for two releases rather than one, so that people have a year to update.

Tree-SHA512: 0aadd1258a07bfa53806f19a3c41af8d3b1132aa42e7a2015a59c58c4309d7a9b50b86d076c181ce5870ba5acd989feec32669352ecf857ae6fd982873482c34
This commit is contained in:
Andrew Poelstra 2024-05-28 14:50:38 +00:00
commit b31ac0fcb7
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
3 changed files with 15 additions and 3 deletions

View File

@ -283,7 +283,11 @@ impl Witness {
}
/// Convenience method to create an array of byte-arrays from this witness.
pub fn to_vec(&self) -> Vec<Vec<u8>> { self.iter().map(|s| s.to_vec()).collect() }
pub fn to_bytes(&self) -> Vec<Vec<u8>> { self.iter().map(|s| s.to_vec()).collect() }
/// Convenience method to create an array of byte-arrays from this witness.
#[deprecated(since = "TBD", note = "Use to_bytes instead")]
pub fn to_vec(&self) -> Vec<Vec<u8>> { self.to_bytes() }
/// Returns `true` if the witness contains no element.
pub fn is_empty(&self) -> bool { self.witness_elements == 0 }

View File

@ -57,7 +57,7 @@ impl Signature {
///
/// Note: this performs an extra heap allocation, you might prefer the
/// [`serialize`](Self::serialize) method instead.
pub fn to_vec(self) -> Vec<u8> {
pub fn to_bytes(self) -> Vec<u8> {
self.signature
.serialize_der()
.iter()
@ -66,6 +66,10 @@ impl Signature {
.collect()
}
/// Serializes an ECDSA signature (inner secp256k1 signature in DER format) into `Vec`.
#[deprecated(since = "TBD", note = "Use to_bytes instead")]
pub fn to_vec(self) -> Vec<u8> { self.to_bytes() }
/// Serializes an ECDSA signature (inner secp256k1 signature in DER format) to a `writer`.
#[inline]
pub fn serialize_to_writer<W: Write + ?Sized>(&self, writer: &mut W) -> Result<(), io::Error> {

View File

@ -46,7 +46,7 @@ impl Signature {
/// Serialize Signature
///
/// Note: this allocates on the heap, prefer [`serialize`](Self::serialize) if vec is not needed.
pub fn to_vec(self) -> Vec<u8> {
pub fn to_bytes(self) -> Vec<u8> {
let mut ser_sig = self.signature.as_ref().to_vec();
if self.sighash_type == TapSighashType::Default {
// default sighash type, don't add extra sighash byte
@ -56,6 +56,10 @@ impl Signature {
ser_sig
}
/// Serialize Signature
#[deprecated(since = "TBD", note = "Use to_bytes instead")]
pub fn to_vec(self) -> Vec<u8> { self.to_bytes() }
/// Serializes the signature to `writer`.
#[inline]
pub fn serialize_to_writer<W: Write + ?Sized>(&self, writer: &mut W) -> Result<(), io::Error> {