This is an automated email from the ASF dual-hosted git repository.
tison pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datasketches-rust.git
The following commit(s) were added to refs/heads/main by this push:
new 493132c refactor: remove unnecessary allocations in error formatting
(#79)
493132c is described below
commit 493132cd8f435bd43c8bad7b6859babe5e75c6ad
Author: Cheng-Yang Chou <[email protected]>
AuthorDate: Thu Jan 29 11:19:18 2026 +0800
refactor: remove unnecessary allocations in error formatting (#79)
Signed-off-by: Cheng-Yang Chou <[email protected]>
---
datasketches/src/error.rs | 50 ++++++++++++++++++++++++++++++++++++++---------
1 file changed, 41 insertions(+), 9 deletions(-)
diff --git a/datasketches/src/error.rs b/datasketches/src/error.rs
index 31559d3..c5a32cb 100644
--- a/datasketches/src/error.rs
+++ b/datasketches/src/error.rs
@@ -161,15 +161,12 @@ impl fmt::Display for Error {
if !self.context.is_empty() {
write!(f, ", context: {{ ")?;
- write!(
- f,
- "{}",
- self.context
- .iter()
- .map(|(k, v)| format!("{k}: {v}"))
- .collect::<Vec<_>>()
- .join(", ")
- )?;
+ for (i, (k, v)) in self.context.iter().enumerate() {
+ if i > 0 {
+ write!(f, ", ")?;
+ }
+ write!(f, "{}: {}", k, v)?;
+ }
write!(f, " }}")?;
}
@@ -182,3 +179,38 @@ impl fmt::Display for Error {
}
impl std::error::Error for Error {}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_format_consistency() {
+ // Case 1: Test basic error message
+ let err = Error::new(ErrorKind::InvalidArgument, "something went
wrong");
+ assert_eq!(
+ format!("{}", err),
+ "InvalidArgument => something went wrong",
+ "Basic error formatting mismatch"
+ );
+ }
+
+ #[test]
+ fn test_format_with_multiple_contexts() {
+ // Case 2: Test with multiple contexts
+ // This validates the comma separation logic which is prone to
regression
+ let err = Error::new(ErrorKind::InvalidData, "parsing failed")
+ .with_context("index", 42)
+ .with_context("file", "foo");
+
+ let output = format!("{}", err);
+
+ // Expected full string
+ let expected = "InvalidData, context: { index: 42, file: foo } =>
parsing failed";
+
+ assert_eq!(
+ output, expected,
+ "Formatted output with context does not match expectation"
+ );
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]