Skip to content

Commit

Permalink
glib: Allow variable expansion in fmt strings passed to `result_from_…
Browse files Browse the repository at this point in the history
…gboolean`
  • Loading branch information
RealKC committed Oct 21, 2023
1 parent 378180a commit d2fc503
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions glib/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,26 +177,30 @@ macro_rules! bool_error(

#[macro_export]
macro_rules! result_from_gboolean(
// Plain strings
($ffi_bool:expr, $msg:expr) => {{
$crate::BoolError::from_glib(
$ffi_bool,
$msg,
file!(),
$crate::function_name!(),
line!(),
)
}};

// Format strings
($ffi_bool:expr, $($msg:tt)*) => {{
$crate::BoolError::from_glib(
$ffi_bool,
format!($($msg)*),
file!(),
$crate::function_name!(),
line!(),
)
match ::std::format_args!($($msg)*) {
formatted => {
if let Some(s) = formatted.as_str() {
$crate::BoolError::from_glib(
$ffi_bool,
s,
file!(),
$crate::function_name!(),
line!(),
)
} else {
$crate::BoolError::from_glib(
$ffi_bool,
formatted.to_string(),
file!(),
$crate::function_name!(),
line!(),
)
}
}
}


}};
);

Expand Down

0 comments on commit d2fc503

Please sign in to comment.