sorry, this memo is in pretty bad shape ..
generalize prints over std::str::pattern::Pattern (like in String::contains) https://doc.rust-lang.org/std/str/pattern/trait.Pattern.html
Currently the Pattern is only implemented for String
and str
and is
not yet stabilized (#27721)
for 3rd party implementations. But once it is stabilized, you would have
regex support and the like for free. :-)
I'm not sure if Rust would accept
fn prints<T, O>(self, output: O) -> Self where
O: Into<T>,
T: Pattern
That would be ambiguous in cases where a type X
implements Into<A>
and
Into<B>
, where A: Pattern, B: Pattern
. You would then probably need to
disambiguate prints(x as Into<A>)
or prints::<A, _>(x)
. Even if Rust
accepted it, it would introduce some forward compatibility hazards, where
adding impls X: Into<B>
and B: Pattern
would make code in other places
suddenly be ambiguous.
So my conclusion is, it should be either prints<O: Into<String>>
or
prints<O: Pattern>
.
In the spirit of the above patterns, might be assertions like this:
- print_starts_with
- print_ends_with
@killercup during the review I also made these memos.
The first is only pseudo-Rust, but might be a possible direction, if you want to generalize the foundation. I don't claim that this would be better or a good design at all. I do like the current simple API. I just wanted to share these ideas instead of just trashing it. :-)
The second is about possibly generalizing std::str::pattern - although it's not yet stabilised yet and only implemented for
String
, which in fact wouldn't add much value now, but maybe in the future.