Skip to content

Commit

Permalink
Merge #805: Remove impl_index_newtype macro
Browse files Browse the repository at this point in the history
63e36fe Remove impl_index_newtype macro (Tobin Harding)

Pull request description:

  This macro is no longer needed since we bumped MSRV to 1.29.

  ~We can implement `SliceIndex` to get the `Index` implementations.~
  We can implement `core::ops::Index` directly since all the inner types implement `Index` already.

  Original ~Idea shamelessly stolen from @elichai [in this comment](#352 (comment)

  New idea proposed by @Kixunil during review below. Thanks.

ACKs for top commit:
  apoelstra:
    ACK 63e36fe
  dr-orlovsky:
    utACK 63e36fe
  sanket1729:
    ACK 63e36fe

Tree-SHA512: f7b4555c7fd9a2d458dcd53ec8caece0d12f3af77a10e850f35201bd7a580ba8fd7cb1d47a7f78ba6582e777dffa13416916ecacac6e0e874bdbb1c866132dc2
  • Loading branch information
sanket1729 committed Mar 24, 2022
2 parents db23006 + 63e36fe commit ea80e65
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 53 deletions.
29 changes: 25 additions & 4 deletions src/blockdata/script.rs
Expand Up @@ -27,6 +27,7 @@ use prelude::*;

use io;
use core::{fmt, default::Default};
use core::ops::Index;

#[cfg(feature = "serde")] use serde;

Expand All @@ -49,6 +50,18 @@ use schnorr::{TapTweak, TweakedPublicKey, UntweakedPublicKey};
#[derive(Clone, Default, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub struct Script(Box<[u8]>);

impl<I> Index<I> for Script
where
[u8]: Index<I>,
{
type Output = <[u8] as Index<I>>::Output;

#[inline]
fn index(&self, index: I) -> &Self::Output {
&self.0[index]
}
}

impl AsRef<[u8]> for Script {
fn as_ref(&self) -> &[u8] {
&self.0
Expand Down Expand Up @@ -108,6 +121,18 @@ impl ::core::str::FromStr for Script {
pub struct Builder(Vec<u8>, Option<opcodes::All>);
display_from_debug!(Builder);

impl<I> Index<I> for Builder
where
Vec<u8>: Index<I>,
{
type Output = <Vec<u8> as Index<I>>::Output;

#[inline]
fn index(&self, index: I) -> &Self::Output {
&self.0[index]
}
}

/// Ways that a script might fail. Not everything is split up as
/// much as it could be; patches welcome if more detailed errors
/// would help you.
Expand Down Expand Up @@ -685,8 +710,6 @@ impl From<Vec<u8>> for Script {
fn from(v: Vec<u8>) -> Script { Script(v.into_boxed_slice()) }
}

impl_index_newtype!(Script, u8);

/// A "parsed opcode" which allows iterating over a [`Script`] in a more sensible way.
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Instruction<'a> {
Expand Down Expand Up @@ -942,8 +965,6 @@ impl From<Vec<u8>> for Builder {
}
}

impl_index_newtype!(Builder, u8);

#[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl<'de> serde::Deserialize<'de> for Script {
Expand Down
54 changes: 6 additions & 48 deletions src/internal_macros.rs
Expand Up @@ -93,59 +93,17 @@ macro_rules! impl_array_newtype {
}
}

impl_index_newtype!($thing, $ty);
}
}

/// Implements standard indexing methods for a given wrapper type
macro_rules! impl_index_newtype {
($thing:ident, $ty:ty) => {

impl ::core::ops::Index<usize> for $thing {
type Output = $ty;

#[inline]
fn index(&self, index: usize) -> &$ty {
&self.0[index]
}
}

impl ::core::ops::Index<::core::ops::Range<usize>> for $thing {
type Output = [$ty];

#[inline]
fn index(&self, index: ::core::ops::Range<usize>) -> &[$ty] {
&self.0[index]
}
}

impl ::core::ops::Index<::core::ops::RangeTo<usize>> for $thing {
type Output = [$ty];

#[inline]
fn index(&self, index: ::core::ops::RangeTo<usize>) -> &[$ty] {
&self.0[index]
}
}

impl ::core::ops::Index<::core::ops::RangeFrom<usize>> for $thing {
type Output = [$ty];
impl<I> $crate::core::ops::Index<I> for $thing
where
[$ty]: $crate::core::ops::Index<I>,
{
type Output = <[$ty] as $crate::core::ops::Index<I>>::Output;

#[inline]
fn index(&self, index: ::core::ops::RangeFrom<usize>) -> &[$ty] {
fn index(&self, index: I) -> &Self::Output {
&self.0[index]
}
}

impl ::core::ops::Index<::core::ops::RangeFull> for $thing {
type Output = [$ty];

#[inline]
fn index(&self, _: ::core::ops::RangeFull) -> &[$ty] {
&self.0[..]
}
}

}
}

Expand Down
14 changes: 13 additions & 1 deletion src/util/bip32.rs
Expand Up @@ -21,6 +21,7 @@ use prelude::*;

use io::Write;
use core::{fmt, str::FromStr, default::Default};
use core::ops::Index;
#[cfg(feature = "std")] use std::error;
#[cfg(feature = "serde")] use serde;

Expand Down Expand Up @@ -238,9 +239,20 @@ pub trait IntoDerivationPath {
/// A BIP-32 derivation path.
#[derive(Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
pub struct DerivationPath(Vec<ChildNumber>);
impl_index_newtype!(DerivationPath, ChildNumber);
serde_string_impl!(DerivationPath, "a BIP-32 derivation path");

impl<I> Index<I> for DerivationPath
where
Vec<ChildNumber>: Index<I>,
{
type Output = <Vec<ChildNumber> as Index<I>>::Output;

#[inline]
fn index(&self, index: I) -> &Self::Output {
&self.0[index]
}
}

impl Default for DerivationPath {
fn default() -> DerivationPath {
DerivationPath::master()
Expand Down

0 comments on commit ea80e65

Please sign in to comment.