Skip to content

Commit b8fafef

Browse files
committed
A few minor write_str optimizations and inlining
Apparently `write!` generates more code than `write_str` when used with a simple string, so optimizing it.
1 parent c42ebb8 commit b8fafef

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

serde/src/de/mod.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -405,17 +405,17 @@ impl<'a> fmt::Display for Unexpected<'a> {
405405
Float(f) => write!(formatter, "floating point `{}`", WithDecimalPoint(f)),
406406
Char(c) => write!(formatter, "character `{}`", c),
407407
Str(s) => write!(formatter, "string {:?}", s),
408-
Bytes(_) => write!(formatter, "byte array"),
409-
Unit => write!(formatter, "unit value"),
410-
Option => write!(formatter, "Option value"),
411-
NewtypeStruct => write!(formatter, "newtype struct"),
412-
Seq => write!(formatter, "sequence"),
413-
Map => write!(formatter, "map"),
414-
Enum => write!(formatter, "enum"),
415-
UnitVariant => write!(formatter, "unit variant"),
416-
NewtypeVariant => write!(formatter, "newtype variant"),
417-
TupleVariant => write!(formatter, "tuple variant"),
418-
StructVariant => write!(formatter, "struct variant"),
408+
Bytes(_) => formatter.write_str("byte array"),
409+
Unit => formatter.write_str("unit value"),
410+
Option => formatter.write_str("Option value"),
411+
NewtypeStruct => formatter.write_str("newtype struct"),
412+
Seq => formatter.write_str("sequence"),
413+
Map => formatter.write_str("map"),
414+
Enum => formatter.write_str("enum"),
415+
UnitVariant => formatter.write_str("unit variant"),
416+
NewtypeVariant => formatter.write_str("newtype variant"),
417+
TupleVariant => formatter.write_str("tuple variant"),
418+
StructVariant => formatter.write_str("struct variant"),
419419
Other(other) => formatter.write_str(other),
420420
}
421421
}
@@ -2278,10 +2278,10 @@ impl Display for OneOf {
22782278
1 => write!(formatter, "`{}`", self.names[0]),
22792279
2 => write!(formatter, "`{}` or `{}`", self.names[0], self.names[1]),
22802280
_ => {
2281-
tri!(write!(formatter, "one of "));
2281+
tri!(formatter.write_str("one of "));
22822282
for (i, alt) in self.names.iter().enumerate() {
22832283
if i > 0 {
2284-
tri!(write!(formatter, ", "));
2284+
tri!(formatter.write_str(", "));
22852285
}
22862286
tri!(write!(formatter, "`{}`", alt));
22872287
}

serde/src/de/value.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,7 @@ struct ExpectedInSeq(usize);
983983
impl Expected for ExpectedInSeq {
984984
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
985985
if self.0 == 1 {
986-
write!(formatter, "1 element in sequence")
986+
formatter.write_str("1 element in sequence")
987987
} else {
988988
write!(formatter, "{} elements in sequence", self.0)
989989
}
@@ -1411,7 +1411,7 @@ struct ExpectedInMap(usize);
14111411
impl Expected for ExpectedInMap {
14121412
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
14131413
if self.0 == 1 {
1414-
write!(formatter, "1 element in map")
1414+
formatter.write_str("1 element in map")
14151415
} else {
14161416
write!(formatter, "{} elements in map", self.0)
14171417
}

test_suite/tests/test_value.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ fn test_map_access_to_enum() {
6363
type Value = Potential;
6464

6565
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
66-
write!(formatter, "a map")
66+
formatter.write_str("a map")
6767
}
6868

6969
fn visit_map<A>(self, map: A) -> Result<Self::Value, A::Error>

0 commit comments

Comments
 (0)