Skip to content

Commit 90f24bd

Browse files
authored
Merge pull request #1210 from RealKC/bool-error-fmt-variable-expansion
glib: Allow variable expansion in format strings passed to bool_error & result_from_gboolean
2 parents d1fdc2e + d2fc503 commit 90f24bd

File tree

1 file changed

+42
-36
lines changed

1 file changed

+42
-36
lines changed

glib/src/error.rs

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -152,49 +152,55 @@ pub trait ErrorDomain: Copy {
152152
/// Generic error used for functions that fail without any further information
153153
#[macro_export]
154154
macro_rules! bool_error(
155-
// Plain strings
156-
($msg:expr) => {{
157-
$crate::BoolError::new(
158-
$msg,
159-
file!(),
160-
$crate::function_name!(),
161-
line!(),
162-
)
163-
}};
164-
165-
// Format strings
166155
($($msg:tt)*) => {{
167-
$crate::BoolError::new(
168-
format!($($msg)*),
169-
file!(),
170-
$crate::function_name!(),
171-
line!(),
172-
)
156+
match ::std::format_args!($($msg)*) {
157+
formatted => {
158+
if let Some(s) = formatted.as_str() {
159+
$crate::BoolError::new(
160+
s,
161+
file!(),
162+
$crate::function_name!(),
163+
line!()
164+
)
165+
} else {
166+
$crate::BoolError::new(
167+
formatted.to_string(),
168+
file!(),
169+
$crate::function_name!(),
170+
line!(),
171+
)
172+
}
173+
}
174+
}
173175
}};
174176
);
175177

176178
#[macro_export]
177179
macro_rules! result_from_gboolean(
178-
// Plain strings
179-
($ffi_bool:expr, $msg:expr) => {{
180-
$crate::BoolError::from_glib(
181-
$ffi_bool,
182-
$msg,
183-
file!(),
184-
$crate::function_name!(),
185-
line!(),
186-
)
187-
}};
188-
189-
// Format strings
190180
($ffi_bool:expr, $($msg:tt)*) => {{
191-
$crate::BoolError::from_glib(
192-
$ffi_bool,
193-
format!($($msg)*),
194-
file!(),
195-
$crate::function_name!(),
196-
line!(),
197-
)
181+
match ::std::format_args!($($msg)*) {
182+
formatted => {
183+
if let Some(s) = formatted.as_str() {
184+
$crate::BoolError::from_glib(
185+
$ffi_bool,
186+
s,
187+
file!(),
188+
$crate::function_name!(),
189+
line!(),
190+
)
191+
} else {
192+
$crate::BoolError::from_glib(
193+
$ffi_bool,
194+
formatted.to_string(),
195+
file!(),
196+
$crate::function_name!(),
197+
line!(),
198+
)
199+
}
200+
}
201+
}
202+
203+
198204
}};
199205
);
200206

0 commit comments

Comments
 (0)