Merge pull request #143 from jeandudey/2018-08-c-conv

Use `as_`,`to_`,`into_` conventions for array types.
This commit is contained in:
Andrew Poelstra 2018-08-20 23:11:11 +00:00 committed by GitHub
commit 3a2e1ce09e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 12 deletions

View File

@ -289,11 +289,14 @@ impl Script {
/// Whether the script is the empty script /// Whether the script is the empty script
pub fn is_empty(&self) -> bool { self.0.is_empty() } pub fn is_empty(&self) -> bool { self.0.is_empty() }
/// Convert the script into a byte vector /// Returns the script data
pub fn into_vec(self) -> Vec<u8> { self.0.into_vec() } pub fn as_bytes(&self) -> &[u8] { &*self.0 }
/// returns a copy of the script data /// Returns a copy of the script data
pub fn data (&self) -> Vec<u8> { self.0.clone().into_vec() } 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 /// Compute the P2SH output corresponding to this redeem script
pub fn to_p2sh(&self) -> 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 } pub fn is_empty(&self) -> bool { false }
#[inline] #[inline]
/// Returns the underlying data. /// Returns the underlying bytes.
pub fn data(&self) -> [$ty; $len] { self.0.clone() } 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 { impl<'a> From<&'a [$ty]> for $thing {

View File

@ -116,7 +116,7 @@ impl Address {
Address { Address {
network: network, network: network,
payload: Payload::ScriptHash( 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; use crypto::digest::Digest;
let mut digest = Sha256::new(); let mut digest = Sha256::new();
digest.input(script.clone().into_vec().as_slice()); digest.input(script.as_bytes());
let mut d = [0u8; 32]; let mut d = [0u8; 32];
digest.result(&mut d); digest.result(&mut d);
@ -151,14 +151,14 @@ impl Address {
use crypto::digest::Digest; use crypto::digest::Digest;
let mut digest = Sha256::new(); let mut digest = Sha256::new();
digest.input(script.clone().into_vec().as_slice()); digest.input(script.as_bytes());
let mut d = [0u8; 32]; let mut d = [0u8; 32];
digest.result(&mut d); digest.result(&mut d);
let ws = script::Builder::new().push_int(0).push_slice(&d).into_script(); let ws = script::Builder::new().push_int(0).push_slice(&d).into_script();
Address { Address {
network: network, 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] #[test]
fn test_sha256d_data() { fn test_sha256d_data() {
assert_eq!( 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, 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, 0x9f, 0xcc, 0x03, 0x81, 0x53, 0x45, 0x45, 0xf5, 0x5c, 0xf4, 0x3e, 0x41, 0x98, 0x3f,
0x5d, 0x4c, 0x94, 0x56, 0x5d, 0x4c, 0x94, 0x56,