diff --git a/Cargo.toml b/Cargo.toml index dca1280..151986a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,7 @@ failure = "0.1.5" flat-tree = "4.1.0" lazy_static = "1.3.0" memory-pager = "0.9.0" -merkle-tree-stream = "0.10.0" +merkle-tree-stream = "0.11.0" pretty-hash = "0.4.0" rand = "0.6.0" random-access-disk = "0.8.0" @@ -41,3 +41,4 @@ quickcheck = "0.8.5" data-encoding = "2.1.2" remove_dir_all = "0.5.2" tempfile = "3.1.0" +async-std = "1.5.0" diff --git a/examples/async.rs b/examples/async.rs new file mode 100644 index 0000000..9fa767d --- /dev/null +++ b/examples/async.rs @@ -0,0 +1,30 @@ +use async_std::task; +use failure::Error; +use hypercore::Feed; +use random_access_storage::RandomAccess; +use std::fmt::Debug; + +async fn append(feed: &mut Feed, content: &[u8]) +where + T: RandomAccess + Debug, +{ + feed.append(content).unwrap(); +} + +async fn print(feed: &mut Feed) +where + T: RandomAccess + Debug, +{ + println!("{:?}", feed.get(0)); + println!("{:?}", feed.get(1)); +} + +fn main() { + task::block_on(task::spawn(async { + let mut feed = Feed::default(); + + append(&mut feed, b"hello").await; + append(&mut feed, b"world").await; + print(&mut feed).await; + })); +} diff --git a/src/crypto/merkle.rs b/src/crypto/merkle.rs index bba8e7a..8711e6a 100644 --- a/src/crypto/merkle.rs +++ b/src/crypto/merkle.rs @@ -1,7 +1,7 @@ use crate::crypto::Hash; use crate::storage::Node; use merkle_tree_stream::{HashMethods, MerkleTreeStream, NodeKind, PartialNode}; -use std::rc::Rc; +use std::sync::Arc; #[derive(Debug)] struct H; @@ -10,7 +10,7 @@ impl HashMethods for H { type Node = Node; type Hash = Hash; - fn leaf(&self, leaf: &PartialNode, _roots: &[Rc]) -> Self::Hash { + fn leaf(&self, leaf: &PartialNode, _roots: &[Arc]) -> Self::Hash { match leaf.data() { NodeKind::Leaf(data) => Hash::from_leaf(&data), NodeKind::Parent => unreachable!(), @@ -26,7 +26,7 @@ impl HashMethods for H { #[derive(Debug)] pub struct Merkle { stream: MerkleTreeStream, - nodes: Vec>, + nodes: Vec>, } impl Default for Merkle { @@ -52,12 +52,12 @@ impl Merkle { } /// Get the roots vector. - pub fn roots(&self) -> &Vec> { + pub fn roots(&self) -> &Vec> { self.stream.roots() } /// Get the nodes from the struct. - pub fn nodes(&self) -> &Vec> { + pub fn nodes(&self) -> &Vec> { &self.nodes } } diff --git a/src/feed.rs b/src/feed.rs index ecc2cf9..225ff98 100644 --- a/src/feed.rs +++ b/src/feed.rs @@ -23,7 +23,7 @@ use std::cmp; use std::fmt::{self, Debug, Display}; use std::ops::Range; use std::path::Path; -use std::rc::Rc; +use std::sync::Arc; /// Append-only log structure. #[derive(Debug)] @@ -398,7 +398,7 @@ where /// root nodes combined. pub fn verify(&mut self, index: usize, signature: &Signature) -> Result<()> { let roots = self.root_hashes(index)?; - let roots: Vec<_> = roots.into_iter().map(Rc::new).collect(); + let roots: Vec<_> = roots.into_iter().map(Arc::new).collect(); let message = Hash::from_roots(&roots); let message = message.as_bytes();