From 63e36fe6b4130c555bbb08eaea3b401c98c44d8e Mon Sep 17 00:00:00 2001 From: Tobin Harding Date: Fri, 21 Jan 2022 12:50:01 +1100 Subject: [PATCH] Remove impl_index_newtype macro This macro is no longer needed since we bumped MSRV to 1.29. We can implement `core::ops::Index` directly since all the inner types implement `Index` already. --- src/blockdata/script.rs | 29 +++++++++++++++++++--- src/internal_macros.rs | 54 +++++------------------------------------ src/util/bip32.rs | 14 ++++++++++- 3 files changed, 44 insertions(+), 53 deletions(-) diff --git a/src/blockdata/script.rs b/src/blockdata/script.rs index c190f6efbb..87c1461831 100644 --- a/src/blockdata/script.rs +++ b/src/blockdata/script.rs @@ -27,6 +27,7 @@ use prelude::*; use io; use core::{fmt, default::Default}; +use core::ops::Index; #[cfg(feature = "serde")] use serde; @@ -49,6 +50,18 @@ use schnorr::{TapTweak, TweakedPublicKey, UntweakedPublicKey}; #[derive(Clone, Default, PartialOrd, Ord, PartialEq, Eq, Hash)] pub struct Script(Box<[u8]>); +impl Index for Script +where + [u8]: Index, +{ + type Output = <[u8] as Index>::Output; + + #[inline] + fn index(&self, index: I) -> &Self::Output { + &self.0[index] + } +} + impl AsRef<[u8]> for Script { fn as_ref(&self) -> &[u8] { &self.0 @@ -109,6 +122,18 @@ impl ::core::str::FromStr for Script { pub struct Builder(Vec, Option); display_from_debug!(Builder); +impl Index for Builder +where + Vec: Index, +{ + type Output = as Index>::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. @@ -686,8 +711,6 @@ impl From> for Script { fn from(v: Vec) -> 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> { @@ -943,8 +966,6 @@ impl From> for Builder { } } -impl_index_newtype!(Builder, u8); - #[cfg(feature = "serde")] #[cfg_attr(docsrs, doc(cfg(feature = "serde")))] impl<'de> serde::Deserialize<'de> for Script { diff --git a/src/internal_macros.rs b/src/internal_macros.rs index daf78ab3e0..a3913b7921 100644 --- a/src/internal_macros.rs +++ b/src/internal_macros.rs @@ -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 for $thing { - type Output = $ty; - - #[inline] - fn index(&self, index: usize) -> &$ty { - &self.0[index] - } - } - - impl ::core::ops::Index<::core::ops::Range> for $thing { - type Output = [$ty]; - - #[inline] - fn index(&self, index: ::core::ops::Range) -> &[$ty] { - &self.0[index] - } - } - - impl ::core::ops::Index<::core::ops::RangeTo> for $thing { - type Output = [$ty]; - - #[inline] - fn index(&self, index: ::core::ops::RangeTo) -> &[$ty] { - &self.0[index] - } - } - - impl ::core::ops::Index<::core::ops::RangeFrom> for $thing { - type Output = [$ty]; + impl $crate::core::ops::Index for $thing + where + [$ty]: $crate::core::ops::Index, + { + type Output = <[$ty] as $crate::core::ops::Index>::Output; #[inline] - fn index(&self, index: ::core::ops::RangeFrom) -> &[$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[..] - } - } - } } diff --git a/src/util/bip32.rs b/src/util/bip32.rs index 6008c3483f..cc4cb694e1 100644 --- a/src/util/bip32.rs +++ b/src/util/bip32.rs @@ -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; @@ -238,9 +239,20 @@ pub trait IntoDerivationPath { /// A BIP-32 derivation path. #[derive(Clone, PartialEq, Eq, Ord, PartialOrd, Hash)] pub struct DerivationPath(Vec); -impl_index_newtype!(DerivationPath, ChildNumber); serde_string_impl!(DerivationPath, "a BIP-32 derivation path"); +impl Index for DerivationPath +where + Vec: Index, +{ + type Output = as Index>::Output; + + #[inline] + fn index(&self, index: I) -> &Self::Output { + &self.0[index] + } +} + impl Default for DerivationPath { fn default() -> DerivationPath { DerivationPath::master()