Use `as_`,`to_`,`into_` conventions for array types.

Signed-off-by: Jean Pierre Dudey <jeandudey@hotmail.com>
This commit is contained in:
Jean Pierre Dudey 2018-08-20 17:20:43 -04:00
parent 2d961412af
commit 4dfb98bd70
4 changed files with 23 additions and 12 deletions

View File

@ -289,11 +289,14 @@ impl Script {
/// Whether the script is the empty script
pub fn is_empty(&self) -> bool { self.0.is_empty() }
/// Convert the script into a byte vector
pub fn into_vec(self) -> Vec<u8> { self.0.into_vec() }
/// Returns the script data
pub fn as_bytes(&self) -> &[u8] { &*self.0 }
/// returns a copy of the script data
pub fn data (&self) -> Vec<u8> { self.0.clone().into_vec() }
/// Returns a copy of the script data
pub fn to_bytes(&self) -> Vec<u8> { self.0.clone().into_vec() }
/// Convert the script into a byte vector
pub fn into_bytes(self) -> Vec<u8> { self.0.into_vec() }
/// Compute the P2SH output corresponding to this redeem script
pub fn to_p2sh(&self) -> Script {

View File

@ -79,8 +79,16 @@ macro_rules! impl_array_newtype {
pub fn is_empty(&self) -> bool { false }
#[inline]
/// Returns the underlying data.
pub fn data(&self) -> [$ty; $len] { self.0.clone() }
/// Returns the underlying bytes.
pub fn as_bytes(&self) -> &[$ty; $len] { &self.0 }
#[inline]
/// Returns the underlying bytes.
pub fn to_bytes(&self) -> [$ty; $len] { self.0.clone() }
#[inline]
/// Returns the underlying bytes.
pub fn into_bytes(self) -> [$ty; $len] { self.0 }
}
impl<'a> From<&'a [$ty]> for $thing {

View File

@ -116,7 +116,7 @@ impl Address {
Address {
network: network,
payload: Payload::ScriptHash(
Hash160::from_data(builder.into_script().into_vec().as_slice())
Hash160::from_data(builder.into_script().as_bytes())
)
}
}
@ -127,7 +127,7 @@ impl Address {
use crypto::digest::Digest;
let mut digest = Sha256::new();
digest.input(script.clone().into_vec().as_slice());
digest.input(script.as_bytes());
let mut d = [0u8; 32];
digest.result(&mut d);
@ -151,14 +151,14 @@ impl Address {
use crypto::digest::Digest;
let mut digest = Sha256::new();
digest.input(script.clone().into_vec().as_slice());
digest.input(script.as_bytes());
let mut d = [0u8; 32];
digest.result(&mut d);
let ws = script::Builder::new().push_int(0).push_slice(&d).into_script();
Address {
network: network,
payload: Payload::ScriptHash(Hash160::from_data(ws.into_vec().as_slice()))
payload: Payload::ScriptHash(Hash160::from_data(ws.as_bytes()))
}
}

View File

@ -510,8 +510,8 @@ mod tests {
#[test]
fn test_sha256d_data() {
assert_eq!(
Sha256dHash::from_data(&[]).data(),
[
Sha256dHash::from_data(&[]).as_bytes(),
&[
0x5d, 0xf6, 0xe0, 0xe2, 0x76, 0x13, 0x59, 0xd3, 0x0a, 0x82, 0x75, 0x05, 0x8e, 0x29,
0x9f, 0xcc, 0x03, 0x81, 0x53, 0x45, 0x45, 0xf5, 0x5c, 0xf4, 0x3e, 0x41, 0x98, 0x3f,
0x5d, 0x4c, 0x94, 0x56,