Skip to content

Commit

Permalink
chore: Use RPITIT for Multihasher and remove async-trait
Browse files Browse the repository at this point in the history
  • Loading branch information
oblique committed Apr 4, 2024
1 parent 13f1a08 commit ff08be3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ version = "0.1.0"
edition = "2021"
license = "Apache-2.0"
description = "Implementation of bitswap protocol for libp2p"
rust-version = "1.75"

[dependencies]
async-trait = "0.1"
asynchronous-codec = "0.7"
blockstore = "0.3"
bytes = "1"
Expand Down
34 changes: 28 additions & 6 deletions src/multihasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
use std::collections::VecDeque;
use std::fmt::{self, Display};
use std::future::Future;
use std::pin::Pin;

use async_trait::async_trait;
use futures::FutureExt;
use libp2p_core::multihash::Multihash;
use multihash_codetable::MultihashDigest;

Expand Down Expand Up @@ -63,7 +65,6 @@ impl MultihasherError {
}

/// Trait for producing a custom [`Multihash`].
#[async_trait]
pub trait Multihasher<const S: usize> {
/// Hash the `input` based on the `multihash_code`.
///
Expand All @@ -73,17 +74,38 @@ pub trait Multihasher<const S: usize> {
/// [`BehaviourBuilder::register_multihasher`].
///
/// [`BehaviourBuilder::register_multihasher`]: crate::BehaviourBuilder::register_multihasher
async fn hash(
fn hash(
&self,
multihash_code: u64,
input: &[u8],
) -> Result<Multihash<S>, MultihasherError>;
) -> impl Future<Output = Result<Multihash<S>, MultihasherError>> + Send;
}

/// Workaround for having dynamic dispatch for `Multihasher` internally.
trait DispatchableMultihasher<const S: usize> {
fn hash<'a>(
&'a self,
multihash_code: u64,
input: &'a [u8],
) -> Pin<Box<dyn Future<Output = Result<Multihash<S>, MultihasherError>> + Send + 'a>>;
}

impl<const S: usize, T> DispatchableMultihasher<S> for T
where
T: Multihasher<S>,
{
fn hash<'a>(
&'a self,
multihash_code: u64,
input: &'a [u8],
) -> Pin<Box<dyn Future<Output = Result<Multihash<S>, MultihasherError>> + Send + 'a>> {
Multihasher::<S>::hash(self, multihash_code, input).boxed()
}
}

/// [`Multihasher`] that uses [`multihash_codetable::Code`]
pub struct StandardMultihasher;

#[async_trait]
impl<const S: usize> Multihasher<S> for StandardMultihasher {
async fn hash(
&self,
Expand All @@ -100,7 +122,7 @@ impl<const S: usize> Multihasher<S> for StandardMultihasher {
}

pub(crate) struct MultihasherTable<const S: usize> {
multihashers: VecDeque<Box<dyn Multihasher<S> + Send + Sync + 'static>>,
multihashers: VecDeque<Box<dyn DispatchableMultihasher<S> + Send + Sync + 'static>>,
}

impl<const S: usize> fmt::Debug for MultihasherTable<S> {
Expand Down

0 comments on commit ff08be3

Please sign in to comment.