Skip to content

Implement Send and Sync for predicates that have PhantomData in them #108

@taminomara

Description

@taminomara

Predicates such as FnPredicate use PhantomData<T> to bypass the requirement that all generic parameters must be used. This makes the compiler think that predicate owns an instance of T, leading to two effects:

  • if T is bound by lifetime 'a, then the predicate will also be bound by 'a,
  • if T is not Send or Sync, the predicate will also not be Send or Sync.

There's nothing we can do about the first one. The second one, however, can be remedied by manually implementing Send and Sync.

For FnPredicate it will look like this:

unsafe impl<F, T> Send for FnPredicate<F, T>
    where
        F: Fn(&T) -> bool + Send,
        T: ?Sized,
{
}

unsafe impl<F, T> Sync for FnPredicate<F, T>
    where
        F: Fn(&T) -> bool + Sync,
        T: ?Sized,
{
}

This change will add unsafe to the crate. The benefit is that predicates will be easier to use in multi-threaded environments.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions