Skip to content

Commit

Permalink
Remove all the Sync and Send trait bounds
Browse files Browse the repository at this point in the history
Trait bounds should generally be pushed to the latest moment when they are actually needed.
That is, the functions that manipulate the data and that actually require the bound.
It is usually avoided (to the reasonable extend) to put trait bounds in the type parameters
of data types and trait declarations, as they unleash an unergonomic "trait bound hell" and are usually overly restrictive.

Some relevant references:
- [API Guidelines]
- [discussion API Guidelines]
- [flat2 case]
- [Haskell Programming Guidelines]

In this case, the trait bounds "Sync" and "Send" are already enforced (if necessary) when the async runtime actually schedule the Futures,
and it is at this point (if actually enforced by the runtime) that all the types and intermediate functions that manipulate those types are required
to enforce these markers; However, if at any point of the rabbit hole a type bound is placed on the data, hell breaks loose and all types that touch
the bounded type get contaminated. [Implied bounds RFC] might mitigate the issue, but for the time being, this convention is gold one of those that
is "all or nothing", because of the "viral" effect.

[API Guidelines]: https://rust-lang.github.io/api-guidelines/future-proofing.html#data-structures-do-not-duplicate-derived-trait-bounds-c-struct-bounds
[discussion API Guidelines]: rust-lang/api-guidelines#6
[Haskell Programming Guidelines]: https://wiki.haskell.org/Programming_guidelines#Types
[flate2 case]: rust-lang/flate2-rs#88
[Implied bounds RFC]: rust-lang/rust#44491
  • Loading branch information
Arnau Orriols committed Sep 3, 2021
1 parent de493f7 commit 6f9e3bd
Show file tree
Hide file tree
Showing 21 changed files with 63 additions and 83 deletions.
4 changes: 2 additions & 2 deletions iota-streams-app-channels/src/api/key_store.rs
Expand Up @@ -14,7 +14,7 @@ use iota_streams_core::{
};
use iota_streams_core_edsig::key_exchange::x25519;

pub trait KeyStore<Info, F: PRP>: Default + Send + Sync {
pub trait KeyStore<Info, F: PRP>: Default {
fn filter<'a, I>(&self, ids: I) -> Vec<(&Identifier, Vec<u8>)>
where
I: IntoIterator<Item = &'a Identifier>;
Expand Down Expand Up @@ -55,7 +55,7 @@ impl<Info> Default for KeyMap<Info> {
}
}

impl<Info: Send + Sync, F: PRP> KeyStore<Info, F> for KeyMap<Info> {
impl<Info, F: PRP> KeyStore<Info, F> for KeyMap<Info> {
fn filter<'a, I>(&self, ids: I) -> Vec<(&Identifier, Vec<u8>)>
where
I: IntoIterator<Item = &'a Identifier>,
Expand Down
6 changes: 3 additions & 3 deletions iota-streams-app-channels/src/api/user.rs
Expand Up @@ -995,7 +995,7 @@ where
}
}

#[async_trait]
#[async_trait(?Send)]
impl<F, Link, LG, LS, Keys> ContentSizeof<F> for User<F, Link, LG, LS, Keys>
where
F: PRP,
Expand Down Expand Up @@ -1054,7 +1054,7 @@ where
}
}

#[async_trait]
#[async_trait(?Send)]
impl<F, Link, Store, LG, LS, Keys> ContentWrap<F, Store> for User<F, Link, LG, LS, Keys>
where
F: PRP,
Expand Down Expand Up @@ -1118,7 +1118,7 @@ where
}
}

#[async_trait]
#[async_trait(?Send)]
impl<F, Link, Store, LG, LS, Keys> ContentUnwrap<F, Store> for User<F, Link, LG, LS, Keys>
where
F: PRP,
Expand Down
9 changes: 4 additions & 5 deletions iota-streams-app-channels/src/message/announce.rs
Expand Up @@ -55,7 +55,7 @@ impl<'a, F> ContentWrap<'a, F> {
}
}

