Skip to content

Commit 20cd232

Browse files
committed
Move impl_array_newtype to internals
Discussion rust-bitcoin#5331 suggested removing `bitcoin` as a direct dependency could be a reasonable goal for the `p2p` crate. One conflict along the way is the use of this macro in `bip152` to implement the array-like traits for the short IDs. Because `bitcoin` depends on `internals`, as does `p2p`, we can move this macro into `internals` to help detangle `p2p` and `bitcoin`. Moves the deprecated methods to the `impl` blocks.
1 parent 7c5a5a9 commit 20cd232

File tree

5 files changed

+134
-127
lines changed

5 files changed

+134
-127
lines changed

bitcoin/src/bip152.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use internals::ToU64 as _;
1717
use io::{BufRead, Write};
1818

1919
use crate::consensus::encode::{self, Decodable, Encodable, ReadExt, WriteExt};
20-
use crate::internal_macros::{self, impl_array_newtype, impl_array_newtype_stringify};
20+
use crate::internal_macros::{self, impl_array_newtype_stringify};
2121
use crate::prelude::Vec;
2222
use crate::transaction::TxIdentifier;
2323
use crate::{block, consensus, Block, BlockChecked, BlockHash, Transaction};
@@ -99,7 +99,7 @@ impl Decodable for PrefilledTransaction {
9999
/// Short transaction IDs are used to represent a transaction without sending a full 256-bit hash.
100100
#[derive(PartialEq, Eq, Clone, Copy, Hash, Default, PartialOrd, Ord)]
101101
pub struct ShortId([u8; 6]);
102-
impl_array_newtype!(ShortId, u8, 6);
102+
internals::impl_array_newtype!(ShortId, u8, 6);
103103
impl_array_newtype_stringify!(ShortId, 6);
104104

105105
impl ShortId {

bitcoin/src/bip32.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,24 @@ pub type ExtendedPrivKey = Xpriv;
3939
/// A chain code
4040
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
4141
pub struct ChainCode([u8; 32]);
42-
internal_macros::impl_array_newtype!(ChainCode, u8, 32);
42+
internals::impl_array_newtype!(ChainCode, u8, 32);
4343
internal_macros::impl_array_newtype_stringify!(ChainCode, 32);
4444

4545
impl ChainCode {
4646
fn from_hmac(hmac: Hmac<sha512::Hash>) -> Self {
4747
Self(*hmac.as_byte_array().split_array::<32, 32>().1)
4848
}
49+
50+
/// Copies the underlying bytes into a new `Vec`.
51+
#[inline]
52+
#[deprecated(since = "TBD", note = "use to_vec instead")]
53+
pub fn to_bytes(self) -> alloc::vec::Vec<u8> { self.to_vec() }
4954
}
5055

5156
/// A fingerprint
5257
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
5358
pub struct Fingerprint([u8; 4]);
54-
internal_macros::impl_array_newtype!(Fingerprint, u8, 4);
59+
internals::impl_array_newtype!(Fingerprint, u8, 4);
5560
internal_macros::impl_array_newtype_stringify!(Fingerprint, 4);
5661

5762
hash_newtype! {

bitcoin/src/blockdata/constants.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//! single transaction.
88
99
use crate::block::{self, Block, Checked};
10-
use crate::internal_macros::{impl_array_newtype, impl_array_newtype_stringify};
10+
use crate::internal_macros::impl_array_newtype_stringify;
1111
use crate::locktime::absolute;
1212
use crate::network::{Network, Params};
1313
use crate::opcodes::all::*;
@@ -192,7 +192,7 @@ pub fn genesis_block(params: impl AsRef<Params>) -> Block<Checked> {
192192
/// The uniquely identifying hash of the target blockchain.
193193
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
194194
pub struct ChainHash([u8; 32]);
195-
impl_array_newtype!(ChainHash, u8, 32);
195+
internals::impl_array_newtype!(ChainHash, u8, 32);
196196
impl_array_newtype_stringify!(ChainHash, 32);
197197

198198
impl ChainHash {
@@ -261,6 +261,11 @@ impl ChainHash {
261261
pub fn from_genesis_block_hash(block_hash: crate::BlockHash) -> Self {
262262
Self(block_hash.to_byte_array())
263263
}
264+
265+
/// Copies the underlying bytes into a new `Vec`.
266+
#[inline]
267+
#[deprecated(since = "TBD", note = "use to_vec instead")]
268+
pub fn to_bytes(self) -> alloc::vec::Vec<u8> { self.to_vec() }
264269
}
265270

266271
#[cfg(test)]

bitcoin/src/internal_macros.rs

Lines changed: 0 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -278,125 +278,4 @@ macro_rules! define_extension_trait {
278278
}
279279
pub(crate) use define_extension_trait;
280280

281-
/// Implements standard array methods for a given wrapper type.
282-
macro_rules! impl_array_newtype {
283-
($thing:ident, $ty:ty, $len:literal) => {
284-
impl $thing {
285-
/// Constructs a new `Self` by wrapping `bytes`.
286-
#[inline]
287-
pub fn from_byte_array(bytes: [u8; $len]) -> Self { Self(bytes) }
288-
289-
/// Returns a reference the underlying byte array.
290-
#[inline]
291-
pub fn as_byte_array(&self) -> &[u8; $len] { &self.0 }
292-
293-
/// Returns the underlying byte array.
294-
#[inline]
295-
pub fn to_byte_array(self) -> [u8; $len] {
296-
// We rely on `Copy` being implemented for $thing so conversion
297-
// methods use the correct Rust naming conventions.
298-
fn check_copy<T: Copy>() {}
299-
check_copy::<$thing>();
300-
301-
self.0
302-
}
303-
304-
/// Copies the underlying bytes into a new `Vec`.
305-
#[inline]
306-
pub fn to_vec(self) -> alloc::vec::Vec<u8> { self.0.to_vec() }
307-
308-
/// Returns a slice of the underlying bytes.
309-
#[inline]
310-
pub fn as_bytes(&self) -> &[u8] { &self.0 }
311-
312-
/// Copies the underlying bytes into a new `Vec`.
313-
#[inline]
314-
#[deprecated(since = "TBD", note = "use to_vec instead")]
315-
pub fn to_bytes(self) -> alloc::vec::Vec<u8> { self.to_vec() }
316-
317-
/// Converts the object to a raw pointer.
318-
#[inline]
319-
pub fn as_ptr(&self) -> *const $ty {
320-
let &$thing(ref dat) = self;
321-
dat.as_ptr()
322-
}
323-
324-
/// Converts the object to a mutable raw pointer.
325-
#[inline]
326-
pub fn as_mut_ptr(&mut self) -> *mut $ty {
327-
let &mut $thing(ref mut dat) = self;
328-
dat.as_mut_ptr()
329-
}
330-
331-
/// Returns the length of the object as an array.
332-
#[inline]
333-
pub fn len(&self) -> usize { $len }
334-
335-
/// Returns whether the object, as an array, is empty. Always false.
336-
#[inline]
337-
pub fn is_empty(&self) -> bool { false }
338-
}
339-
340-
impl<'a> core::convert::From<[$ty; $len]> for $thing {
341-
fn from(data: [$ty; $len]) -> Self { $thing(data) }
342-
}
343-
344-
impl<'a> core::convert::From<&'a [$ty; $len]> for $thing {
345-
fn from(data: &'a [$ty; $len]) -> Self { $thing(*data) }
346-
}
347-
348-
impl<'a> core::convert::TryFrom<&'a [$ty]> for $thing {
349-
type Error = core::array::TryFromSliceError;
350-
351-
fn try_from(data: &'a [$ty]) -> core::result::Result<Self, Self::Error> {
352-
use core::convert::TryInto;
353-
354-
Ok($thing(data.try_into()?))
355-
}
356-
}
357-
358-
impl AsRef<[$ty; $len]> for $thing {
359-
fn as_ref(&self) -> &[$ty; $len] { &self.0 }
360-
}
361-
362-
impl AsMut<[$ty; $len]> for $thing {
363-
fn as_mut(&mut self) -> &mut [$ty; $len] { &mut self.0 }
364-
}
365-
366-
impl AsRef<[$ty]> for $thing {
367-
fn as_ref(&self) -> &[$ty] { &self.0 }
368-
}
369-
370-
impl AsMut<[$ty]> for $thing {
371-
fn as_mut(&mut self) -> &mut [$ty] { &mut self.0 }
372-
}
373281

374-
impl core::borrow::Borrow<[$ty; $len]> for $thing {
375-
fn borrow(&self) -> &[$ty; $len] { &self.0 }
376-
}
377-
378-
impl core::borrow::BorrowMut<[$ty; $len]> for $thing {
379-
fn borrow_mut(&mut self) -> &mut [$ty; $len] { &mut self.0 }
380-
}
381-
382-
// The following two are valid because `[T; N]: Borrow<[T]>`
383-
impl core::borrow::Borrow<[$ty]> for $thing {
384-
fn borrow(&self) -> &[$ty] { &self.0 }
385-
}
386-
387-
impl core::borrow::BorrowMut<[$ty]> for $thing {
388-
fn borrow_mut(&mut self) -> &mut [$ty] { &mut self.0 }
389-
}
390-
391-
impl<I> core::ops::Index<I> for $thing
392-
where
393-
[$ty]: core::ops::Index<I>,
394-
{
395-
type Output = <[$ty] as core::ops::Index<I>>::Output;
396-
397-
#[inline]
398-
fn index(&self, index: I) -> &Self::Output { &self.0[index] }
399-
}
400-
};
401-
}
402-
pub(crate) use impl_array_newtype;

internals/src/macros.rs

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,121 @@ macro_rules! _emit_alloc {
244244
macro_rules! _emit_alloc {
245245
($($tokens:tt)*) => {};
246246
}
247+
248+
/// Implements standard array methods for a given wrapper type.
249+
#[macro_export]
250+
macro_rules! impl_array_newtype {
251+
($thing:ident, $ty:ty, $len:literal) => {
252+
impl $thing {
253+
/// Constructs a new `Self` by wrapping `bytes`.
254+
#[inline]
255+
pub fn from_byte_array(bytes: [u8; $len]) -> Self { Self(bytes) }
256+
257+
/// Returns a reference the underlying byte array.
258+
#[inline]
259+
pub fn as_byte_array(&self) -> &[u8; $len] { &self.0 }
260+
261+
/// Returns the underlying byte array.
262+
#[inline]
263+
pub fn to_byte_array(self) -> [u8; $len] {
264+
// We rely on `Copy` being implemented for $thing so conversion
265+
// methods use the correct Rust naming conventions.
266+
fn check_copy<T: Copy>() {}
267+
check_copy::<$thing>();
268+
269+
self.0
270+
}
271+
272+
/// Copies the underlying bytes into a new `Vec`.
273+
#[inline]
274+
pub fn to_vec(self) -> alloc::vec::Vec<u8> { self.0.to_vec() }
275+
276+
/// Returns a slice of the underlying bytes.
277+
#[inline]
278+
pub fn as_bytes(&self) -> &[u8] { &self.0 }
279+
280+
/// Converts the object to a raw pointer.
281+
#[inline]
282+
pub fn as_ptr(&self) -> *const $ty {
283+
let &$thing(ref dat) = self;
284+
dat.as_ptr()
285+
}
286+
287+
/// Converts the object to a mutable raw pointer.
288+
#[inline]
289+
pub fn as_mut_ptr(&mut self) -> *mut $ty {
290+
let &mut $thing(ref mut dat) = self;
291+
dat.as_mut_ptr()
292+
}
293+
294+
/// Returns the length of the object as an array.
295+
#[inline]
296+
pub fn len(&self) -> usize { $len }
297+
298+
/// Returns whether the object, as an array, is empty. Always false.
299+
#[inline]
300+
pub fn is_empty(&self) -> bool { false }
301+
}
302+
303+
impl<'a> core::convert::From<[$ty; $len]> for $thing {
304+
fn from(data: [$ty; $len]) -> Self { $thing(data) }
305+
}
306+
307+
impl<'a> core::convert::From<&'a [$ty; $len]> for $thing {
308+
fn from(data: &'a [$ty; $len]) -> Self { $thing(*data) }
309+
}
310+
311+
impl<'a> core::convert::TryFrom<&'a [$ty]> for $thing {
312+
type Error = core::array::TryFromSliceError;
313+
314+
fn try_from(data: &'a [$ty]) -> core::result::Result<Self, Self::Error> {
315+
use core::convert::TryInto;
316+
317+
Ok($thing(data.try_into()?))
318+
}
319+
}
320+
321+
impl AsRef<[$ty; $len]> for $thing {
322+
fn as_ref(&self) -> &[$ty; $len] { &self.0 }
323+
}
324+
325+
impl AsMut<[$ty; $len]> for $thing {
326+
fn as_mut(&mut self) -> &mut [$ty; $len] { &mut self.0 }
327+
}
328+
329+
impl AsRef<[$ty]> for $thing {
330+
fn as_ref(&self) -> &[$ty] { &self.0 }
331+
}
332+
333+
impl AsMut<[$ty]> for $thing {
334+
fn as_mut(&mut self) -> &mut [$ty] { &mut self.0 }
335+
}
336+
337+
impl core::borrow::Borrow<[$ty; $len]> for $thing {
338+
fn borrow(&self) -> &[$ty; $len] { &self.0 }
339+
}
340+
341+
impl core::borrow::BorrowMut<[$ty; $len]> for $thing {
342+
fn borrow_mut(&mut self) -> &mut [$ty; $len] { &mut self.0 }
343+
}
344+
345+
// The following two are valid because `[T; N]: Borrow<[T]>`
346+
impl core::borrow::Borrow<[$ty]> for $thing {
347+
fn borrow(&self) -> &[$ty] { &self.0 }
348+
}
349+
350+
impl core::borrow::BorrowMut<[$ty]> for $thing {
351+
fn borrow_mut(&mut self) -> &mut [$ty] { &mut self.0 }
352+
}
353+
354+
impl<I> core::ops::Index<I> for $thing
355+
where
356+
[$ty]: core::ops::Index<I>,
357+
{
358+
type Output = <[$ty] as core::ops::Index<I>>::Output;
359+
360+
#[inline]
361+
fn index(&self, index: I) -> &Self::Output { &self.0[index] }
362+
}
363+
};
364+
}

0 commit comments

Comments
 (0)