Open JohnScience opened 2 weeks ago
I can do that for a concrete type:
mod workaround {
pub struct SelfBorrowingClosureReturningStdFmtArguments<
State,
F : Fn(&'_ State) -> std::fmt::Arguments<'_>
> {
pub state: State,
pub closure: F,
}
impl<State, F> SelfBorrowingClosureReturningStdFmtArguments<State, F>
where
F : Fn(&'_ State) -> std::fmt::Arguments<'_>,
{
pub fn new(state: State, closure: F) -> Self {
Self { state, closure }
}
pub fn call (self: &'_ SelfBorrowingClosureReturningStdFmtArguments<State, F>)
-> std::fmt::Arguments<'_>
{
(self.closure)(&self.state)
}
}
}
However, even my adhoc workaround didn't help me:
I ended up creating a simple lib for what I wanted to do: https://crates.io/crates/display_with
I was trying to implement a generic
SelfBorrowingClosure
based on https://users.rust-lang.org/t/why-closure-cannot-return-a-reference-to-data-moved-into-closure/72655/6 but got a problem compiling the following piece of code:Minimal Reproducible Example (MRE):
Error: