Skip to content

Commit

Permalink
refactor: remove lifetime, and return RepoPutBlock in Ipfs::put_block (
Browse files Browse the repository at this point in the history
  • Loading branch information
dariusc93 authored Feb 6, 2025
1 parent d1621bc commit 6b5b1ed
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# 0.15.0
- refactor: remove lifetime from RepoPutBlock and return RepoPutBlock in Ipfs::put_block. [PR 384](https://github.com/dariusc93/rust-ipfs/pull/384)
- 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)
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1199,8 +1199,8 @@ impl Ipfs {
}

/// Puts a block into the ipfs repo.
pub async fn put_block(&self, block: &Block) -> Result<Cid, Error> {
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
Expand Down Expand Up @@ -3081,7 +3081,7 @@ pub(crate) fn to_dht_key<B: AsRef<str>, F: Fn(&str) -> anyhow::Result<Key>>(
}

use crate::p2p::AddressBookConfig;
use crate::repo::RepoGetBlock;
use crate::repo::{RepoGetBlock, RepoPutBlock};
#[doc(hidden)]
pub use node::Node;

Expand Down
17 changes: 9 additions & 8 deletions src/repo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -1113,15 +1113,16 @@ impl IntoFuture for RepoGetBlocks {
}
}

pub struct RepoPutBlock<'a> {
pub struct RepoPutBlock {
repo: Repo,
block: &'a Block,
block: Option<Block>,
span: Option<Span>,
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,
Expand All @@ -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<Cid, Error>;
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 {
Expand Down

0 comments on commit 6b5b1ed

Please sign in to comment.