use super::arithmetic::AtLeast32BitUnsigned;
use core::array::TryFromSliceError;
use derive_more::From;
use scale::{
Decode,
Encode,
};
#[cfg(feature = "std")]
use scale_info::TypeInfo;
pub trait FromLittleEndian {
type Bytes: Default + AsRef<[u8]> + AsMut<[u8]>;
fn from_le_bytes(bytes: Self::Bytes) -> Self;
}
impl FromLittleEndian for u8 {
type Bytes = [u8; 1];
#[inline]
fn from_le_bytes(bytes: Self::Bytes) -> Self {
u8::from_le_bytes(bytes)
}
}
impl FromLittleEndian for u16 {
type Bytes = [u8; 2];
#[inline]
fn from_le_bytes(bytes: Self::Bytes) -> Self {
u16::from_le_bytes(bytes)
}
}
impl FromLittleEndian for u32 {
type Bytes = [u8; 4];
#[inline]
fn from_le_bytes(bytes: Self::Bytes) -> Self {
u32::from_le_bytes(bytes)
}
}
impl FromLittleEndian for u64 {
type Bytes = [u8; 8];
#[inline]
fn from_le_bytes(bytes: Self::Bytes) -> Self {
u64::from_le_bytes(bytes)
}
}
impl FromLittleEndian for u128 {
type Bytes = [u8; 16];
#[inline]
fn from_le_bytes(bytes: Self::Bytes) -> Self {
u128::from_le_bytes(bytes)
}
}
pub trait Environment {
const MAX_EVENT_TOPICS: usize;
type AccountId: 'static
+ scale::Codec
+ Clone
+ PartialEq
+ Eq
+ Ord
+ AsRef<[u8]>
+ AsMut<[u8]>
+ Default;
type Balance: 'static
+ scale::Codec
+ Copy
+ Clone
+ PartialEq
+ Eq
+ AtLeast32BitUnsigned
+ FromLittleEndian;
type Hash: 'static
+ scale::Codec
+ Copy
+ Clone
+ Clear
+ PartialEq
+ Eq
+ Ord
+ AsRef<[u8]>
+ AsMut<[u8]>;
type Timestamp: 'static
+ scale::Codec
+ Copy
+ Clone
+ PartialEq
+ Eq
+ AtLeast32BitUnsigned
+ FromLittleEndian;
type BlockNumber: 'static
+ scale::Codec
+ Copy
+ Clone
+ PartialEq
+ Eq
+ AtLeast32BitUnsigned
+ FromLittleEndian;
type ChainExtension;
}
pub enum NoChainExtension {}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(TypeInfo))]
pub enum DefaultEnvironment {}
impl Environment for DefaultEnvironment {
const MAX_EVENT_TOPICS: usize = 4;
type AccountId = AccountId;
type Balance = Balance;
type Hash = Hash;
type Timestamp = Timestamp;
type BlockNumber = BlockNumber;
type ChainExtension = NoChainExtension;
}
pub type Balance = u128;
pub type Timestamp = u64;
pub type Gas = u64;
pub type BlockNumber = u32;
#[derive(
Debug,
Copy,
Clone,
PartialEq,
Eq,
Ord,
PartialOrd,
Hash,
Encode,
Decode,
From,
Default,
)]
#[cfg_attr(feature = "std", derive(TypeInfo))]
pub struct AccountId([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,
Encode,
Decode,
From,
Default,
)]
#[cfg_attr(feature = "std", derive(TypeInfo))]
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 address = <[u8; 32]>::try_from(bytes)?;
Ok(Self(address))
}
}
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[..]
}
}
pub trait Clear {
fn is_clear(&self) -> bool;
fn clear() -> Self;
}
impl Clear for [u8; 32] {
fn is_clear(&self) -> bool {
self.as_ref().iter().all(|&byte| byte == 0x00)
}
fn clear() -> Self {
[0x00; 32]
}
}
impl Clear for Hash {
fn is_clear(&self) -> bool {
<[u8; 32] as Clear>::is_clear(&self.0)
}
fn clear() -> Self {
Self(<[u8; 32] as Clear>::clear())
}
}