Skip to content

Commit

Permalink
Replace uses of write! with write_str and write_fmt
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jonasbb committed Feb 13, 2024
1 parent 76cf252 commit 9b40c7b
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions serde_with/src/de/impls.rs
Expand Up @@ -847,7 +847,7 @@ where
type Value = S;

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

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

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

fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
Expand Down
2 changes: 1 addition & 1 deletion serde_with/src/enum_map.rs
Expand Up @@ -186,7 +186,7 @@ where
type Value = Vec<T>;

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

fn visit_map<A: MapAccess<'de>>(self, map: A) -> Result<Self::Value, A::Error> {
Expand Down
2 changes: 1 addition & 1 deletion serde_with/src/key_value_map.rs
Expand Up @@ -188,7 +188,7 @@ where
type Value = Vec<T>;

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

fn visit_map<A: MapAccess<'de>>(self, map: A) -> Result<Self::Value, A::Error> {
Expand Down
2 changes: 1 addition & 1 deletion serde_with/src/utils/duration.rs
Expand Up @@ -390,7 +390,7 @@ impl<'de> DeserializeAs<'de, DurationSigned> for DurationSeconds<String, Strict>
type Value = DurationSigned;

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

fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
Expand Down
4 changes: 2 additions & 2 deletions serde_with/tests/derives/serialize_display.rs
@@ -1,5 +1,5 @@
use super::*;
use core::fmt;
use core::fmt::{self, Write};

Check failure on line 2 in serde_with/tests/derives/serialize_display.rs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, 1.65)

unused import: `Write`

Check failure on line 2 in serde_with/tests/derives/serialize_display.rs

View workflow job for this annotation

GitHub Actions / clippy_check (ubuntu-latest, stable)

unused import: `Write`

Check failure on line 2 in serde_with/tests/derives/serialize_display.rs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, 1.70)

unused import: `Write`

Check failure on line 2 in serde_with/tests/derives/serialize_display.rs

View workflow job for this annotation

GitHub Actions / clippy_check (ubuntu-latest, nightly)

unused import: `Write`

Check failure on line 2 in serde_with/tests/derives/serialize_display.rs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, 1.75)

unused import: `Write`

Check failure on line 2 in serde_with/tests/derives/serialize_display.rs

View workflow job for this annotation

GitHub Actions / clippy_check (windows-latest, stable)

unused import: `Write`

Check failure on line 2 in serde_with/tests/derives/serialize_display.rs

View workflow job for this annotation

GitHub Actions / clippy_check (windows-latest, nightly)

unused import: `Write`
use serde_with::SerializeDisplay;

#[derive(Debug, SerializeDisplay)]
Expand All @@ -10,7 +10,7 @@ struct A {

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

Expand Down
2 changes: 1 addition & 1 deletion serde_with/tests/serde_as/enum_map.rs
Expand Up @@ -9,7 +9,7 @@ fn bytes_debug_readable(bytes: &[u8]) -> String {
for &byte in bytes {
match byte {
non_printable if !(0x20..0x7f).contains(&non_printable) => {
write!(result, "\\x{byte:02x}").unwrap();
result.write_fmt(format_args!("\\x{byte:02x}")).unwrap();
}
b'\\' => result.push_str("\\\\"),
_ => {
Expand Down

0 comments on commit 9b40c7b

Please sign in to comment.