From 9aa7f1a8c48dbe0aab6b6b792bbb2fb4b0ef835b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20P=C5=82askonka?= Date: Fri, 24 Nov 2023 11:54:09 +0100 Subject: [PATCH] Fight for Prelude (#267) * Removed unneeded alloc:: and std:: uses, replacing them with odra::prelude. Removed if std from odra_core::prelude * Updated prelude and it's usages to `use odra::prelude::*` --- core/src/address.rs | 10 +---- core/src/call_def.rs | 2 +- core/src/call_result.rs | 5 +-- core/src/contract_def.rs | 4 +- core/src/error.rs | 6 +-- core/src/event.rs | 2 +- core/src/host_env.rs | 1 - core/src/item.rs | 4 +- core/src/mapping.rs | 2 +- core/src/module.rs | 4 +- core/src/native_token.rs | 2 +- core/src/prelude.rs | 40 ++++--------------- core/src/utils.rs | 2 +- examples2/src/counter_pack.rs | 1 + examples2/src/erc20.rs | 3 +- justfile | 2 +- odra-casper/test-vm/src/casper_host.rs | 2 +- odra-casper/test-vm/src/vm/casper_vm.rs | 4 +- odra-casper/wasm-env/src/host_functions.rs | 6 +-- odra-casper/wasm-env/src/wasm_contract_env.rs | 4 +- odra-vm/src/odra_vm_host.rs | 2 +- odra-vm/src/vm/contract_container.rs | 2 +- odra-vm/src/vm/contract_register.rs | 2 +- 23 files changed, 36 insertions(+), 76 deletions(-) diff --git a/core/src/address.rs b/core/src/address.rs index f71cb1a2..0e784bac 100644 --- a/core/src/address.rs +++ b/core/src/address.rs @@ -1,13 +1,7 @@ //! Better address representation for Casper. - -use core::str::FromStr; - +use crate::prelude::*; use crate::AddressError::ZeroAddress; use crate::{AddressError, OdraError, VmError}; -use alloc::{ - string::{String, ToString}, - vec::Vec -}; use casper_types::{ account::AccountHash, bytesrepr::{self, FromBytes, ToBytes}, @@ -44,7 +38,6 @@ impl Address { // TODO: move those methods to odra_vm as they shouldn't be public pub fn account_from_str(str: &str) -> Self { - use alloc::format; use casper_types::account::{ACCOUNT_HASH_FORMATTED_STRING_PREFIX, ACCOUNT_HASH_LENGTH}; let desired_length = ACCOUNT_HASH_LENGTH * 2; let padding_length = desired_length - str.len(); @@ -56,7 +49,6 @@ impl Address { // TODO: move those methods to odra_vm as they shouldn't be public pub fn contract_from_u32(i: u32) -> Self { - use alloc::format; use casper_types::KEY_HASH_LENGTH; let desired_length = KEY_HASH_LENGTH * 2; let padding_length = desired_length - i.to_string().len(); diff --git a/core/src/call_def.rs b/core/src/call_def.rs index a24cd53d..017547ef 100644 --- a/core/src/call_def.rs +++ b/core/src/call_def.rs @@ -1,4 +1,4 @@ -use alloc::string::String; +use crate::prelude::*; use casper_types::bytesrepr::FromBytes; use casper_types::{CLTyped, RuntimeArgs, U512}; diff --git a/core/src/call_result.rs b/core/src/call_result.rs index 75dea2dc..84c3e931 100644 --- a/core/src/call_result.rs +++ b/core/src/call_result.rs @@ -1,7 +1,4 @@ -use crate::prelude::collections::BTreeMap; -use crate::prelude::string::{String, ToString}; -use crate::prelude::vec; -use crate::prelude::vec::Vec; +use crate::prelude::*; use crate::utils::extract_event_name; use crate::{Address, Bytes, OdraError, ToBytes}; use casper_event_standard::EventInstance; diff --git a/core/src/contract_def.rs b/core/src/contract_def.rs index 6546027e..aedc6af6 100644 --- a/core/src/contract_def.rs +++ b/core/src/contract_def.rs @@ -1,6 +1,6 @@ //! Encapsulates a set of structures that abstract out a smart contract layout. -use alloc::{string::String, vec::Vec}; +use crate::prelude::*; use casper_types::CLType; /// Contract's entrypoint. @@ -99,7 +99,7 @@ pub struct ContractBlueprint2 { #[allow(dead_code)] mod test { use super::Node; - use alloc::{string::String, vec, vec::Vec}; + use crate::prelude::{string::String, vec, vec::Vec}; use core::marker::PhantomData; #[test] diff --git a/core/src/error.rs b/core/src/error.rs index 7ddc4261..9732a4d2 100644 --- a/core/src/error.rs +++ b/core/src/error.rs @@ -1,8 +1,6 @@ -use core::any::Any; - -use alloc::{boxed::Box, string::String}; - use crate::arithmetic::ArithmeticsError; +use crate::prelude::*; +use core::any::Any; /// General error type in Odra framework #[repr(u16)] diff --git a/core/src/event.rs b/core/src/event.rs index 335f56aa..fb35d9de 100644 --- a/core/src/event.rs +++ b/core/src/event.rs @@ -1,6 +1,6 @@ //! Events interface and errors. -use crate::prelude::string::String; +use crate::prelude::*; /// Event-related errors. #[derive(Debug, PartialEq, Eq, PartialOrd)] diff --git a/core/src/host_env.rs b/core/src/host_env.rs index c358164c..34e6f179 100644 --- a/core/src/host_env.rs +++ b/core/src/host_env.rs @@ -2,7 +2,6 @@ use crate::call_result::CallResult; use crate::entry_point_callback::EntryPointsCaller; use crate::event::EventError; use crate::host_context::HostContext; -use crate::prelude::collections::BTreeMap; use crate::prelude::*; use crate::utils::extract_event_name; use crate::{Address, OdraError, VmError, U512}; diff --git a/core/src/item.rs b/core/src/item.rs index 7d3bed17..3ff73610 100644 --- a/core/src/item.rs +++ b/core/src/item.rs @@ -1,4 +1,4 @@ -use alloc::{collections::BTreeMap, string::String, vec::Vec}; +use crate::prelude::*; use casper_types::{ bytesrepr::{FromBytes, ToBytes}, U128, U256, U512 @@ -11,7 +11,7 @@ pub trait OdraItem { #[cfg(not(target_arch = "wasm32"))] fn events() -> Vec { - alloc::vec![] + vec![] } } diff --git a/core/src/mapping.rs b/core/src/mapping.rs index f8ac0e82..8977826c 100644 --- a/core/src/mapping.rs +++ b/core/src/mapping.rs @@ -1,6 +1,6 @@ +use crate::prelude::*; use crate::{ module::{Module, ModuleWrapper}, - prelude::*, variable::Variable, ContractEnv }; diff --git a/core/src/module.rs b/core/src/module.rs index 73e5ebae..b03e1c33 100644 --- a/core/src/module.rs +++ b/core/src/module.rs @@ -1,10 +1,10 @@ +use crate::prelude::*; use core::cell::OnceCell; -use core::ops::{Deref, DerefMut}; use crate::call_def::CallDef; use crate::contract_env::ContractEnv; use crate::odra_result::OdraResult; -use crate::prelude::*; +use core::ops::{Deref, DerefMut}; pub trait Callable { fn call(&self, env: ContractEnv, call_def: CallDef) -> OdraResult>; diff --git a/core/src/native_token.rs b/core/src/native_token.rs index fdc2b93d..f8358bf8 100644 --- a/core/src/native_token.rs +++ b/core/src/native_token.rs @@ -1,4 +1,4 @@ -use alloc::string::String; +use crate::prelude::string::String; pub struct NativeTokenMetadata { pub name: String, diff --git a/core/src/prelude.rs b/core/src/prelude.rs index 325a262b..22cae3ae 100644 --- a/core/src/prelude.rs +++ b/core/src/prelude.rs @@ -1,39 +1,15 @@ -#[cfg(feature = "std")] -pub use std::{borrow, boxed, format, string, vec}; - -#[cfg(feature = "std")] -pub use std::string::ToString; - -#[cfg(feature = "std")] -pub mod collections { - pub use self::{ - binary_heap::BinaryHeap, btree_map::BTreeMap, btree_set::BTreeSet, linked_list::LinkedList, - vec_deque::VecDeque, Bound - }; - pub use std::collections::*; -} - -#[cfg(feature = "std")] -pub use std::cell::RefCell; -#[cfg(feature = "std")] -pub use std::rc::Rc; - -#[cfg(not(feature = "std"))] #[allow(clippy::module_inception)] mod prelude { + pub use alloc::borrow::ToOwned; + pub use alloc::boxed::Box; + pub use alloc::collections::*; + pub use alloc::format; pub use alloc::rc::Rc; + pub use alloc::{string, string::*}; + pub use alloc::{vec, vec::*}; pub use core::cell::RefCell; - pub mod collections { - pub use self::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque}; - pub use alloc::collections::*; - pub use core::ops::Bound; - } - pub use crate::module::Module; - pub use alloc::string::String; - pub use alloc::vec; - pub use alloc::{borrow, boxed, format, string, string::ToString}; - pub use vec::Vec; + pub use core::ops::Bound; + pub use core::str::FromStr; } -#[cfg(not(feature = "std"))] pub use prelude::*; diff --git a/core/src/utils.rs b/core/src/utils.rs index 3494f7ff..0b2629f2 100644 --- a/core/src/utils.rs +++ b/core/src/utils.rs @@ -81,7 +81,7 @@ pub fn hex_to_slice(src: &[u8], dst: &mut [u8]) { /// Joins two parts of a key with the [`KEY_DELIMITER`]. #[inline] pub fn create_key(left: &str, right: &str) -> String { - alloc::format!("{}{}{}", left, KEY_DELIMITER, right) + crate::prelude::format!("{}{}{}", left, KEY_DELIMITER, right) } #[cfg(test)] diff --git a/examples2/src/counter_pack.rs b/examples2/src/counter_pack.rs index 9b3cc0bb..c0513fbb 100644 --- a/examples2/src/counter_pack.rs +++ b/examples2/src/counter_pack.rs @@ -2,6 +2,7 @@ use crate::counter::Counter; use odra::prelude::*; use odra::ContractEnv; use odra::Mapping; +use odra::Module; use odra::ModuleWrapper; pub struct CounterPack { diff --git a/examples2/src/erc20.rs b/examples2/src/erc20.rs index 41b44fc4..9d7f1dcd 100644 --- a/examples2/src/erc20.rs +++ b/examples2/src/erc20.rs @@ -1,4 +1,4 @@ -use odra::{casper_event_standard, OdraError}; +use odra::{casper_event_standard, Module, OdraError}; use odra::{prelude::*, CallDef, Event, ModuleWrapper}; use odra::{Address, ContractEnv, Mapping, Variable, U256, U512}; @@ -186,6 +186,7 @@ mod __erc20_wasm_parts { use odra::odra_casper_wasm_env::casper_contract::contract_api::runtime; use odra::odra_casper_wasm_env::casper_contract::unwrap_or_revert::UnwrapOrRevert; use odra::odra_casper_wasm_env::WasmContractEnv; + use odra::Module; use odra::{prelude::*, ContractEnv}; use odra::{runtime_args, Address, U256}; diff --git a/justfile b/justfile index 7511aaa3..62716272 100644 --- a/justfile +++ b/justfile @@ -75,7 +75,7 @@ clean: build-erc20: cd examples2 && ODRA_MODULE=Erc20 cargo build --release --target wasm32-unknown-unknown --bin contract wasm-strip examples2/target/wasm32-unknown-unknown/release/contract.wasm - rm -rf examples2/wasm/erc20.wasm + rm -rf examples2/wasm/Erc20.wasm mkdir -p examples2/wasm mv examples2/target/wasm32-unknown-unknown/release/contract.wasm examples2/wasm/Erc20.wasm diff --git a/odra-casper/test-vm/src/casper_host.rs b/odra-casper/test-vm/src/casper_host.rs index 682709e4..6777933e 100644 --- a/odra-casper/test-vm/src/casper_host.rs +++ b/odra-casper/test-vm/src/casper_host.rs @@ -1,4 +1,4 @@ -use odra_core::prelude::{collections::*, *}; +use odra_core::prelude::*; use std::cell::RefCell; use std::env; use std::path::PathBuf; diff --git a/odra-casper/test-vm/src/vm/casper_vm.rs b/odra-casper/test-vm/src/vm/casper_vm.rs index 67cc302f..ae146bb3 100644 --- a/odra-casper/test-vm/src/vm/casper_vm.rs +++ b/odra-casper/test-vm/src/vm/casper_vm.rs @@ -1,7 +1,5 @@ use odra_core::consts::*; -use odra_core::prelude::{collections::*, *}; - -use odra_core::prelude::{collections::*, *}; +use odra_core::prelude::*; use std::cell::RefCell; use std::env; use std::path::PathBuf; diff --git a/odra-casper/wasm-env/src/host_functions.rs b/odra-casper/wasm-env/src/host_functions.rs index a46de020..673228b7 100644 --- a/odra-casper/wasm-env/src/host_functions.rs +++ b/odra-casper/wasm-env/src/host_functions.rs @@ -17,9 +17,7 @@ use odra_core::casper_types::{ contracts::NamedKeys, ApiError, CLTyped, ContractPackageHash, EntryPoints, Key, URef }; -use odra_core::prelude::borrow::ToOwned; -use odra_core::prelude::boxed::Box; -use odra_core::prelude::{format, string::String, vec, vec::Vec}; +use odra_core::prelude::*; use odra_core::{Address, ExecutionError}; use crate::consts; @@ -340,7 +338,7 @@ fn revoke_access_to_constructor_group( contract_package_hash: ContractPackageHash, constructor_access: URef ) { - let mut urefs = odra_core::prelude::collections::BTreeSet::new(); + let mut urefs = BTreeSet::new(); urefs.insert(constructor_access); storage::remove_contract_user_group_urefs( contract_package_hash, diff --git a/odra-casper/wasm-env/src/wasm_contract_env.rs b/odra-casper/wasm-env/src/wasm_contract_env.rs index 5ddd5ab6..1e3d80bd 100644 --- a/odra-casper/wasm-env/src/wasm_contract_env.rs +++ b/odra-casper/wasm-env/src/wasm_contract_env.rs @@ -2,9 +2,9 @@ use crate::host_functions; use casper_types::bytesrepr::ToBytes; use casper_types::U512; use odra_core::casper_types; -use odra_core::prelude::boxed::Box; -use odra_core::{prelude::*, ContractContext, ContractEnv}; +use odra_core::prelude::*; use odra_core::{Address, Bytes, OdraError}; +use odra_core::{ContractContext, ContractEnv}; #[derive(Clone)] pub struct WasmContractEnv; diff --git a/odra-vm/src/odra_vm_host.rs b/odra-vm/src/odra_vm_host.rs index 7dd2f82a..61dd8eee 100644 --- a/odra-vm/src/odra_vm_host.rs +++ b/odra-vm/src/odra_vm_host.rs @@ -2,7 +2,7 @@ use crate::odra_vm_contract_env::OdraVmContractEnv; use crate::OdraVm; use odra_core::entry_point_callback::EntryPointsCaller; use odra_core::event::EventError; -use odra_core::prelude::{collections::*, *}; +use odra_core::prelude::*; use odra_core::{Address, Bytes, OdraError, RuntimeArgs, VmError, U512}; use odra_core::{CallDef, ContractContext, ContractEnv, HostContext, HostEnv}; diff --git a/odra-vm/src/vm/contract_container.rs b/odra-vm/src/vm/contract_container.rs index 503446a9..d912030c 100644 --- a/odra-vm/src/vm/contract_container.rs +++ b/odra-vm/src/vm/contract_container.rs @@ -1,6 +1,6 @@ use odra_core::call_def::CallDef; use odra_core::entry_point_callback::EntryPointsCaller; -use odra_core::prelude::{collections::*, *}; +use odra_core::prelude::*; use odra_core::HostEnv; use odra_core::{ casper_types::{NamedArg, RuntimeArgs}, diff --git a/odra-vm/src/vm/contract_register.rs b/odra-vm/src/vm/contract_register.rs index 02ca35a8..029dce4b 100644 --- a/odra-vm/src/vm/contract_register.rs +++ b/odra-vm/src/vm/contract_register.rs @@ -1,5 +1,5 @@ use odra_core::call_def::CallDef; -use odra_core::prelude::{collections::*, *}; +use odra_core::prelude::*; use odra_core::HostEnv; use odra_core::{casper_types::RuntimeArgs, Address, Bytes, OdraError, VmError};