Skip to content

Commit

Permalink
Merge pull request #95 from bltavares/send
Browse files Browse the repository at this point in the history
example to ensure structs are send
  • Loading branch information
bltavares authored Feb 19, 2020
2 parents f77fe7b + 46be519 commit 31dfdd1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
30 changes: 30 additions & 0 deletions examples/async.rs
Original file line number Diff line number Diff line change
@@ -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<T>(feed: &mut Feed<T>, content: &[u8])
where
T: RandomAccess<Error = Error> + Debug,
{
feed.append(content).unwrap();
}

async fn print<T>(feed: &mut Feed<T>)
where
T: RandomAccess<Error = Error> + 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;
}));
}
10 changes: 5 additions & 5 deletions src/crypto/merkle.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,7 +10,7 @@ impl HashMethods for H {
type Node = Node;
type Hash = Hash;

fn leaf(&self, leaf: &PartialNode, _roots: &[Rc<Self::Node>]) -> Self::Hash {
fn leaf(&self, leaf: &PartialNode, _roots: &[Arc<Self::Node>]) -> Self::Hash {
match leaf.data() {
NodeKind::Leaf(data) => Hash::from_leaf(&data),
NodeKind::Parent => unreachable!(),
Expand All @@ -26,7 +26,7 @@ impl HashMethods for H {
#[derive(Debug)]
pub struct Merkle {
stream: MerkleTreeStream<H>,
nodes: Vec<Rc<Node>>,
nodes: Vec<Arc<Node>>,
}

impl Default for Merkle {
Expand All @@ -52,12 +52,12 @@ impl Merkle {
}

/// Get the roots vector.
pub fn roots(&self) -> &Vec<Rc<Node>> {
pub fn roots(&self) -> &Vec<Arc<Node>> {
self.stream.roots()
}

/// Get the nodes from the struct.
pub fn nodes(&self) -> &Vec<Rc<Node>> {
pub fn nodes(&self) -> &Vec<Arc<Node>> {
&self.nodes
}
}
4 changes: 2 additions & 2 deletions src/feed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 31dfdd1

Please sign in to comment.