Skip to content

Commit

Permalink
Consistently avoid '?' throughout serde crate
Browse files Browse the repository at this point in the history
This makes it easy to redefine a 'try' macro to compare compile-time
between `$expr?` and `match $expr { Ok=>v, Err=>return }`.
  • Loading branch information
dtolnay committed Sep 22, 2022
1 parent d208762 commit a9320db
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion serde/src/de/ignored_any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ impl<'de> Visitor<'de> for IgnoredAny {
where
A: EnumAccess<'de>,
{
data.variant::<IgnoredAny>()?.1.newtype_variant()
try!(data.variant::<IgnoredAny>()).1.newtype_variant()
}
}

Expand Down
8 changes: 4 additions & 4 deletions serde/src/de/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2268,14 +2268,14 @@ where
where
D: Deserializer<'de>,
{
let (start, end) = deserializer.deserialize_struct(
let (start, end) = try!(deserializer.deserialize_struct(
"Range",
range::FIELDS,
range::RangeVisitor {
expecting: "struct Range",
phantom: PhantomData,
},
)?;
));
Ok(start..end)
}
}
Expand All @@ -2289,14 +2289,14 @@ where
where
D: Deserializer<'de>,
{
let (start, end) = deserializer.deserialize_struct(
let (start, end) = try!(deserializer.deserialize_struct(
"RangeInclusive",
range::FIELDS,
range::RangeVisitor {
expecting: "struct RangeInclusive",
phantom: PhantomData,
},
)?;
));
Ok(RangeInclusive::new(start, end))
}
}
Expand Down
2 changes: 1 addition & 1 deletion serde/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ pub trait Deserialize<'de>: Sized {
D: Deserializer<'de>,
{
// Default implementation just delegates to `deserialize` impl.
*place = Deserialize::deserialize(deserializer)?;
*place = try!(Deserialize::deserialize(deserializer));
Ok(())
}
}
Expand Down
2 changes: 1 addition & 1 deletion serde/src/de/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1501,7 +1501,7 @@ where
where
T: de::DeserializeSeed<'de>,
{
match self.map.next_key_seed(seed)? {
match try!(self.map.next_key_seed(seed)) {
Some(key) => Ok((key, private::map_as_enum(self.map))),
None => Err(de::Error::invalid_type(de::Unexpected::Map, &"enum")),
}
Expand Down
7 changes: 4 additions & 3 deletions serde/src/ser/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,9 +614,10 @@ impl Serialize for SystemTime {
S: Serializer,
{
use super::SerializeStruct;
let duration_since_epoch = self
.duration_since(UNIX_EPOCH)
.map_err(|_| S::Error::custom("SystemTime must be later than UNIX_EPOCH"))?;
let duration_since_epoch = match self.duration_since(UNIX_EPOCH) {
Ok(duration_since_epoch) => duration_since_epoch,
Err(_) => return Err(S::Error::custom("SystemTime must be later than UNIX_EPOCH")),
};
let mut state = try!(serializer.serialize_struct("SystemTime", 2));
try!(state.serialize_field("secs_since_epoch", &duration_since_epoch.as_secs()));
try!(state.serialize_field("nanos_since_epoch", &duration_since_epoch.subsec_nanos()));
Expand Down

0 comments on commit a9320db

Please sign in to comment.