#[async_trait]
#[async_trait(?Send)]
impl<'a, F: PRP> message::ContentSizeof<F> for ContentWrap<'a, F> {
async fn sizeof<'c>(&self, ctx: &'c mut sizeof::Context<F>) -> Result<&'c mut sizeof::Context<F>> {
ctx.absorb(&self.sig_kp.public)?;
Expand All @@ -65,8 +65,8 @@ impl<'a, F: PRP> message::ContentSizeof<F> for ContentWrap<'a, F> {
}
}

#[async_trait]
impl<'a, F: PRP, Store: Send + Sync> message::ContentWrap<F, Store> for ContentWrap<'a, F> {
#[async_trait(?Send)]
impl<'a, F: PRP, Store> message::ContentWrap<F, Store> for ContentWrap<'a, F> {
async fn wrap<'c, OS: io::OStream>(
&self,
_store: &Store,
Expand Down Expand Up @@ -103,11 +103,10 @@ impl<F> Default for ContentUnwrap<F> {
}
}

#[async_trait]
#[async_trait(?Send)]
impl<F, Store> message::ContentUnwrap<F, Store> for ContentUnwrap<F>
where
F: PRP,
Store: Send + Sync,
{
async fn unwrap<'c, IS: io::IStream>(
&mut self,
Expand Down
10 changes: 5 additions & 5 deletions iota-streams-app-channels/src/message/keyload.rs
Expand Up @@ -104,7 +104,7 @@ where
pub(crate) _phantom: core::marker::PhantomData<(F, Link)>,
}

#[async_trait]
#[async_trait(?Send)]
impl<'a, F, Link> message::ContentSizeof<F> for ContentWrap<'a, F, Link>
where
F: 'a + PRP, // weird 'a constraint, but compiler requires it somehow?!
Expand Down Expand Up @@ -149,7 +149,7 @@ where
}
}

#[async_trait]
#[async_trait(?Send)]
impl<'a, F, Link, Store> message::ContentWrap<F, Store> for ContentWrap<'a, F, Link>
where
F: 'a + PRP, // weird 'a constraint, but compiler requires it somehow?!
Expand Down Expand Up @@ -241,16 +241,16 @@ where
}
}

#[async_trait]
#[async_trait(?Send)]
impl<'a, 'b, F, Link, LStore, PskStore, KeSkStore> message::ContentUnwrap<F, LStore>
for ContentUnwrap<'a, F, Link, PskStore, KeSkStore>
where
F: PRP + Clone,
Link: HasLink,
Link::Rel: Eq + Default + SkipFallback<F>,
LStore: LinkStore<F, Link::Rel>,
PskStore: for<'c> Lookup<&'c Identifier, psk::Psk> + Send + Sync,
KeSkStore: for<'c> Lookup<&'c Identifier, &'b x25519::StaticSecret> + 'b + Send + Sync,
PskStore: for<'c> Lookup<&'c Identifier, psk::Psk>,
KeSkStore: for<'c> Lookup<&'c Identifier, &'b x25519::StaticSecret> + 'b,
{
async fn unwrap<'c, IS: io::IStream>(
&mut self,
Expand Down
6 changes: 3 additions & 3 deletions iota-streams-app-channels/src/message/sequence.rs
Expand Up @@ -55,7 +55,7 @@ where
pub(crate) ref_link: &'a <Link as HasLink>::Rel,
}

#[async_trait]
#[async_trait(?Send)]
impl<'a, F, Link> message::ContentSizeof<F> for ContentWrap<'a, Link>
where
F: PRP,
Expand All @@ -73,7 +73,7 @@ where
}
}

#[async_trait]
#[async_trait(?Send)]
impl<'a, F, Link, Store> message::ContentWrap<F, Store> for ContentWrap<'a, Link>
where
F: PRP,
Expand Down Expand Up @@ -117,7 +117,7 @@ where
}
}

#[async_trait]
#[async_trait(?Send)]
impl<F, Link, Store> message::ContentUnwrap<F, Store> for ContentUnwrap<Link>
where
F: PRP,
Expand Down
6 changes: 3 additions & 3 deletions iota-streams-app-channels/src/message/signed_packet.rs
Expand Up @@ -61,7 +61,7 @@ where
pub(crate) _phantom: core::marker::PhantomData<(F, Link)>,
}

#[async_trait]
#[async_trait(?Send)]
impl<'a, F, Link> message::ContentSizeof<F> for ContentWrap<'a, F, Link>
where
F: PRP,
Expand All @@ -80,7 +80,7 @@ where
}
}

