I'm trying to write a case-insensitive string type, and I'm having problems with match arms. It seems that match doesn't use PartialEq under the hood, but I couldn't find out if there's another trait that it uses. (Most results for "match" + "trait" are about matching on traits, which is not what I'm interested in here).
Here's a minimal example:
#[derive(Debug)]
pub struct CaseInsensitiveStr<'a>(&'a str);
impl<'a> CaseInsensitiveStr<'a> {
#[inline]
pub const fn new(s: &'a str) -> CaseInsensitiveStr {
CaseInsensitiveStr(s)
}
}
impl<'a, S: AsRef<str>> PartialEq<S> for CaseInsensitiveStr<'a> {
#[inline]
fn eq(&self, other: &S) -> bool {
self.0.eq_ignore_ascii_case(other.as_ref())
}
}
fn main() {
let a = CaseInsensitiveStr::new("FOO");
if a == "foo" {
println!("eq works");
}
match a {
"foo" => println!("matched"),
_ => println!("no match"),
};
}
which gives the following compiler error:
error[E0308]: mismatched types
--> src/main.rs:26:9
|
25 | match a {
| - this expression has type `CaseInsensitiveStr<'_>`
26 | "foo" => println!("matched"),
| ^^^^^ expected `CaseInsensitiveStr<'_>`, found `&str`
|
help: try wrapping the pattern in `CaseInsensitiveStr`
|
26 | CaseInsensitiveStr("foo") => println!("matched"),
| +++++++++++++++++++ +
help: you might have meant to use field `0` whose type is `&str`
|
25 | match a.0 {
| ++
I certainly don't want to use a.0 and CaseInsensitiveStr("foo") compiles but doesn't actually do the correct thing (I think it's just essentially doing a.0?).
CaseInsensitiveStr::new("foo") is also invalid here, which I suspect also means any kind of macro tricky in a plain match statement isn't going to work either.
Is there a trait that I'm missing, or is it just not possible to get match to like this?
match is, by definition, structural - it's not based on PartialEq or Eq directly.
Structural equality, in turn, is roughly "are the two things the same type, and if so, are they byte-for-byte the same apart from padding fields?". It's why this example using String and a "string constant" doesn't compile, either. You can't get match to do what you want.
As @farnz mentions, it's indeed impossible to control the pattern matching behavior in Rust (at least in stable Rust).
However, you can still use the PartialEq impl by using a guard, as follow:
match a {
x if x == "foo" => println!("matched"),
_ => println!("no match"),
}
Or more controversially, you can match () to treat each branch like if/else. But most people I've exposed to this pattern disliked it (I love it, I call the spine guard):
match () {
() if a == "foo" => println!("matched"),
() => println!("no match"),
}
get can't work with CaseInsensitiveStr being a newtype around &str. It'd have to be something like struct CaseInsensitiveString(String) which stores the lowercased data. Or get would have to return a String or such.
Thanks everyone for the help, lots of interesting leads for potential workarounds.
@quinedot@mordecai.brain.leak Yes, my actual implementation has a Cow for storing the lowercased string, but I was wanting to see how far I could get without actually paying that cost.