[tokio-util Codec] Making it Send and Sync again?

We have a custom Codec. For my Decoder I have a state buffer with a bunch of variables. An employee got an error while trying to use it in a new service the other day, and we tracked it down to the struct containing a writer: Option<Box<dyn Write>>. I don't recall the error, but it was something that implied it was crossing a future's bounds.

I hadn't noticed until earlier, but I noticed that our Codec indeed is !Send and !Sync, which makes sense. But what I'm not entirely sure about is how to make it Send and Sync again.

Just to make sure it was that member I tried simply commenting it out, and fixing the few errors that followed (minimal changes). This caused our Codec to become Send and Sync again.

So then I figured that I just need to slap an Arc<Mutex<...>> on it and I'd be set.

Well, needless to say (because I'm asking) that didn't help.

If my Decoder contains a Option<Box<dyn Write>> -- how would I go about regaining Send and Sync (assuming that this member is the one causing it to lose them).

Have you tried using Option<Box<dyn Write + Sync + Send>>?


I think I was just struck by a pretty major epiphany concerning trait bounds.

I now much better understand what chalk is meant to do.

The situation where a mutex can help is when you have an Send + !Sync type.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.