From the documentation for std::marker::Sync:
As one would expect, primitive types like u8 and f64 are all Sync, and so are simple aggregate types containing them
This means that any struct you implement that contains only fields which are Sync
will automatically be Sync
. To get a little more of a guarantee that a struct is Sync
when you start doing things with it, there are a couple things you can do.
First of all, if your struct doesn't contain nothing but types that are obviously Sync
, like the types mentioned in the docs there- for instance, if you have a generic parameter- you can use trait constraints like so:
struct<T: SomeTrait + Sync> MyStruct {
some_thing: T
}
This way, even if you don't know right away what T
is, you have specified that it must be Sync
in any case.
Now, secondly, you can write functions to require its argument(s) be Sync
also by using trait constraints.
fn do_a_thing<T: Sync>(thing: T) {
// ... do something ...
}
Note that, in this case, the function do_a_thing
would accept any argument that is Sync
, so you'd probably want to also constrain T
further by requiring it satisfy some other traits.
If all of this feels like too much work to make this requirement explicit, I'd suggest just relying on the fact that the compiler will make your struct Sync
as long as all of its contents are Sync
. You can safely rely on it to do that and, if you ever tried to use the struct in a way that violates Sync
, it will tell you.