Attribute Macro ion_c_sys_macros::position_error
source · #[position_error]
Expand description
This macro is intended to support the IonCReaderHandle
type. When any
function returns an IonCError
, the error should be populated with the
current reader position.
Without this macro, errors could be decorated like so:
fn foo() -> IonCResult<()> {
fn1().map_err(|err| include_current_position(self.reader, err))?
// ...
Ok(())
}
However, functions can fail in many places, so decorating each of these with
map_err
is tedious and error prone:
fn foo() -> IonCResult<()> {
fn1()?.map_err(..);
fn2()?.map_err(..);
}
This macro rewrites the function such that any error gets the position
added. It does this by moving the existing implementation into a closure and
then calling map_err
on the result.
Note: to make the variety of possible functions work (which include mutable
borrows, lifetime elision), the closure uses the move
keyword. There are
some gymnastics (adapted from dtolnay/no-panic) to make that work out.