macro_rules! impl_array_newtype {
($thing:ident, $ty:ty, $len:expr) => {
impl $thing {
#[inline]
pub fn as_ptr(&self) -> *const $ty {
let &$thing(ref dat) = self;
dat.as_ptr()
}
#[inline]
pub fn as_mut_ptr(&mut self) -> *mut $ty {
let &mut $thing(ref mut dat) = self;
dat.as_mut_ptr()
}
#[inline]
pub fn len(&self) -> usize {
$len
}
#[inline]
pub fn is_empty(&self) -> bool {
false
}
#[inline]
pub fn as_bytes(&self) -> &[$ty; $len] {
&self.0
}
#[inline]
pub fn to_bytes(&self) -> [$ty; $len] {
self.0.clone()
}
#[inline]
pub fn into_bytes(self) -> [$ty; $len] {
self.0
}
}
impl<'a> ::core::convert::From<&'a [$ty]> for $thing {
fn from(data: &'a [$ty]) -> $thing {
assert_eq!(data.len(), $len);
let mut ret = [0; $len];
ret.copy_from_slice(&data[..]);
$thing(ret)
}
}
impl ::core::ops::Index<usize> for $thing {
type Output = $ty;
#[inline]
fn index(&self, index: usize) -> &$ty {
let &$thing(ref dat) = self;
&dat[index]
}
}
impl_index_newtype!($thing, $ty);
impl ::core::cmp::PartialEq for $thing {
#[inline]
fn eq(&self, other: &$thing) -> bool {
&self[..] == &other[..]
}
}
impl ::core::cmp::Eq for $thing {}
impl ::core::cmp::PartialOrd for $thing {
#[inline]
fn partial_cmp(&self, other: &$thing) -> Option<::core::cmp::Ordering> {
Some(self.cmp(&other))
}
}
impl ::core::cmp::Ord for $thing {
#[inline]
fn cmp(&self, other: &$thing) -> ::core::cmp::Ordering {
for i in 0..$len {
if self[$len - 1 - i] < other[$len - 1 - i] {
return ::core::cmp::Ordering::Less;
}
if self[$len - 1 - i] > other[$len - 1 - i] {
return ::core::cmp::Ordering::Greater;
}
}
::core::cmp::Ordering::Equal
}
}
#[cfg_attr(feature = "clippy", allow(expl_impl_clone_on_copy))] impl ::core::clone::Clone for $thing {
#[inline]
fn clone(&self) -> $thing {
$thing::from(&self[..])
}
}
impl ::core::marker::Copy for $thing {}
impl ::core::hash::Hash for $thing {
#[inline]
fn hash<H>(&self, state: &mut H)
where
H: ::core::hash::Hasher,
{
(&self[..]).hash(state);
}
fn hash_slice<H>(data: &[$thing], state: &mut H)
where
H: ::core::hash::Hasher,
{
for d in data.iter() {
(&d[..]).hash(state);
}
}
}
};
}
macro_rules! impl_index_newtype {
($thing:ident, $ty:ty) => {
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[..]
}
}
};
}
#[macro_export]
macro_rules! impl_bytes_newtype {
($t:ident, $len:expr) => (
impl ::core::fmt::LowerHex for $t {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
for &ch in self.0.iter() {
write!(f, "{:02x}", ch)?;
}
Ok(())
}
}
impl ::core::fmt::Display for $t {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
fmt::LowerHex::fmt(self, f)
}
}
impl $crate::hex::FromHex for $t {
fn from_byte_iter<I>(iter: I) -> Result<Self, $crate::hex::Error>
where I: ::core::iter::Iterator<Item=Result<u8, $crate::hex::Error>> +
::core::iter::ExactSizeIterator +
::core::iter::DoubleEndedIterator,
{
if iter.len() == $len {
let mut ret = [0; $len];
for (n, byte) in iter.enumerate() {
ret[n] = byte?;
}
Ok($t(ret))
} else {
Err($crate::hex::Error::InvalidLength(2 * $len, 2 * iter.len()))
}
}
}
impl ::core::str::FromStr for $t {
type Err = $crate:::hex::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
$crate::hex::FromHex::from_hex(s)
}
}
#[cfg(feature="serde")]
impl $crate::serde::Serialize for $t {
fn serialize<S: $crate::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
if s.is_human_readable() {
s.serialize_str(&$crate::hex::ToHex::to_hex(self))
} else {
s.serialize_bytes(&self[..])
}
}
}
#[cfg(feature="serde")]
impl<'de> $crate::serde::Deserialize<'de> for $t {
fn deserialize<D: $crate::serde::Deserializer<'de>>(d: D) -> Result<$t, D::Error> {
if d.is_human_readable() {
struct HexVisitor;
impl<'de> $crate::serde::de::Visitor<'de> for HexVisitor {
type Value = $t;
fn expecting(&self, formatter: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
formatter.write_str("an ASCII hex string")
}
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
where
E: $crate::serde::de::Error,
{
if let Ok(hex) = ::core::str::from_utf8(v) {
$crate::hex::FromHex::from_hex(hex).map_err(E::custom)
} else {
return Err(E::invalid_value($crate::serde::de::Unexpected::Bytes(v), &self));
}
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: $crate::serde::de::Error,
{
$crate::hex::FromHex::from_hex(v).map_err(E::custom)
}
}
d.deserialize_str(HexVisitor)
} else {
struct BytesVisitor;
impl<'de> $crate::serde::de::Visitor<'de> for BytesVisitor {
type Value = $t;
fn expecting(&self, formatter: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
formatter.write_str("a bytestring")
}
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
where
E: $crate::serde::de::Error,
{
if v.len() != $len {
Err(E::invalid_length(v.len(), &stringify!($len)))
} else {
let mut ret = [0; $len];
ret.copy_from_slice(v);
Ok($t(ret))
}
}
}
d.deserialize_bytes(BytesVisitor)
}
}
}
)
}