I'm trying to DRY up a piece of code by using a closure, but I can't get the lifetime to work.
Here is a link to an example of what I'm trying to achieve: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=2b336614ef6648d00f38166e5ffd5c5c. This is a simplification, in the real code I cannot change the Buffer
struct. What I'm trying to get right is the write()
function:
fn write<'a, F, T>(buf: Buffer, write_fn: F, val: T)
where
F: FnOnce(Buffer<'a>, T)
{
buf.write_with_prefix(1, |buf| {
write_fn(buf, val); // How do I specify the lifetime of buf?
})
}