diff --git a/src/blockdata/script.rs b/src/blockdata/script.rs index ec071c3f21..d5659e9178 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 @@ -108,6 +121,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. @@ -685,8 +710,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> { @@ -942,8 +965,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 53ce2d60a5..3078f8c48f 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 2876c97ebb..65a973f5f3 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()