#[async_trait]
#[async_trait(?Send)]
impl<'a, F, Link, Store> message::ContentWrap<F, Store> for ContentWrap<'a, F, Link>
where
F: PRP,
Expand Down Expand Up @@ -126,7 +126,7 @@ where
}
}

#[async_trait]
#[async_trait(?Send)]
impl<F, Link, Store> message::ContentUnwrap<F, Store> for ContentUnwrap<F, Link>
where
F: PRP,
Expand Down
6 changes: 3 additions & 3 deletions iota-streams-app-channels/src/message/subscribe.rs
Expand Up @@ -74,7 +74,7 @@ pub struct ContentWrap<'a, F, Link: HasLink> {
pub(crate) _phantom: core::marker::PhantomData<(Link, F)>,
}

#[async_trait]
#[async_trait(?Send)]
impl<'a, F, Link> message::ContentSizeof<F> for ContentWrap<'a, F, Link>
where
F: PRP,
Expand All @@ -91,7 +91,7 @@ where
}
}

#[async_trait]
#[async_trait(?Send)]
impl<'a, F, Link, Store> message::ContentWrap<F, Store> for ContentWrap<'a, F, Link>
where
F: PRP,
Expand Down Expand Up @@ -140,7 +140,7 @@ where
}
}

#[async_trait]
#[async_trait(?Send)]
impl<'a, F, Link, Store> message::ContentUnwrap<F, Store> for ContentUnwrap<'a, F, Link>
where
F: PRP,
Expand Down
6 changes: 3 additions & 3 deletions iota-streams-app-channels/src/message/tagged_packet.rs
Expand Up @@ -61,7 +61,7 @@ where
pub(crate) _phantom: core::marker::PhantomData<(F, Link)>,
}

#[async_trait]
#[async_trait(?Send)]
impl<'a, F, Link> message::ContentSizeof<F> for ContentWrap<'a, F, Link>
where
F: PRP,
Expand All @@ -81,7 +81,7 @@ where
}
}

#[async_trait]
#[async_trait(?Send)]
impl<'a, F, Link, Store> message::ContentWrap<F, Store> for ContentWrap<'a, F, Link>
where
F: PRP,
Expand Down Expand Up @@ -126,7 +126,7 @@ where
}
}

#[async_trait]
#[async_trait(?Send)]
impl<F, Link, Store> message::ContentUnwrap<F, Store> for ContentUnwrap<F, Link>
where
F: PRP,
Expand Down
14 changes: 7 additions & 7 deletions iota-streams-app/src/identifier.rs
Expand Up @@ -78,7 +78,7 @@ impl From<PskId> for Identifier {
}
}

#[async_trait]
#[async_trait(?Send)]
impl<F: PRP> ContentSizeof<F> for Identifier {
async fn sizeof<'c>(&self, ctx: &'c mut sizeof::Context<F>) -> Result<&'c mut sizeof::Context<F>> {
match *self {
Expand All @@ -96,8 +96,8 @@ impl<F: PRP> ContentSizeof<F> for Identifier {
}
}

