Replacing async-trait with dynosaur and Box<dyn MyTrait>>

Hi
,
I’m trying to modernise my code and get rid of async-trait.

I had a trait with async fn obviously and a struct with a problematic field:

struct S {
    pub b: Box<dyn T + Send + Sync>
}

And using dynosaur’s rule of replacing dyn T by DynT doesn’t do it here as it is a type, not a trait

How do we do in this case?

Would you mind expanding on your example a bit? I'm having trouble understanding your problem and how it relates to storing Box<DynT> in a struct—constructing DynT from something that implements T via DynT's interface seems like it should do the trick?
Also I'm not sure whether I'd consider replacing async-trait with dynosaur a modernisation. Both crates are well maintained and it is not immediately clear to me what benefits the latter has over the former.

Ok I think I was just out of caffeine, I need to get rid of the Box and use DynT directly, I think

No, DynT is unsized, same as dyn T, you need to use it behind a reference type. But you can create Box<DynT> from something that implements T via DynT::boxed.

Indeed for the unsize part.

I understand that I can construct a Box<DynT> but there’s no way to translate my field type using DynT then?

it’s the + Send + Sync that trigger the error, maybe I need to use trait-variant?

In dynosaur's documentation it has an example of how to use it with the trait-variant macro, but it's not clear if you can do this instead:

#[dynosaur::dynosaur(DynMyTrait = dyn MyTrait + Send + Sync)]

That's not possible, the dynosaur trait only parses StructName[ = TraitName] as its attribute, not StructName[ = TraitName[ + Bounds]].[1] AFAICT, OP could either use trait-variant or make Send and Sync supertraits of T manually, if supporting !Send + !Sync implementors is not necessary.


  1. The square brackets should indicate optional content. ↩︎

2 Likes