use core::array::TryFromSliceError;
use derive_more::From;
use scale::{
Decode,
Encode,
MaxEncodedLen,
};
#[cfg(feature = "std")]
use {
scale_decode::DecodeAsType,
scale_encode::EncodeAsType,
scale_info::TypeInfo,
};
#[derive(
Debug,
Copy,
Clone,
PartialEq,
Eq,
Ord,
PartialOrd,
Hash,
Decode,
Encode,
MaxEncodedLen,
From,
)]
#[cfg_attr(feature = "std", derive(TypeInfo, DecodeAsType, EncodeAsType))]
pub struct AccountId(pub [u8; 32]);
impl AsRef<[u8; 32]> for AccountId {
#[inline]
fn as_ref(&self) -> &[u8; 32] {
&self.0
}
}
impl AsMut<[u8; 32]> for AccountId {
#[inline]
fn as_mut(&mut self) -> &mut [u8; 32] {
&mut self.0
}
}
impl AsRef<[u8]> for AccountId {
#[inline]
fn as_ref(&self) -> &[u8] {
&self.0[..]
}
}
impl AsMut<[u8]> for AccountId {
#[inline]
fn as_mut(&mut self) -> &mut [u8] {
&mut self.0[..]
}
}
impl<'a> TryFrom<&'a [u8]> for AccountId {
type Error = TryFromSliceError;
fn try_from(bytes: &'a [u8]) -> Result<Self, TryFromSliceError> {
let address = <[u8; 32]>::try_from(bytes)?;
Ok(Self(address))
}
}
#[derive(
Debug,
Copy,
Clone,
PartialEq,
Eq,
Ord,
PartialOrd,
Hash,
Decode,
Encode,
MaxEncodedLen,
From,
Default,
)]
#[cfg_attr(feature = "std", derive(TypeInfo, DecodeAsType, EncodeAsType))]
pub struct Hash([u8; 32]);
impl<'a> TryFrom<&'a [u8]> for Hash {
type Error = TryFromSliceError;
fn try_from(bytes: &'a [u8]) -> Result<Self, TryFromSliceError> {
let hash = <[u8; 32]>::try_from(bytes)?;
Ok(Self(hash))
}
}
impl AsRef<[u8]> for Hash {
fn as_ref(&self) -> &[u8] {
&self.0[..]
}
}
impl AsMut<[u8]> for Hash {
fn as_mut(&mut self) -> &mut [u8] {
&mut self.0[..]
}
}
impl From<Hash> for [u8; 32] {
fn from(hash: Hash) -> Self {
hash.0
}
}
pub trait Clear {
const CLEAR_HASH: Self;
fn is_clear(&self) -> bool;
}
impl Clear for [u8; 32] {
const CLEAR_HASH: Self = [0x00; 32];
fn is_clear(&self) -> bool {
self == &Self::CLEAR_HASH
}
}
impl Clear for Hash {
const CLEAR_HASH: Self = Self(<[u8; 32] as Clear>::CLEAR_HASH);
fn is_clear(&self) -> bool {
<[u8; 32] as Clear>::is_clear(&self.0)
}
}