-
Notifications
You must be signed in to change notification settings - Fork 29
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
API doesn't work well with distinct owned / ref types #20
Comments
Commented in #18 but makes sense to add here. The |
In looking at this, I also might need to change the Predicate trait. In researching, it seems the recommendation is to use associative types for return types (don't need to mention type on every call) and generics for parameters (allows multiple trait implementations per struct). |
Ok, got a branch with
So remaining areas to explore
|
The hope is that generics will also make implementing support for assert-rs#20 easier. So when to use each according to [The Book](https://doc.rust-lang.org/book/second-edition/ch19-03-advanced-traits.html#associated-types-versus-generics): Generics - Used when type is going to be function input - Can have multiple implementations of trait Associated types - Used when type is going to be a function output, like iterator - Avoids need to annotate type - Can only have one implementation of trait BREAKING CHANGE: The `Predicate` trait's API has changed.
You can see my Ran into life time issues of all things. |
I tried just limited myself to modifying Example of issues I ran into
https://github.com/epage/predicates-rs/tree/borrow_borrow I also tried |
Had some time to evaluate this. The work for #29 is probably the best we'll get. |
So while we need to address |
#49 is another step towards addressing this |
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.
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.
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.
EqPredicate
storesT
and itseval
takes a&T
. But what aboutVec
/slice
,String
/str
, andPathBuf
/path
?Exploration Summary
After making
Predicate
use generics rather than associated types (see #29)Dead ends
Predicate { eval<I: AsRef<Item>>(&self, item: I) }
: not object-safe which means we can'tBox<Predicate>
or&Predicate
.Predicate { eval(&self, item: Item) }
(ieItem=&str
rather thanItem=str
): violates borrow checker in boolean predicatesItem: Copy
: Lifetime issues (branch)impl Predicate<AsRef<T>> for EqPredicate<T>
: No AsRef implemented for numeric typesimpl Predicate<Borrow<T>> for EqPredicate<T>
:Sized
issues (branch)Options:
AsRef
adapterThe text was updated successfully, but these errors were encountered: