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