Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

example to ensure structs are send #95

Merged
merged 4 commits into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = { version = "0.10.0", git = "https://github.com/bltavares/merkle-tree-stream", branch = "send" }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we've merged this, but not published. Added you as a contrib, let me know if you need publish access on crates.io as well!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New version released.

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