Merge rust-bitcoin/rust-bitcoin#805: Remove impl_index_newtype macro

63e36fe6b4 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](https://github.com/rust-bitcoin/rust-bitcoin/issues/352#issuecomment-560331856).~

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

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

Tree-SHA512: f7b4555c7fd9a2d458dcd53ec8caece0d12f3af77a10e850f35201bd7a580ba8fd7cb1d47a7f78ba6582e777dffa13416916ecacac6e0e874bdbb1c866132dc2
This commit is contained in:
sanket1729 2022-03-23 19:00:50 -07:00
commit ea80e6568a
No known key found for this signature in database
GPG Key ID: 648FFB183E0870A2
3 changed files with 44 additions and 53 deletions

View File

@ -27,6 +27,7 @@ use prelude::*;
use io; use io;
use core::{fmt, default::Default}; use core::{fmt, default::Default};
use core::ops::Index;
#[cfg(feature = "serde")] use serde; #[cfg(feature = "serde")] use serde;
@ -49,6 +50,18 @@ use schnorr::{TapTweak, TweakedPublicKey, UntweakedPublicKey};
#[derive(Clone, Default, PartialOrd, Ord, PartialEq, Eq, Hash)] #[derive(Clone, Default, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub struct Script(Box<[u8]>); 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 { impl AsRef<[u8]> for Script {
fn as_ref(&self) -> &[u8] { fn as_ref(&self) -> &[u8] {
&self.0 &self.0
@ -108,6 +121,18 @@ impl ::core::str::FromStr for Script {
pub struct Builder(Vec<u8>, Option<opcodes::All>); pub struct Builder(Vec<u8>, Option<opcodes::All>);
display_from_debug!(Builder); 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 /// Ways that a script might fail. Not everything is split up as
/// much as it could be; patches welcome if more detailed errors /// much as it could be; patches welcome if more detailed errors
/// would help you. /// would help you.
@ -685,8 +710,6 @@ impl From<Vec<u8>> for Script {
fn from(v: Vec<u8>) -> Script { Script(v.into_boxed_slice()) } 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. /// A "parsed opcode" which allows iterating over a [`Script`] in a more sensible way.
#[derive(Debug, PartialEq, Eq, Clone)] #[derive(Debug, PartialEq, Eq, Clone)]
pub enum Instruction<'a> { pub enum Instruction<'a> {
@ -942,8 +965,6 @@ impl From<Vec<u8>> for Builder {
} }
} }
impl_index_newtype!(Builder, u8);
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))] #[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl<'de> serde::Deserialize<'de> for Script { impl<'de> serde::Deserialize<'de> for Script {

View File

@ -93,59 +93,17 @@ macro_rules! impl_array_newtype {
} }
} }
impl_index_newtype!($thing, $ty); impl<I> $crate::core::ops::Index<I> for $thing
} where
} [$ty]: $crate::core::ops::Index<I>,
{
/// Implements standard indexing methods for a given wrapper type type Output = <[$ty] as $crate::core::ops::Index<I>>::Output;
macro_rules! impl_index_newtype {
($thing:ident, $ty:ty) => {
impl ::core::ops::Index<usize> for $thing {
type Output = $ty;
#[inline] #[inline]
fn index(&self, index: usize) -> &$ty { fn index(&self, index: I) -> &Self::Output {
&self.0[index] &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];
#[inline]
fn index(&self, index: ::core::ops::RangeFrom<usize>) -> &[$ty] {
&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[..]
}
}
} }
} }

View File

@ -21,6 +21,7 @@ use prelude::*;
use io::Write; use io::Write;
use core::{fmt, str::FromStr, default::Default}; use core::{fmt, str::FromStr, default::Default};
use core::ops::Index;
#[cfg(feature = "std")] use std::error; #[cfg(feature = "std")] use std::error;
#[cfg(feature = "serde")] use serde; #[cfg(feature = "serde")] use serde;
@ -238,9 +239,20 @@ pub trait IntoDerivationPath {
/// A BIP-32 derivation path. /// A BIP-32 derivation path.
#[derive(Clone, PartialEq, Eq, Ord, PartialOrd, Hash)] #[derive(Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
pub struct DerivationPath(Vec<ChildNumber>); pub struct DerivationPath(Vec<ChildNumber>);
impl_index_newtype!(DerivationPath, ChildNumber);
serde_string_impl!(DerivationPath, "a BIP-32 derivation path"); 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 { impl Default for DerivationPath {
fn default() -> DerivationPath { fn default() -> DerivationPath {
DerivationPath::master() DerivationPath::master()