Hi. A function returns a closure to another function:
use std::borrow::Cow;
fn encoding_override_bytes<'r, 'name: 'r, 'value: 'r>(
name: &'name [u8],
value: &'value [u8],
) -> impl Fn(&str) -> Cow<'r, [u8]> {
|a| if a.is_empty() { name.into() } else { value.into() }
}
fn main() {
let mut serializer = form_urlencoded::Serializer::new(String::new());
let f = encoding_override_bytes(b"name=\xFF", b"value");
dbg!(serializer
.encoding_override(Some(&f))
.append_pair("", "0")
.finish());
}
Compilation of the above program yields an error:
error[E0308]: mismatched types
--> src/main.rs:14:33
|
14 | .encoding_override(Some(&f))
| ^^ one type is more general than the other
|
= note: expected enum `Cow<'_, _>`
found enum `Cow<'_, _>`
Changing to encoding_override(Some(& move |a| f(a)))
make it compile. My question is not how to compile it, but what the result type of encoding_override_bytes
should be. move |a| f(a)
is just an eta-expansion of f
, so their types should be equal. The error message suggests that their types are different, but is unhelpful otherwise.
“Cargo.toml”:
[dependencies]
form_urlencoded = "1.2.1"