Hello all.
I use third-party library which has struct Foo
constructed from closure:
pub struct Foo<T> { t: T }
impl<'a, T> Foo<T> where T: 'a + FnMut() -> &'a [u8] {
pub fn from_callback(closure: T) -> Self {
…
}
}
If I understand correctly, it means that returned slice lives as long as closure itself.
I can use it in my code:
fn main() {
let buf = vec![0; 100];
let foo = Foo::from_callback(|| { &buf });
}
but I want to return it from function. Since abstract return types aren't implemented yet (sadly), I can't use closure, so I have decided to use struct which implements FnMut:
#![feature(unboxed_closures)]
pub struct Func {
buffer: Vec<u8>,
}
impl FnMut<()> for Func {
extern "rust-call" fn call_mut(&mut self, _: ()) -> Self::Output {
&self.buffer
}
}
fn create_foo() -> Foo<Func> {
Foo::from_callback(Func { buffer: vec![0; 100] })
}
fn main() {
let foo = create_foo();
}
this gives me:
error: the trait `core::ops::FnOnce<()>` is not implemented for the type `Func` [E0277]
ok, lets implement FnOnce:
impl FnOnce<()> for Func {
type Output = &[u8];
^~~~ error: missing lifetime specifier [E0106]
extern "rust-call" fn call_once(self, _: ()) -> Self::Output {
unimplemented!();
}
}
Since buffer lifetime must depend on closure lifetime, I expected something like this to work:
impl<'a> FnOnce<()> for Func where Func: 'a {
^~~~~ error: the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates
type Output = &'a [u8];
extern "rust-call" fn call_once(self, _: ()) -> Self::Output {
unimplemented!();
}
}
but it doesn't.
Please help me to implement FnMut.