assert-rs / predicates-rs

Boolean-valued predicate functions in Rust
docs.rs/predicates
Apache License 2.0
173 stars 29 forks source link

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

Closed taminomara closed 3 years ago

taminomara commented 3 years ago

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:

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.

epage commented 3 years ago

Seems reasonable! Be willing to prep a PR for this?

taminomara commented 3 years ago

Sure, I'll try to send it later this week.