Can I declare a struct as "send"?

I'd like to require that a structure have the "Send" trait, so that, if something down within it isn't Send (probably an Rc), that causes a compile error when the structure is compiled, not when a user of the library tries to pass it through a spawn move.

pub struct foo: Send { ... }

would make sense, but isn't allowed. Is there syntax to do this, or is it not supported.

You could do something like this:

#[allow_unused]
fn _assert_foo_send(x:&foo) -> impl Send { x }
1 Like

The static_assertions crate offers an assert_impl! macro for exactly this.

2 Likes

Or...

struct AssertSend<T: Send>(T);
impl AssertSend<Foo> {}
2 Likes

In Tokio, we test these things like this: tests/async_send_sync.rs

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.