avoid more intermediate allocations in validation errors

This commit is contained in:
Ralf Jung 2019-12-12 16:48:46 +01:00
parent 3ddc0278d3
commit 14b2436993

View File

@ -22,28 +22,23 @@
macro_rules! throw_validation_failure {
($what:expr, $where:expr, $details:expr) => {{
let where_ = path_format(&$where);
let where_ = if where_.is_empty() {
String::new()
} else {
format!(" at {}", where_)
};
throw_unsup!(ValidationFailure(format!(
"encountered {}{}, but expected {}",
$what, where_, $details,
)))
let mut msg = format!("encountered {}", $what);
let where_ = &$where;
if !where_.is_empty() {
msg.push_str(" at ");
write_path(&mut msg, where_);
}
write!(&mut msg, ", but expected {}", $details).unwrap();
throw_unsup!(ValidationFailure(msg))
}};
($what:expr, $where:expr) => {{
let where_ = path_format(&$where);
let where_ = if where_.is_empty() {
String::new()
} else {
format!(" at {}", where_)
};
throw_unsup!(ValidationFailure(format!(
"encountered {}{}",
$what, where_,
)))
let mut msg = format!("encountered {}", $what);
let where_ = &$where;
if !where_.is_empty() {
msg.push_str(" at ");
write_path(&mut msg, where_);
}
throw_unsup!(ValidationFailure(msg))
}};
}
@ -113,10 +108,9 @@ pub fn track(&mut self, op: T, path: impl FnOnce() -> PATH) {
}
/// Format a path
fn path_format(path: &Vec<PathElem>) -> String {
fn write_path(out: &mut String, path: &Vec<PathElem>) {
use self::PathElem::*;
let mut out = String::new();
for elem in path.iter() {
match elem {
Field(name) => write!(out, ".{}", name),
@ -135,7 +129,6 @@ fn path_format(path: &Vec<PathElem>) -> String {
DynDowncast => write!(out, ".<dyn-downcast>"),
}.unwrap()
}
out
}
// Test if a range that wraps at overflow contains `test`