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

feat!: Use RPITIT for Multihasher and remove async-trait #39

Merged
merged 1 commit into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading