Skip to content

You can't actually impl !Send or impl !Sync (yet) #497

@kbolino

Description

@kbolino

The page on "Send and Sync" [1] says if you need a type T to be not-Send and/or not-Sync (when it isn't automatically so) you can just negative-implement the trait(s) as follows:

impl !Send for T {}
impl !Sync for T {}

However, no you can't. On the stable toolchain, you will get the following error:

error[E0658]: negative trait bounds are not fully implemented; use marker types for now
...
   = note: see issue #68318 <https://github.com/rust-lang/rust/issues/68318> for more information

It seems that the "correct" solution for now is to add a PhantomData<S> field where S is some other type which lacks the undesired trait(s) [2].

1: https://doc.rust-lang.org/stable/nomicon/send-and-sync.html
2: https://stackoverflow.com/q/62713667/814422

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions