diff --git a/Cargo.lock b/Cargo.lock index e47152b..feb2f0b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -293,7 +293,6 @@ name = "beetswap" version = "0.1.0" dependencies = [ "anyhow", - "async-trait", "asynchronous-codec", "blockstore", "bytes", diff --git a/Cargo.toml b/Cargo.toml index 94c8dca..4582efe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/multihasher.rs b/src/multihasher.rs index 78d3ae3..c8e11d8 100644 --- a/src/multihasher.rs +++ b/src/multihasher.rs @@ -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; @@ -63,7 +65,6 @@ impl MultihasherError { } /// Trait for producing a custom [`Multihash`]. -#[async_trait] pub trait Multihasher { /// Hash the `input` based on the `multihash_code`. /// @@ -73,17 +74,38 @@ pub trait Multihasher { /// [`BehaviourBuilder::register_multihasher`]. /// /// [`BehaviourBuilder::register_multihasher`]: crate::BehaviourBuilder::register_multihasher - async fn hash( + fn hash( &self, multihash_code: u64, input: &[u8], - ) -> Result, MultihasherError>; + ) -> impl Future, MultihasherError>> + Send; +} + +/// Workaround for having dynamic dispatch for `Multihasher` internally. +trait DispatchableMultihasher { + fn hash<'a>( + &'a self, + multihash_code: u64, + input: &'a [u8], + ) -> Pin, MultihasherError>> + Send + 'a>>; +} + +impl DispatchableMultihasher for T +where + T: Multihasher, +{ + fn hash<'a>( + &'a self, + multihash_code: u64, + input: &'a [u8], + ) -> Pin, MultihasherError>> + Send + 'a>> { + Multihasher::::hash(self, multihash_code, input).boxed() + } } /// [`Multihasher`] that uses [`multihash_codetable::Code`] pub struct StandardMultihasher; -#[async_trait] impl Multihasher for StandardMultihasher { async fn hash( &self, @@ -100,7 +122,7 @@ impl Multihasher for StandardMultihasher { } pub(crate) struct MultihasherTable { - multihashers: VecDeque + Send + Sync + 'static>>, + multihashers: VecDeque + Send + Sync + 'static>>, } impl fmt::Debug for MultihasherTable {