Skip to content

Commit 9cb2a2e

Browse files
committed
Get rid of deserialize_option! macro
MapValueDeseriaizer and SeqItemDeserializer should emit error on DeEvent::Eof instead of calling Visitor::visit_none(). We delegate emitting appropriate error to the Some variant. The content of deserialize_option! macro was inlined and handling of DeEvent::Eof was removed from MapValueDeseriaizer and SeqItemDeserializer. This does not change existing behavior, because Eof in the middle of struct anyway would be captured by `read_to_end()` call at the end of structure deserialization, but this change will report an error in appropriate place, which would important when we implement error position tracking
1 parent 4b8f17d commit 9cb2a2e

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

src/de/map.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -543,11 +543,14 @@ where
543543

544544
forward!(deserialize_any);
545545

546-
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, DeError>
546+
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
547547
where
548548
V: Visitor<'de>,
549549
{
550-
deserialize_option!(self.map.de, self, visitor)
550+
match self.map.de.peek()? {
551+
DeEvent::Text(t) if t.is_empty() => visitor.visit_none(),
552+
_ => visitor.visit_some(self),
553+
}
551554
}
552555

553556
/// Forwards deserialization of the inner type. Always calls [`Visitor::visit_newtype_struct`]
@@ -870,11 +873,14 @@ where
870873

871874
forward!(deserialize_any);
872875

873-
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, DeError>
876+
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
874877
where
875878
V: Visitor<'de>,
876879
{
877-
deserialize_option!(self.map.de, self, visitor)
880+
match self.map.de.peek()? {
881+
DeEvent::Text(t) if t.is_empty() => visitor.visit_none(),
882+
_ => visitor.visit_some(self),
883+
}
878884
}
879885

880886
/// Forwards deserialization of the inner type. Always calls [`Visitor::visit_newtype_struct`]

src/de/mod.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,16 +1954,6 @@ macro_rules! deserialize_primitives {
19541954
};
19551955
}
19561956

1957-
macro_rules! deserialize_option {
1958-
($de:expr, $deserializer:ident, $visitor:ident) => {
1959-
match $de.peek()? {
1960-
DeEvent::Text(t) if t.is_empty() => $visitor.visit_none(),
1961-
DeEvent::Eof => $visitor.visit_none(),
1962-
_ => $visitor.visit_some($deserializer),
1963-
}
1964-
};
1965-
}
1966-
19671957
mod key;
19681958
mod map;
19691959
mod resolver;
@@ -2875,7 +2865,11 @@ where
28752865
where
28762866
V: Visitor<'de>,
28772867
{
2878-
deserialize_option!(self, self, visitor)
2868+
match self.peek()? {
2869+
DeEvent::Text(t) if t.is_empty() => visitor.visit_none(),
2870+
DeEvent::Eof => visitor.visit_none(),
2871+
_ => visitor.visit_some(self),
2872+
}
28792873
}
28802874

28812875
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, DeError>

0 commit comments

Comments
 (0)