diff --git a/CHANGELOG.md b/CHANGELOG.md index a2a20c93f..39787dcda 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ # 0.15.0 +- refactor: remove lifetime from RepoPutBlock and return RepoPutBlock in Ipfs::put_block. [PR XXX](https://github.com/dariusc93/rust-ipfs/pull/XXX) - chore: update libp2p to 0.55.0. [PR 375](https://github.com/dariusc93/rust-ipfs/pull/375) - feat: Add reconnect option to address book. [PR 356](https://github.com/dariusc93/rust-ipfs/pull/356) - chore: use async-rt in place of rt utils. [PR 362](https://github.com/dariusc93/rust-ipfs/pull/362) diff --git a/src/lib.rs b/src/lib.rs index 3643bab93..b8110991a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1199,8 +1199,8 @@ impl Ipfs { } /// Puts a block into the ipfs repo. - pub async fn put_block(&self, block: &Block) -> Result { - self.repo.put_block(block).span(self.span.clone()).await + pub fn put_block(&self, block: &Block) -> RepoPutBlock { + self.repo.put_block(block).span(self.span.clone()) } /// Retrieves a block from the local blockstore, or starts fetching from the network or join an @@ -3081,7 +3081,7 @@ pub(crate) fn to_dht_key, F: Fn(&str) -> anyhow::Result>( } use crate::p2p::AddressBookConfig; -use crate::repo::RepoGetBlock; +use crate::repo::{RepoGetBlock, RepoPutBlock}; #[doc(hidden)] pub use node::Node; diff --git a/src/repo/mod.rs b/src/repo/mod.rs index 2b32131cd..220079004 100644 --- a/src/repo/mod.rs +++ b/src/repo/mod.rs @@ -587,7 +587,7 @@ impl Repo { } /// Puts a block into the block store. - pub fn put_block<'a>(&self, block: &'a Block) -> RepoPutBlock<'a> { + pub fn put_block(&self, block: &Block) -> RepoPutBlock { RepoPutBlock::new(self, block).broadcast_on_new_block(true) } @@ -1113,15 +1113,16 @@ impl IntoFuture for RepoGetBlocks { } } -pub struct RepoPutBlock<'a> { +pub struct RepoPutBlock { repo: Repo, - block: &'a Block, + block: Option, span: Option, broadcast_on_new_block: bool, } -impl<'a> RepoPutBlock<'a> { - fn new(repo: &Repo, block: &'a Block) -> Self { +impl RepoPutBlock { + fn new(repo: &Repo, block: &Block) -> Self { + let block = Some(block.clone()); Self { repo: repo.clone(), block, @@ -1141,11 +1142,11 @@ impl<'a> RepoPutBlock<'a> { } } -impl IntoFuture for RepoPutBlock<'_> { +impl IntoFuture for RepoPutBlock { type IntoFuture = BoxFuture<'static, Self::Output>; type Output = Result; - fn into_future(self) -> Self::IntoFuture { - let block = self.block.clone(); + fn into_future(mut self) -> Self::IntoFuture { + let block = self.block.take().expect("valid block is set"); let span = self.span.unwrap_or(Span::current()); let span = debug_span!(parent: &span, "put_block", cid = %block.cid()); async move {