#[async_trait]
impl<F: PRP, Store: Send + Sync> ContentWrap<F, Store> for Identifier {
#[async_trait(?Send)]
impl<F: PRP, Store> ContentWrap<F, Store> for Identifier {
async fn wrap<'c, OS: io::OStream>(
&self,
_store: &Store,
Expand All @@ -118,8 +118,8 @@ impl<F: PRP, Store: Send + Sync> ContentWrap<F, Store> for Identifier {
}
}

#[async_trait]
impl<F: PRP, Store: Send + Sync> ContentUnwrap<F, Store> for Identifier {
#[async_trait(?Send)]
impl<F: PRP, Store> ContentUnwrap<F, Store> for Identifier {
async fn unwrap<'c, IS: io::IStream>(
&mut self,
_store: &'c Store,
Expand All @@ -131,8 +131,8 @@ impl<F: PRP, Store: Send + Sync> ContentUnwrap<F, Store> for Identifier {
}
}

#[async_trait]
impl<F: PRP, Store: Send + Sync> ContentUnwrapNew<F, Store> for Identifier {
#[async_trait(?Send)]
impl<F: PRP, Store> ContentUnwrapNew<F, Store> for Identifier {
async fn unwrap_new<'c, IS: io::IStream>(
_store: &Store,
ctx: &'c mut unwrap::Context<F, IS>,
Expand Down
14 changes: 7 additions & 7 deletions iota-streams-app/src/message/content.rs
Expand Up @@ -13,12 +13,12 @@ use iota_streams_ddml::{
io,
};

#[async_trait]
pub trait ContentSizeof<F>: Send + Sync {
#[async_trait(?Send)]
pub trait ContentSizeof<F> {
async fn sizeof<'c>(&self, ctx: &'c mut sizeof::Context<F>) -> Result<&'c mut sizeof::Context<F>>;
}

#[async_trait]
#[async_trait(?Send)]
pub trait ContentWrap<F, Store>: ContentSizeof<F> {
async fn wrap<'c, OS: io::OStream>(
&self,
Expand All @@ -27,17 +27,17 @@ pub trait ContentWrap<F, Store>: ContentSizeof<F> {
) -> Result<&'c mut wrap::Context<F, OS>>;
}

#[async_trait]
pub trait ContentUnwrap<F, Store>: Send + Sync {
#[async_trait(?Send)]
pub trait ContentUnwrap<F, Store> {
async fn unwrap<'c, IS: io::IStream>(
&mut self,
store: &'c Store,
ctx: &'c mut unwrap::Context<F, IS>,
) -> Result<&'c mut unwrap::Context<F, IS>>;
}

#[async_trait]
pub trait ContentUnwrapNew<F, Store>: Send + Sync
#[async_trait(?Send)]
pub trait ContentUnwrapNew<F, Store>
where
Self: Sized,
{
Expand Down
8 changes: 3 additions & 5 deletions iota-streams-app/src/message/hdf.rs
Expand Up @@ -180,7 +180,7 @@ where
}
}

#[async_trait]
#[async_trait(?Send)]
impl<F, Link> ContentSizeof<F> for HDF<Link>
where
F: PRP,
Expand All @@ -205,12 +205,11 @@ where
}
}

#[async_trait]
#[async_trait(?Send)]
impl<F, Link, Store> ContentWrap<F, Store> for HDF<Link>
where
F: PRP,
Link: AbsorbExternalFallback<F>,
Store: Send + Sync,
{
async fn wrap<'c, OS: io::OStream>(
&self,
Expand Down Expand Up @@ -250,12 +249,11 @@ where
}
}

#[async_trait]
#[async_trait(?Send)]
impl<F, Link, Store> ContentUnwrap<F, Store> for HDF<Link>
where
F: PRP,
Link: AbsorbExternalFallback<F> + std::fmt::Debug + Clone,
Store: Send + Sync,
{
async fn unwrap<'c, IS: io::IStream>(
&mut self,
Expand Down
6 changes: 3 additions & 3 deletions iota-streams-app/src/message/link.rs
Expand Up @@ -11,15 +11,15 @@ use crate::identifier::Identifier;
use iota_streams_ddml::types::Bytes;

/// Type of "absolute" links. For http it's the absolute URL.
pub trait HasLink: Sized + Default + Clone + Eq + Send + Sync {
pub trait HasLink: Sized + Default + Clone + Eq {
/// Type of "base" links. For http it's domain name.
type Base: Default + Clone;

/// Get base part of the link.
fn base(&self) -> &Self::Base;

/// Type of "relative" links. For http it's URL path.
type Rel: Default + Clone + Send + Sync;
type Rel: Default + Clone;

/// Get relative part of the link.
fn rel(&self) -> &Self::Rel;
Expand Down Expand Up @@ -109,7 +109,7 @@ impl<Link: fmt::Debug> fmt::Debug for Cursor<Link> {
}

/// Abstraction-helper to generate message links.
pub trait LinkGenerator<Link: HasLink>: Default + Send + Sync {
pub trait LinkGenerator<Link: HasLink>: Default {
/// Used by Author to generate a new application instance: channels address and announcement message identifier
fn gen(&mut self, pk: &ed25519::PublicKey, idx: u64);

Expand Down

0 comments on commit 6f9e3bd

Please sign in to comment.