From 5cbabbdb37c6d4ba55b4bab1ff78c63f4c5b93b5 Mon Sep 17 00:00:00 2001 From: timofey Date: Fri, 16 Jun 2023 07:33:32 +0000 Subject: [PATCH] `write!` --- src/flow/binary/avl.rs | 8 +++---- src/flow/traversible/unbalanced.rs | 12 +++++----- src/rstd.rs | 2 +- src/rstd/atomic/au64.rs | 7 +++--- src/rstd/atomic/boolean.rs | 9 ++++---- src/rstd/cast.rs | 14 ++++++------ src/rstd/collections/avl.rs | 24 ++++++++++---------- src/rstd/collections/pair.rs | 14 +++++------- src/rstd/collections/stack.rs | 8 +++---- src/rstd/inlining.rs | 11 +++++----- src/rstd/point.rs | 7 +++--- src/rstd/tracing/rendered_display.rs | 32 +++++++++++++-------------- src/rstd/tracing/trace.rs | 33 ++++++++++++++-------------- src/rstd/typeless.rs | 2 +- src/testing.rs | 15 ++++++------- 15 files changed, 98 insertions(+), 100 deletions(-) diff --git a/src/flow/binary/avl.rs b/src/flow/binary/avl.rs index 73b94b2..0670839 100644 --- a/src/flow/binary/avl.rs +++ b/src/flow/binary/avl.rs @@ -13,7 +13,7 @@ struct AvlN { impl Display for AvlN { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_fmt(format_args!("({} {} {})", self.l, self.key, self.r)) + write!(f, "({} {} {})", self.l, self.key, self.r) } } @@ -39,7 +39,7 @@ impl Clone for AvlR { impl Display for AvlR { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_fmt(format_args!("{}", self.node)) + write!(f, "{}", self.node) } } @@ -67,8 +67,8 @@ impl Clone for AvlT { impl Display for AvlT { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match &self.reference { - Some(reference) => f.write_fmt(format_args!("{}", reference)), - None => f.write_fmt(format_args!("-")), + Some(reference) => write!(f, "{}", reference), + None => write!(f, "-"), } } } diff --git a/src/flow/traversible/unbalanced.rs b/src/flow/traversible/unbalanced.rs index ddac66f..65a2071 100644 --- a/src/flow/traversible/unbalanced.rs +++ b/src/flow/traversible/unbalanced.rs @@ -43,7 +43,7 @@ where UnbalancedReference<'a, T, A>: std::fmt::Display, { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_fmt(format_args!("({} {} {})", self.cl, self.key, self.cr)) + write!(f, "({} {} {})", self.cl, self.key, self.cr) } } @@ -53,15 +53,15 @@ where { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - UnbalancedTree::Leaf => f.write_fmt(format_args!(".")), - UnbalancedTree::Node(reference) => f.write_fmt(format_args!("{}", reference)), + UnbalancedTree::Leaf => write!(f, "."), + UnbalancedTree::Node(reference) => write!(f, "{}", reference), } } } impl<'a, A: 'a + Display> Display for UnbalancedReference<'a, instances::solo::SoloInstance, A> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_fmt(format_args!("{}", self.0())) + write!(f, "{}", self.0()) } } @@ -70,8 +70,8 @@ impl<'a, A: 'a + Display> Display { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self.0() { - Ok(node) => f.write_fmt(format_args!("{}", node)), - Err(()) => f.write_fmt(format_args!("~")), + Ok(node) => write!(f, "{}", node), + Err(()) => write!(f, "~"), } } } diff --git a/src/rstd.rs b/src/rstd.rs index 9c6a832..6036603 100644 --- a/src/rstd.rs +++ b/src/rstd.rs @@ -19,7 +19,7 @@ use crate::rcore::*; impl Display for Address { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_fmt(format_args!("{}@{}", hex::encode(self.point), self.index)) + write!(f, "{}@{}", hex::encode(self.point), self.index) } } diff --git a/src/rstd/atomic/au64.rs b/src/rstd/atomic/au64.rs index 52b0359..54826f4 100644 --- a/src/rstd/atomic/au64.rs +++ b/src/rstd/atomic/au64.rs @@ -17,10 +17,11 @@ pub enum IntParseError { impl Display for IntParseError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - Self::Eof => f.write_fmt(format_args!("encountered EOF write parsing an integer.")), - Self::ExtraData(length) => f.write_fmt(format_args!( + Self::Eof => write!(f, "encountered EOF write parsing an integer."), + Self::ExtraData(length) => write!( + f, "encountered extra data of length {length} while parsing an integer.", - )), + ), } } } diff --git a/src/rstd/atomic/boolean.rs b/src/rstd/atomic/boolean.rs index b491309..ebd4f26 100644 --- a/src/rstd/atomic/boolean.rs +++ b/src/rstd/atomic/boolean.rs @@ -22,12 +22,13 @@ impl Display for BooleanParseError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::OutOfBounds(value) => { - f.write_fmt(format_args!("boolean value out of bounds: {}.", value)) + write!(f, "boolean value out of bounds: {}.", value) } - Self::Eof => f.write_fmt(format_args!("encountered EOF write parsing a boolean.")), - Self::ExtraData(length) => f.write_fmt(format_args!( + Self::Eof => write!(f, "encountered EOF write parsing a boolean."), + Self::ExtraData(length) => write!( + f, "encountered extra data of length {length} while parsing a boolean.", - )), + ), } } } diff --git a/src/rstd/cast.rs b/src/rstd/cast.rs index 5281d9c..52a620c 100644 --- a/src/rstd/cast.rs +++ b/src/rstd/cast.rs @@ -41,22 +41,22 @@ impl<'a> Display for CastError<'a> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { CastError::Typeless(typeless_error) => { - f.write_fmt(format_args!("typeless cast error: {}", typeless_error)) + write!(f, "typeless cast error: {}", typeless_error) + } + CastError::AddressIndexOutOfBounds { index, length } => { + write!(f, "cast index out of bound: {}>={}", index, length) } - CastError::AddressIndexOutOfBounds { index, length } => f.write_fmt(format_args!( - "cast index out of bound: {}>={}", - index, length - )), CastError::AddressPointMismatch { expected, received, index, - } => f.write_fmt(format_args!( + } => write!( + f, "address mismatch at index {}: {}>={}", index, hex::encode(expected), hex::encode(received), - )), + ), } } } diff --git a/src/rstd/collections/avl.rs b/src/rstd/collections/avl.rs index 24c0a37..094abd2 100644 --- a/src/rstd/collections/avl.rs +++ b/src/rstd/collections/avl.rs @@ -39,23 +39,23 @@ impl Display for AvlError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::Int(int_error) => { - f.write_fmt(format_args!("failed to parse AVL tree height: {int_error}")) + write!(f, "failed to parse AVL tree height: {int_error}") + } + Self::Point(point_error) => { + write!(f, "failed to parse AVL node reference: {point_error}") } - Self::Point(point_error) => f.write_fmt(format_args!( - "failed to parse AVL node reference: {point_error}" - )), Self::Key(key_error) => { - f.write_fmt(format_args!("failed to parse AVL node key: {key_error}")) + write!(f, "failed to parse AVL node key: {key_error}") } - Self::NodeHeight => f.write_fmt(format_args!("invalid AVL non-leaf height: 0.")), + Self::NodeHeight => write!(f, "invalid AVL non-leaf height: 0."), Self::LeafHeight(height) => { - f.write_fmt(format_args!("invalid AVL leaf height: {height}!=0.")) + write!(f, "invalid AVL leaf height: {height}!=0.") + } + Self::Balance(hl, hr) => write!(f, "unbalanced AVL node: {hl} {hr}."), + Self::HeightOverflow => write!(f, "AVL tree height overflow"), + Self::HeightMismatch { child, parent } => { + write!(f, "AVL child-parent height mismatch: {child:?}, {parent}") } - Self::Balance(hl, hr) => f.write_fmt(format_args!("unbalanced AVL node: {hl} {hr}.")), - Self::HeightOverflow => f.write_fmt(format_args!("AVL tree height overflow")), - Self::HeightMismatch { child, parent } => f.write_fmt(format_args!( - "AVL child-parent height mismatch: {child:?}, {parent}" - )), } } } diff --git a/src/rstd/collections/pair.rs b/src/rstd/collections/pair.rs index d7572d8..3e9cbb7 100644 --- a/src/rstd/collections/pair.rs +++ b/src/rstd/collections/pair.rs @@ -37,14 +37,12 @@ pub enum PairParseError { impl Display for PairParseError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - PairParseError::A(error) => f.write_fmt(format_args!( - "error while parsing first element of a pair: {}", - error - )), - PairParseError::B(error) => f.write_fmt(format_args!( - "error while parsing first element of a pair: {}", - error - )), + PairParseError::A(error) => { + write!(f, "error while parsing first element of a pair: {}", error) + } + PairParseError::B(error) => { + write!(f, "error while parsing second element of a pair: {}", error) + } } } } diff --git a/src/rstd/collections/stack.rs b/src/rstd/collections/stack.rs index fd4ff48..deea1a4 100644 --- a/src/rstd/collections/stack.rs +++ b/src/rstd/collections/stack.rs @@ -57,11 +57,11 @@ pub enum StackParseError { impl Display for StackParseError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - StackParseError::Point(ppe) => { - f.write_fmt(format_args!("can't parse stack's next pointer: {}", ppe)) + Self::Point(ppe) => { + write!(f, "can't parse stack's next pointer: {}", ppe) } - StackParseError::Element(epe) => { - f.write_fmt(format_args!("can't parse stack's element: {}", epe)) + Self::Element(epe) => { + write!(f, "can't parse stack's element: {}", epe) } } } diff --git a/src/rstd/inlining.rs b/src/rstd/inlining.rs index 6d04d2f..6aa9c6e 100644 --- a/src/rstd/inlining.rs +++ b/src/rstd/inlining.rs @@ -96,10 +96,11 @@ pub struct SizeError { impl Display for SizeError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_fmt(format_args!( + write!( + f, "expected {} bytes, read/wrote {} instead.", self.expected, self.received - )) + ) } } @@ -115,9 +116,9 @@ pub enum CheckedParseError { impl Display for CheckedParseError

{ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - CheckedParseError::Parse(parse_error) => f.write_fmt(format_args!("{}", parse_error)), - CheckedParseError::Size(size_error) => { - f.write_fmt(format_args!("size check failed: {}", size_error)) + Self::Parse(parse_error) => write!(f, "{}", parse_error), + Self::Size(size_error) => { + write!(f, "size check failed: {}", size_error) } } } diff --git a/src/rstd/point.rs b/src/rstd/point.rs index bd771e9..efc20aa 100644 --- a/src/rstd/point.rs +++ b/src/rstd/point.rs @@ -41,10 +41,9 @@ pub enum PointParseError { impl Display for PointParseError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - PointParseError::WrongLength(length) => f.write_fmt(format_args!( - "expected {} bytes, received {}.", - HASH_SIZE, length - )), + Self::WrongLength(length) => { + write!(f, "expected {} bytes, received {}.", HASH_SIZE, length) + } } } } diff --git a/src/rstd/tracing/rendered_display.rs b/src/rstd/tracing/rendered_display.rs index 3e2a476..addf7e9 100644 --- a/src/rstd/tracing/rendered_display.rs +++ b/src/rstd/tracing/rendered_display.rs @@ -4,16 +4,16 @@ use super::*; impl Display for WithLengthAndWidth { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_fmt(format_args!("{}", self.value())) + write!(f, "{}", self.value()) } } impl Display for RenderedAny { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - Self::Common(common) => f.write_fmt(format_args!("{}", common)), - Self::Wide(vec) => f.write_fmt(format_args!("( {} )", RenderVec(vec))), - Self::Long(vec) => f.write_fmt(format_args!("( {} )", RenderVec(vec))), + Self::Common(common) => write!(f, "{}", common), + Self::Wide(vec) => write!(f, "( {} )", RenderVec(vec)), + Self::Long(vec) => write!(f, "( {} )", RenderVec(vec)), } } } @@ -21,10 +21,10 @@ impl Display for RenderedAny { impl Display for RenderedCommon { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - Self::Empty => f.write_fmt(format_args!(".")), - Self::Resolution => f.write_fmt(format_args!("?")), - Self::Event(event) => f.write_fmt(format_args!("{}", event)), - Self::Action { name, rendered } => f.write_fmt(format_args!("{} @ {}", name, rendered)), + Self::Empty => write!(f, "."), + Self::Resolution => write!(f, "?"), + Self::Event(event) => write!(f, "{}", event), + Self::Action { name, rendered } => write!(f, "{} @ {}", name, rendered), } } } @@ -36,11 +36,11 @@ impl Display for RenderVec<&Vec>> { let mut delimiter = ""; let mut tail = "~"; for rendered in self.0 { - f.write_fmt(format_args!("{}{}", delimiter, rendered))?; + write!(f, "{}{}", delimiter, rendered)?; delimiter = " > "; tail = ""; } - f.write_fmt(format_args!("{}", tail)) + write!(f, "{}", tail) } } @@ -49,19 +49,19 @@ impl Display for RenderVec<&Vec>> { let mut delimiter = ""; let mut tail = "~"; for rendered in self.0 { - f.write_fmt(format_args!("{}{}", delimiter, rendered))?; + write!(f, "{}{}", delimiter, rendered)?; delimiter = " | "; tail = ""; } - f.write_fmt(format_args!("{}", tail)) + write!(f, "{}", tail) } } impl Display for RenderedLong { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - Self::Common(common) => f.write_fmt(format_args!("{}", common)), - Self::Long(vec) => f.write_fmt(format_args!("( {} )", RenderVec(vec))), + Self::Common(common) => write!(f, "{}", common), + Self::Long(vec) => write!(f, "( {} )", RenderVec(vec)), } } } @@ -69,8 +69,8 @@ impl Display for RenderedLong { impl Display for RenderedWide { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - Self::Common(common) => f.write_fmt(format_args!("{}", common)), - Self::Wide(vec) => f.write_fmt(format_args!("( {} )", RenderVec(vec))), + Self::Common(common) => write!(f, "{}", common), + Self::Wide(vec) => write!(f, "( {} )", RenderVec(vec)), } } } diff --git a/src/rstd/tracing/trace.rs b/src/rstd/tracing/trace.rs index 5714295..73e2779 100644 --- a/src/rstd/tracing/trace.rs +++ b/src/rstd/tracing/trace.rs @@ -21,20 +21,18 @@ impl Trace { fn fmt_parallel(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Trace::Parallel(a, b) => { - f.write_fmt(format_args!("{} | {}", ParallelBox(a), ParallelBox(b))) + write!(f, "{} | {}", ParallelBox(a), ParallelBox(b)) } - trace => f.write_fmt(format_args!("{}", trace)), + trace => write!(f, "{}", trace), } } fn fmt_sequential(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - Trace::Sequential { first, second } => f.write_fmt(format_args!( - "{} > {}", - SequentialBox(first), - SequentialBox(second) - )), - trace => f.write_fmt(format_args!("{}", trace)), + Trace::Sequential { first, second } => { + write!(f, "{} > {}", SequentialBox(first), SequentialBox(second)) + } + trace => write!(f, "{}", trace), } } } @@ -58,25 +56,26 @@ impl<'a> Display for SequentialBox<'a> { impl Display for Trace { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - Trace::Pure => f.write_fmt(format_args!(".")), - Trace::InvolvesOneResolution => f.write_fmt(format_args!("?")), - Trace::Event(event) => f.write_fmt(format_args!("{}", event)), - Trace::Wrapped { name, trace } => f.write_fmt(format_args!("{} @ {}", name, trace)), - Trace::Parallel(a, b) => { - f.write_fmt(format_args!("( {} | {} )", ParallelBox(a), ParallelBox(b))) + Self::Pure => write!(f, "."), + Self::InvolvesOneResolution => write!(f, "?"), + Self::Event(event) => write!(f, "{}", event), + Self::Wrapped { name, trace } => write!(f, "{} @ {}", name, trace), + Self::Parallel(a, b) => { + write!(f, "( {} | {} )", ParallelBox(a), ParallelBox(b)) } - Trace::Sequential { first, second } => f.write_fmt(format_args!( + Self::Sequential { first, second } => write!( + f, "( {} > {} )", SequentialBox(first), SequentialBox(second) - )), + ), } } } impl Display for TraceBox { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_fmt(format_args!("{}", self.trace)) + write!(f, "{}", self.trace) } } diff --git a/src/rstd/typeless.rs b/src/rstd/typeless.rs index 45377b5..bbfdcfd 100644 --- a/src/rstd/typeless.rs +++ b/src/rstd/typeless.rs @@ -84,7 +84,7 @@ pub struct TypelessError<'a>(Box); impl<'a> Display for TypelessError<'a> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_fmt(format_args!("typeless error: {}", self.0)) + write!(f, "typeless error: {}", self.0) } } diff --git a/src/testing.rs b/src/testing.rs index dedd6a5..b412cd6 100644 --- a/src/testing.rs +++ b/src/testing.rs @@ -50,16 +50,15 @@ impl<'a> From> for TestLookupError<'a> { impl<'a> Display for TestLookupError<'a> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - TestLookupError::Typeless(typeless_error) => { - f.write_fmt(format_args!("typeless lookup failure: {}", typeless_error)) + Self::Typeless(typeless_error) => { + write!(f, "typeless lookup failure: {}", typeless_error) } - TestLookupError::Cast(cast_error) => { - f.write_fmt(format_args!("cast failure: {}", cast_error)) + Self::Cast(cast_error) => { + write!(f, "cast failure: {}", cast_error) + } + Self::EmptyResolverAccess(address) => { + write!(f, "accessed an empty resolved at address {}.", address) } - TestLookupError::EmptyResolverAccess(address) => f.write_fmt(format_args!( - "accessed an empty resolved at address {}.", - address - )), } } }