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 c6edddd
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions serde_with/src/de/impls.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
2 changes: 1 addition & 1 deletion serde_with/tests/derives/serialize_display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
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 c6edddd

Please sign in to comment.