diff --git a/Cargo.toml b/Cargo.toml index 5771000..949c382 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,8 +7,13 @@ license = "MIT" description = "A generalized merkle mountain range implementation" repository = "https://github.com/nervosnetwork/merkle-mountain-range" +[features] +default = ["std"] +std = [] + [dependencies] failure = "0.1.5" +cfg-if = "0.1" [dev-dependencies] faster-hex = "0.3" diff --git a/Makefile b/Makefile index 19a3ffa..30fa2b8 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ default: ci -ci: fmt clippy test bench-test +ci: fmt check-no-std clippy test bench-test test: cargo test --all --all-features @@ -13,3 +13,6 @@ clippy: fmt: cargo fmt --all -- --check + +check-no-std: + cargo check --all --no-default-features diff --git a/src/error.rs b/src/error.rs index a2c0d1f..108830c 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,5 +1,5 @@ pub use failure::Fail; -pub type Result = ::std::result::Result; +pub type Result = core::result::Result; #[derive(Fail, Debug, PartialEq, Eq, Clone)] pub enum Error { @@ -8,5 +8,5 @@ pub enum Error { #[fail(display = "Inconsistent store")] InconsistentStore, #[fail(display = "Store error {}", _0)] - StoreError(String), + StoreError(crate::string::String), } diff --git a/src/helper.rs b/src/helper.rs index c8dfff5..fe623cb 100644 --- a/src/helper.rs +++ b/src/helper.rs @@ -1,3 +1,5 @@ +use crate::vec::Vec; + pub fn leaf_index_to_pos(index: u64) -> u64 { if index == 0 { return 0; diff --git a/src/lib.rs b/src/lib.rs index a36e787..215905c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ +#![cfg_attr(not(feature = "std"), no_std)] + mod error; mod helper; mod merge; @@ -12,3 +14,18 @@ pub use helper::{leaf_index_to_mmr_size, leaf_index_to_pos}; pub use merge::Merge; pub use mmr::{MerkleProof, MMR}; pub use mmr_store::MMRStore; + +cfg_if::cfg_if! { + if #[cfg(feature = "std")] { + use std::borrow; + use std::collections; + use std::vec; + use std::string; + } else { + extern crate alloc; + use alloc::borrow; + use alloc::collections; + use alloc::vec; + use alloc::string; + } +} diff --git a/src/mmr.rs b/src/mmr.rs index cbbbc6c..75eff9b 100644 --- a/src/mmr.rs +++ b/src/mmr.rs @@ -4,12 +4,13 @@ //! https://github.com/mimblewimble/grin/blob/master/doc/mmr.md#structure //! https://github.com/mimblewimble/grin/blob/0ff6763ee64e5a14e70ddd4642b99789a1648a32/core/src/core/pmmr.rs#L606 +use crate::borrow::Cow; use crate::helper::{get_peaks, parent_offset, pos_height_in_tree, sibling_offset}; use crate::mmr_store::{MMRBatch, MMRStore}; +use crate::vec::Vec; use crate::{Error, Merge, Result}; -use std::borrow::Cow; -use std::fmt::Debug; -use std::marker::PhantomData; +use core::fmt::Debug; +use core::marker::PhantomData; pub struct MMR> { mmr_size: u64, diff --git a/src/mmr_store.rs b/src/mmr_store.rs index 2114ad6..c27ed72 100644 --- a/src/mmr_store.rs +++ b/src/mmr_store.rs @@ -1,4 +1,4 @@ -use crate::Result; +use crate::{vec::Vec, Result}; #[derive(Default)] pub struct MMRBatch> { @@ -45,7 +45,7 @@ impl> MMRBatch { impl> IntoIterator for MMRBatch { type Item = (u64, Vec); - type IntoIter = ::std::vec::IntoIter; + type IntoIter = crate::vec::IntoIter; fn into_iter(self) -> Self::IntoIter { self.memory_batch.into_iter() diff --git a/src/util.rs b/src/util.rs index 94ec617..f174cb5 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,11 +1,11 @@ -use crate::{MMRStore, Merge, MerkleProof, Result, MMR}; -use std::cell::RefCell; -use std::collections::HashMap; -use std::fmt::Debug; -use std::marker::PhantomData; +use crate::collections::BTreeMap; +use crate::{vec::Vec, MMRStore, Merge, MerkleProof, Result, MMR}; +use core::cell::RefCell; +use core::fmt::Debug; +use core::marker::PhantomData; #[derive(Clone)] -pub struct MemStore(RefCell>); +pub struct MemStore(RefCell>); impl Default for MemStore { fn default() -> Self { @@ -15,7 +15,7 @@ impl Default for MemStore { impl MemStore { fn new() -> Self { - MemStore(RefCell::new(HashMap::new())) + MemStore(RefCell::new(Default::default())) } }