Skip to content

Commit

Permalink
Fight for Prelude (#267)
Browse files Browse the repository at this point in the history
* 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::*`
  • Loading branch information
kubaplas authored Nov 24, 2023
1 parent e922525 commit 9aa7f1a
Show file tree
Hide file tree
Showing 23 changed files with 36 additions and 76 deletions.
10 changes: 1 addition & 9 deletions core/src/address.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion core/src/call_def.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloc::string::String;
use crate::prelude::*;
use casper_types::bytesrepr::FromBytes;
use casper_types::{CLTyped, RuntimeArgs, U512};

Expand Down
5 changes: 1 addition & 4 deletions core/src/call_result.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 2 additions & 2 deletions core/src/contract_def.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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]
Expand Down
6 changes: 2 additions & 4 deletions core/src/error.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion core/src/event.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Events interface and errors.
use crate::prelude::string::String;
use crate::prelude::*;

/// Event-related errors.
#[derive(Debug, PartialEq, Eq, PartialOrd)]
Expand Down
1 change: 0 additions & 1 deletion core/src/host_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
4 changes: 2 additions & 2 deletions core/src/item.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloc::{collections::BTreeMap, string::String, vec::Vec};
use crate::prelude::*;
use casper_types::{
bytesrepr::{FromBytes, ToBytes},
U128, U256, U512
Expand All @@ -11,7 +11,7 @@ pub trait OdraItem {

#[cfg(not(target_arch = "wasm32"))]
fn events() -> Vec<crate::contract_def::Event> {
alloc::vec![]
vec![]
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/mapping.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::prelude::*;
use crate::{
module::{Module, ModuleWrapper},
prelude::*,
variable::Variable,
ContractEnv
};
Expand Down
4 changes: 2 additions & 2 deletions core/src/module.rs
Original file line number Diff line number Diff line change
@@ -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<Vec<u8>>;
Expand Down
2 changes: 1 addition & 1 deletion core/src/native_token.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloc::string::String;
use crate::prelude::string::String;

pub struct NativeTokenMetadata {
pub name: String,
Expand Down
40 changes: 8 additions & 32 deletions core/src/prelude.rs
Original file line number Diff line number Diff line change
@@ -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::*;
2 changes: 1 addition & 1 deletion core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
1 change: 1 addition & 0 deletions examples2/src/counter_pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion examples2/src/erc20.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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};

Expand Down
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion odra-casper/test-vm/src/casper_host.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use odra_core::prelude::{collections::*, *};
use odra_core::prelude::*;
use std::cell::RefCell;
use std::env;
use std::path::PathBuf;
Expand Down
4 changes: 1 addition & 3 deletions odra-casper/test-vm/src/vm/casper_vm.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
6 changes: 2 additions & 4 deletions odra-casper/wasm-env/src/host_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions odra-casper/wasm-env/src/wasm_contract_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion odra-vm/src/odra_vm_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
2 changes: 1 addition & 1 deletion odra-vm/src/vm/contract_container.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down
2 changes: 1 addition & 1 deletion odra-vm/src/vm/contract_register.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down

0 comments on commit 9aa7f1a

Please sign in to comment.