Hi!
There's a really useful crate for logging: GitHub - elichai/log-derive: A procedural macro for auto logging output of functions
What it does is it takes a fn
and places its body into a closure and then logs closure value in case of an error:
#[logfn(INFO)]
fn wrapped_function() -> Result<(), String> {
if other() {
return Ok()
} else {
return Err("some error".to_string())
}
something_else()?;
Ok(())
}
it expands (roughly) to
fn wrapped_function() -> Result<(), String> {
let result = (||{
if other() {
return Ok()
} else {
return Err("some error".to_string())
}
something_else()?;
Ok(())
})();
if let Err(e) = result {
error!("{}", e);
}
result
}
The problem is that it does not work well with async/await because async closures are not stable. How would you wrap this function to allow the same functionality but with support for async?
I thought about creating another fn
on the same level as orginal but it does not work for traits. If I put generated fn
into the original then I get problems with self
i.e. you cannot define method inside the method.
Any thoughts how to accomplish this?