Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: make more functions be const #1574

Merged
merged 5 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/e2e/src/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn constructor_exec_input<E: Environment, Args: Encode, R>(
// set all the other properties to default values, we only require the `exec_input`.
builder
.endowment(0u32.into())
.code_hash(ink_primitives::Clear::clear())
.code_hash(ink_primitives::Clear::CLEAR_HASH)
.salt_bytes(Vec::new())
.params()
.exec_input()
Expand Down
12 changes: 6 additions & 6 deletions crates/env/src/call/call_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ where
/// # type AccountId = <DefaultEnvironment as Environment>::AccountId;
/// let my_return_value: i32 = build_call::<DefaultEnvironment>()
/// .call_type(DelegateCall::new()
/// .code_hash(<DefaultEnvironment as Environment>::Hash::clear()))
/// .code_hash(<DefaultEnvironment as Environment>::Hash::CLEAR_HASH))
/// .exec_input(
/// ExecutionInput::new(Selector::new([0xDE, 0xAD, 0xBE, 0xEF]))
/// .push_arg(42u8)
Expand Down Expand Up @@ -322,16 +322,16 @@ pub struct DelegateCall<E: Environment> {

impl<E: Environment> DelegateCall<E> {
/// Returns a clean builder for [`DelegateCall`]
pub fn new() -> Self {
Default::default()
pub const fn new() -> Self {
DelegateCall {
code_hash: E::Hash::CLEAR_HASH,
}
}
}

impl<E: Environment> Default for DelegateCall<E> {
fn default() -> Self {
DelegateCall {
code_hash: E::Hash::clear(),
}
Self::new()
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/env/src/engine/off_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ where
{
let encoded = topic_value.encode();
let len_encoded = encoded.len();
let mut result = <E as Environment>::Hash::clear();
let mut result = <E as Environment>::Hash::CLEAR_HASH;
let len_result = result.as_ref().len();
if len_encoded <= len_result {
result.as_mut()[..len_encoded].copy_from_slice(&encoded[..]);
Expand Down
2 changes: 1 addition & 1 deletion crates/env/src/engine/on_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ where
{
fn inner<E: Environment>(encoded: &mut [u8]) -> <E as Environment>::Hash {
let len_encoded = encoded.len();
let mut result = <E as Environment>::Hash::clear();
let mut result = <E as Environment>::Hash::CLEAR_HASH;
let len_result = result.as_ref().len();
if len_encoded <= len_result {
result.as_mut()[..len_encoded].copy_from_slice(encoded);
Expand Down
2 changes: 1 addition & 1 deletion crates/ink/src/env_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ where
/// let call_params = build_call::<DefaultEnvironment>()
/// .call_type(
/// DelegateCall::new()
/// .code_hash(<DefaultEnvironment as ink::env::Environment>::Hash::clear()))
/// .code_hash(<DefaultEnvironment as ink::env::Environment>::Hash::CLEAR_HASH))
/// .exec_input(
/// ExecutionInput::new(Selector::new([0xCA, 0xFE, 0xBA, 0xBE]))
/// .push_arg(42u8)
Expand Down
20 changes: 8 additions & 12 deletions crates/primitives/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,29 +129,25 @@ impl AsMut<[u8]> for Hash {
///
/// A hash that consists only of 0 bits is clear.
pub trait Clear {
/// The clear hash.
const CLEAR_HASH: Self;

/// Returns `true` if the hash is clear.
fn is_clear(&self) -> bool;

/// Returns a clear hash.
fn clear() -> Self;
}

impl Clear for [u8; 32] {
fn is_clear(&self) -> bool {
self.as_ref().iter().all(|&byte| byte == 0x00)
}
const CLEAR_HASH: Self = [0x00; 32];

fn clear() -> 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)
}

fn clear() -> Self {
Self(<[u8; 32] as Clear>::clear())
}
}
8 changes: 3 additions & 5 deletions crates/storage/src/lazy/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ where
KeyType: StorageKey,
{
fn default() -> Self {
Self {
_marker: Default::default(),
}
Self::new()
}
}

Expand All @@ -105,9 +103,9 @@ where
KeyType: StorageKey,
{
/// Creates a new empty `Mapping`.
pub fn new() -> Self {
pub const fn new() -> Self {
Self {
_marker: Default::default(),
_marker: PhantomData,
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions crates/storage/src/lazy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ where
KeyType: StorageKey,
{
fn default() -> Self {
Self {
_marker: Default::default(),
}
Self::new()
}
}

Expand All @@ -111,9 +109,9 @@ where
KeyType: StorageKey,
{
/// Creates a new empty `Lazy`.
pub fn new() -> Self {
pub const fn new() -> Self {
Self {
_marker: Default::default(),
_marker: PhantomData,
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions examples/erc20/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,10 @@ mod erc20 {
mod tests {
use super::*;

use ink::primitives::Clear;
use ink::primitives::{
Clear,
Hash,
};

type Event = <Erc20 as ::ink::reflect::ContractEventBase>::Type;

Expand Down Expand Up @@ -259,7 +262,7 @@ mod erc20 {
for (n, (actual_topic, expected_topic)) in
topics.iter().zip(expected_topics).enumerate()
{
let mut topic_hash = Hash::clear();
let mut topic_hash = Hash::CLEAR_HASH;
let len = actual_topic.len();
topic_hash.as_mut()[0..len].copy_from_slice(&actual_topic[0..len]);

Expand Down Expand Up @@ -513,7 +516,7 @@ mod erc20 {
primitives::Clear,
};

let mut result = Hash::clear();
let mut result = Hash::CLEAR_HASH;
let len_result = result.as_ref().len();
let encoded = entity.encode();
let len_encoded = encoded.len();
Expand Down
2 changes: 1 addition & 1 deletion examples/trait-erc20/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ mod erc20 {
where
T: scale::Encode,
{
let mut result = Hash::clear();
let mut result = Hash::CLEAR_HASH;
let len_result = result.as_ref().len();
let encoded = entity.encode();
let len_encoded = encoded.len();
Expand Down