Hi,
I'm trying to get the code below to compile (rust playground link). I'm expecting that the borrow checker could match the local lifetime 'de to the lifetime foo expects, but apparently I need to help it somehow.
use serde; // 1.0.124
use serde_json; // 1.0.64
use serde::Deserialize;
#[derive(Debug, Deserialize)]
struct Value<'a> {
v: &'a str
}
trait Handler<A> {
fn call(&self, a: A);
}
fn handler_fn<F>(f: F) -> HandlerFn<F> {
HandlerFn { f }
}
struct HandlerFn<F> {
f: F
}
impl<A, F> Handler<A> for HandlerFn<F>
where
F: Fn(A),
{
fn call(&self, a: A) {
(self.f)(a)
}
}
fn foo<'a>(d: Value<'a>) {
println!("{:?}", d);
}
fn call_foo<A, F>(f: F) -> Result<(), serde_json::Error>
where
A: for<'de> Deserialize<'de>,
F: Handler<A> + 'static,
{
{
let b = "{\"v\": \"x\"}".to_owned();
let x = serde_json::from_str(&b)?;
f.call(x);
}
Ok(())
}
fn main() {
let h = handler_fn(foo);
call_foo(h).unwrap();
}
This produces the error "implementation of Deserialize
is not general enough". I've tried to add some intermediary types, but was never able to get it to work and have Rust infer all the types. Could someone point me in the right direction?