Skip to content

Commit c6edddd

Browse files
committed
Replace uses of write! with write_str and write_fmt
write! with a single string argument is not properly optimized and using write_str generates better code: serde-rs/serde#2697 rust-lang/rust#121001
1 parent 76cf252 commit c6edddd

File tree

6 files changed

+7
-7
lines changed

6 files changed

+7
-7
lines changed

serde_with/src/de/impls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ where
847847
type Value = S;
848848

849849
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
850-
write!(formatter, "a string")
850+
formatter.write_str("a string")
851851
}
852852

853853
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
@@ -1091,7 +1091,7 @@ where
10911091
type Value = I;
10921092

10931093
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
1094-
write!(formatter, "a string")
1094+
formatter.write_str("a string")
10951095
}
10961096

10971097
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>

serde_with/src/enum_map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ where
186186
type Value = Vec<T>;
187187

188188
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
189-
write!(formatter, "a map of enum values")
189+
formatter.write_str("a map of enum values")
190190
}
191191

192192
fn visit_map<A: MapAccess<'de>>(self, map: A) -> Result<Self::Value, A::Error> {

serde_with/src/key_value_map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ where
188188
type Value = Vec<T>;
189189

190190
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
191-
write!(formatter, "a map")
191+
formatter.write_str("a map")
192192
}
193193

194194
fn visit_map<A: MapAccess<'de>>(self, map: A) -> Result<Self::Value, A::Error> {

serde_with/src/utils/duration.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ impl<'de> DeserializeAs<'de, DurationSigned> for DurationSeconds<String, Strict>
390390
type Value = DurationSigned;
391391

392392
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
393-
write!(formatter, "a string containing a number")
393+
formatter.write_str("a string containing a number")
394394
}
395395

396396
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>

serde_with/tests/derives/serialize_display.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ struct A {
1010

1111
impl fmt::Display for A {
1212
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
13-
write!(f, "->{} <> {}<-", self.a, self.b)
13+
f.write_fmt(format_args!("->{} <> {}<-", self.a, self.b))
1414
}
1515
}
1616

serde_with/tests/serde_as/enum_map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn bytes_debug_readable(bytes: &[u8]) -> String {
99
for &byte in bytes {
1010
match byte {
1111
non_printable if !(0x20..0x7f).contains(&non_printable) => {
12-
write!(result, "\\x{byte:02x}").unwrap();
12+
result.write_fmt(format_args!("\\x{byte:02x}")).unwrap();
1313
}
1414
b'\\' => result.push_str("\\\\"),
1515
_ => {

0 commit comments

Comments
 (0)