Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow predicates to own object and evaluate against borrowed types #134

Merged
merged 2 commits into from Dec 29, 2022

Conversation

rshearman
Copy link
Contributor

It is very useful to be able to dynamically construct an object and have that object owned by the predicate, yet evaluate against an unowned type related to the owned one. An obvious example is a String being owned by the predicate but being compared against &strs.

Therefore, implement Predicate for Eq/OrdPredicate that store an object that implements Borrow for the predicate type, replacing existing impls of Predicate for Eq/OrdPredicate and Eq/OrdPredicate<&T>. This is backwards compatible as there are blanket implementations of Borrow for T and Borrow for &T.

Note that Borrow imposes more requirements than are actually required and AsRef would be sufficient. However, AsRef doesn't have a blanket implementation for T and thus the existing impl of Predicate for EqPredicate is still required, but results in a conflict since T may also implement AsRef. Requiring Borrow instead of AsRef is sufficient for common use cases though.

This addresses #20 more completely.

src/ord.rs Outdated Show resolved Hide resolved
src/ord.rs Outdated
}
}

fn find_case<'b>(&'b self, expected: bool, variable: &T) -> Option<reflection::Case<'b>> {
fn find_case<'a>(&'a self, expected: bool, variable: &T) -> Option<reflection::Case<'a>> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're splitting things out into separate commits, please split this one oput

src/ord.rs Outdated Show resolved Hide resolved
It is very useful to be able to dynamically construct an object and
have that object owned by the predicate, yet evaluate against an
unowned type related to the owned one. An obvious example is a String
being owned by the predicate but being compared against &strs.

Therefore, implement Predicate for Eq/OrdPredicate that store an
object that implements Borrow for the predicate type, replacing
existing impls of Predicate<T> for Eq/OrdPredicate<T> and
Eq/OrdPredicate<&T>. This is backwards compatible as there are blanket
implementations of Borrow<T> for T and Borrow<T> for &T.

Note that Borrow imposes more requirements than are actually required
and AsRef would be sufficient. However, AsRef doesn't have a blanket
implementation for T and thus the existing impl of Predicate<T> for
EqPredicate<T> is still required, but results in a conflict since T
may also implement AsRef<T>. Requiring Borrow instead of AsRef is
sufficient for common use cases though.

This addresses assert-rs#20 more completely.
…ypes

It is very useful to be able to dynamically construct an object and
have that object owned by the predicate, yet evaluate against an
unowned type related to the owned one. An obvious example is a String
being owned by the predicate but being compared against &strs.

Therefore, implement Predicate for In/OrdIn/HashInPredicate that store
an object that implements Borrow for the predicate type, replacing
existing impls of Predicate<T> for In/OrdIn/HashInPredicate<T> and
In/OrdIn/HashInPredicate<&T>. This is backwards compatible as there
are blanket implementations of Borrow<T> for T and Borrow<T> for &T.

Note that Borrow imposes more requirements than are actually required
and AsRef would be sufficient. However, AsRef doesn't have a blanket
implementation for T and thus the existing impl of Predicate<T> for
InPredicate<T> is still required, but results in a conflict since T
may also implement AsRef<T>. Requiring Borrow instead of AsRef is
sufficient for common use cases though.

This addresses assert-rs#20 more completely.
@epage epage merged commit 4e1d03c into assert-rs:master Dec 29, 2022
@rshearman rshearman deleted the owned-ord branch December 29, 2022 17:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants