feat: support no_std

This commit is contained in:
jjy
2020-01-17 13:26:05 +08:00
parent c18c099234
commit 1374825778
8 changed files with 43 additions and 15 deletions
+5
View File
@@ -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"
+4 -1
View File
@@ -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
+2 -2
View File
@@ -1,5 +1,5 @@
pub use failure::Fail;
pub type Result<T> = ::std::result::Result<T, Error>;
pub type Result<T> = core::result::Result<T, Error>;
#[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),
}
+2
View File
@@ -1,3 +1,5 @@
use crate::vec::Vec;
pub fn leaf_index_to_pos(index: u64) -> u64 {
if index == 0 {
return 0;
+17
View File
@@ -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;
}
}
+4 -3
View File
@@ -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<T, M, S: MMRStore<T>> {
mmr_size: u64,
+2 -2
View File
@@ -1,4 +1,4 @@
use crate::Result;
use crate::{vec::Vec, Result};
#[derive(Default)]
pub struct MMRBatch<Elem, Store: MMRStore<Elem>> {
@@ -45,7 +45,7 @@ impl<Elem: Clone, Store: MMRStore<Elem>> MMRBatch<Elem, Store> {
impl<Elem, Store: MMRStore<Elem>> IntoIterator for MMRBatch<Elem, Store> {
type Item = (u64, Vec<Elem>);
type IntoIter = ::std::vec::IntoIter<Self::Item>;
type IntoIter = crate::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.memory_batch.into_iter()
+7 -7
View File
@@ -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<T>(RefCell<HashMap<u64, T>>);
pub struct MemStore<T>(RefCell<BTreeMap<u64, T>>);
impl<T> Default for MemStore<T> {
fn default() -> Self {
@@ -15,7 +15,7 @@ impl<T> Default for MemStore<T> {
impl<T> MemStore<T> {
fn new() -> Self {
MemStore(RefCell::new(HashMap::new()))
MemStore(RefCell::new(Default::default()))
}
}