Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
Add as_bytes and to_bytes functions
Browse files Browse the repository at this point in the history
As discussed and implemented in `rust-bitcoin` PR #2585 its useful to
get a byte slice as well as the already provided method for a array
reference. Also, if "alloc" feature is enabled its useful to be able to
get a vector.
  • Loading branch information
tcharding committed May 14, 2024
1 parent d1dee9f commit 42ebe29
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/hmac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ impl<const N: usize> Hmac<N> {
/// Returns a reference to the underlying byte array.
pub fn as_byte_array(&self) -> &[u8; N] { &self.0 }

/// Returns a reference to the underlying byte array as a slice.
pub fn as_bytes(&self) -> &[u8] { &self.0 }

/// Copies the underlying bytes into a new `Vec`.
#[cfg(feature = "alloc")]
pub fn to_bytes(&self) -> Vec<u8> { self.0.to_vec() }

/// Returns an all zero hash.
///
/// An all zeros hash is a made up construct because there is not a known input that can
Expand Down
9 changes: 9 additions & 0 deletions src/internal_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,15 @@ macro_rules! hash_type {
/// Returns a reference to the underlying byte array.
pub fn as_byte_array(&self) -> &[u8; $bits / 8] { &self.0 }

/// Returns a reference to the underlying byte array as a slice.
#[inline]
pub fn as_bytes(&self) -> &[u8] { &self.0 }

/// Copies the underlying bytes into a new `Vec`.
#[cfg(feature = "alloc")]
#[inline]
pub fn to_bytes(&self) -> Vec<u8> { self.0.to_vec() }

/// Returns an all zero hash.
///
/// An all zeros hash is a made up construct because there is not a known input that can
Expand Down
7 changes: 7 additions & 0 deletions src/sha256t.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ impl<T: Tag> Hash<T> {
/// Returns a reference to the underlying byte array.
pub fn as_byte_array(&self) -> &[u8; 32] { &self.0 }

/// Returns a reference to the underlying byte array as a slice.
pub fn as_bytes(&self) -> &[u8] { &self.0 }

/// Copies the underlying bytes into a new `Vec`.
#[cfg(feature = "alloc")]
pub fn to_bytes(&self) -> Vec<u8> { self.0.to_vec() }

/// Returns an all zero hash.
///
/// An all zeros hash is a made up construct because there is not a known input that can
Expand Down

0 comments on commit 42ebe29

Please sign in to comment.