diff --git a/README.md b/README.md index 9b02fd52..d410968d 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,6 @@ Currently implements an altruistic bitswap strategy over mdns. ## Getting started ```rust #![feature(async_await, await_macro, futures_api)] -use futures::future::FutureObj; -use futures::prelude::*; use ipfs::{Block, Ipfs, IpfsOptions, Types}; fn main() { @@ -15,8 +13,8 @@ fn main() { let block = Block::from("hello block2\n"); let cid = Block::from("hello block\n").cid(); - tokio::run(FutureObj::new(Box::new(async move { - tokio::spawn(ipfs.start_daemon().compat()); + tokio::run_async(async move { + tokio::spawn_async(ipfs.start_daemon()); await!(ipfs.init_repo()).unwrap(); await!(ipfs.open_repo()).unwrap(); @@ -25,8 +23,8 @@ fn main() { println!("Received block with contents: {:?}", String::from_utf8_lossy(&block.data())); - Ok(()) - })).compat()); + ipfs.exit_daemon(); + }); } ``` diff --git a/examples/client1.rs b/examples/client1.rs index b7fe36a2..1f0c6373 100644 --- a/examples/client1.rs +++ b/examples/client1.rs @@ -1,6 +1,4 @@ #![feature(async_await, await_macro, futures_api)] -use futures::future::FutureObj; -use futures::prelude::*; use ipfs::{Block, Ipfs, IpfsOptions, Types}; fn main() { @@ -10,8 +8,8 @@ fn main() { let block = Block::from("hello block2\n"); let cid = Block::from("hello block\n").cid(); - tokio::run(FutureObj::new(Box::new(async move { - tokio::spawn(ipfs.start_daemon().compat()); + tokio::run_async(async move { + tokio::spawn_async(ipfs.start_daemon()); await!(ipfs.init_repo()).unwrap(); await!(ipfs.open_repo()).unwrap(); @@ -19,7 +17,5 @@ fn main() { let block = await!(ipfs.get_block(cid.clone())).unwrap(); println!("Received block with contents: {:?}", String::from_utf8_lossy(&block.data())); - - Ok(()) - })).compat()); + }); } diff --git a/examples/client2.rs b/examples/client2.rs index 4a382aa3..7eb6a24d 100644 --- a/examples/client2.rs +++ b/examples/client2.rs @@ -1,6 +1,4 @@ #![feature(async_await, await_macro, futures_api)] -use futures::future::FutureObj; -use futures::prelude::*; use ipfs::{Block, Ipfs, IpfsOptions, RepoTypes, SwarmTypes, IpfsTypes}; #[derive(Clone)] @@ -24,13 +22,12 @@ fn main() { let block = Block::from("hello block\n"); let cid = Block::from("hello block2\n").cid(); - tokio::run(FutureObj::new(Box::new(async move { - tokio::spawn(ipfs.start_daemon().compat()); + tokio::run_async(async move { + tokio::spawn_async(ipfs.start_daemon()); await!(ipfs.put_block(block)).unwrap(); let block = await!(ipfs.get_block(cid)).unwrap(); println!("Received block with contents: {:?}", String::from_utf8_lossy(&block.data())); - Ok(()) - })).compat()); + }); } diff --git a/src/ipld/path.rs b/src/ipld/path.rs index 14791b15..f338808a 100644 --- a/src/ipld/path.rs +++ b/src/ipld/path.rs @@ -69,6 +69,12 @@ impl IpldPath { } } +impl From for IpldPath { + fn from(cid: Cid) -> Self { + IpldPath::new(cid) + } +} + #[derive(Clone, Debug, PartialEq)] pub enum SubPath { Key(String), diff --git a/src/lib.rs b/src/lib.rs index 2351f6a3..6c096924 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -113,6 +113,7 @@ enum IpfsEvent { WantBlock(Cid), ProvideBlock(Cid), UnprovideBlock(Cid), + Exit, } impl Ipfs { @@ -190,6 +191,11 @@ impl Ipfs { swarm: Box::new(swarm), } } + + /// Exit daemon. + pub fn exit_daemon(&mut self) { + self.events.lock().unwrap().push_back(IpfsEvent::Exit) + } } pub struct IpfsFuture { @@ -198,7 +204,7 @@ pub struct IpfsFuture { } impl Future for IpfsFuture { - type Output = Result<(), ()>; + type Output = (); fn poll(self: Pin<&mut Self>, _waker: &Waker) -> Poll { let _self = self.get_mut(); @@ -214,13 +220,16 @@ impl Future for IpfsFuture { IpfsEvent::UnprovideBlock(cid) => { _self.swarm.stop_providing_block(&cid); } + IpfsEvent::Exit => { + return Poll::Ready(()); + } } } let poll = _self.swarm.poll().expect("Error while polling swarm"); match poll { Async::Ready(Some(_)) => {}, Async::Ready(None) => { - return Poll::Ready(Ok(())); + return Poll::Ready(()); }, Async::NotReady => { return Poll::Pending; @@ -232,18 +241,53 @@ impl Future for IpfsFuture { #[cfg(test)] mod tests { - /* use super::*; + #[derive(Clone)] + struct Types; + + impl RepoTypes for Types { + type TBlockStore = crate::repo::mem::MemBlockStore; + type TDataStore = crate::repo::mem::MemDataStore; + } + + impl SwarmTypes for Types { + type TStrategy = crate::bitswap::strategy::AltruisticStrategy; + } + + impl IpfsTypes for Types {} + #[test] fn test_put_and_get_block() { let options = IpfsOptions::test(); - let mut ipfs = Ipfs::new(options); + let mut ipfs = Ipfs::::new(options); let block = Block::from("hello block\n"); - let cid = ipfs.put_block(block.clone()); - let future = ipfs.get_block(cid).map(move |new_block| { + + tokio::run_async(async move { + tokio::spawn_async(ipfs.start_daemon()); + + let cid = await!(ipfs.put_block(block.clone())).unwrap(); + let new_block = await!(ipfs.get_block(cid)).unwrap(); assert_eq!(block, new_block); + + ipfs.exit_daemon(); }); - run_ipfs(ipfs, future); - }*/ + } + + #[test] + fn test_put_and_get_dag() { + let options = IpfsOptions::test(); + let mut ipfs = Ipfs::::new(options); + + tokio::run_async(async move { + tokio::spawn_async(ipfs.start_daemon()); + + let data: Ipld = vec![-1, -2, -3].into(); + let cid = await!(ipfs.put_dag(data.clone())).unwrap(); + let new_data = await!(ipfs.get_dag(cid.into())).unwrap(); + assert_eq!(Some(data), new_data); + + ipfs.exit_daemon(); + }); + } }