Skip to content

Commit

Permalink
Alternative to #413: Some is explicitly not a newtype variant
Browse files Browse the repository at this point in the history
  • Loading branch information
juntyr committed Jul 16, 2023
1 parent 5642e6c commit 80f0156
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 26 deletions.
6 changes: 0 additions & 6 deletions fuzz/fuzz_targets/bench/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ pub fn roundtrip_arbitrary_typed_ron_or_panic(data: &[u8]) -> Option<TypedSerdeD
match err.code {
// Erroring on deep recursion is better than crashing on a stack overflow
ron::error::Error::ExceededRecursionLimit => return None,
// FIXME: deserialising `Some(...)` inside `deserialize_any` with
// `unwrap_variant_newtypes` enabled is unsupported, since `Some`
// is special-cased by `deserialize_any`
ron::error::Error::UnsupportedSelfDescribingUnwrappedSomeNewtypeVariant => {
return None
}
// FIXME: temporarily allow unimplemented cases to pass
ron::error::Error::Message(msg) if msg == "fuzz-unimplemented-fuzz" => return None,
// Everything else is actually a bug we want to find
Expand Down
16 changes: 3 additions & 13 deletions src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,6 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
} else if self.bytes.consume_ident("false") {
return visitor.visit_bool(false);
} else if self.bytes.check_ident("Some") {
if self
.bytes
.exts
.contains(Extensions::UNWRAP_VARIANT_NEWTYPES)
{
return Err(Error::UnsupportedSelfDescribingUnwrappedSomeNewtypeVariant);
}
return self.deserialize_option(visitor);
} else if self.bytes.consume_ident("None") {
return visitor.visit_none();
Expand Down Expand Up @@ -447,15 +440,12 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
} {
self.bytes.skip_ws()?;

self.newtype_variant = self
.bytes
.exts
.contains(Extensions::UNWRAP_VARIANT_NEWTYPES);
// Some is explicitly not a newtype variant, since
// `deserialize_any` cannot handle "Some(a: 42)"
self.newtype_variant = false;

let v = guard_recursion! { self => visitor.visit_some(&mut *self)? };

self.newtype_variant = false;

self.bytes.comma()?;

if self.bytes.consume(")") {
Expand Down
2 changes: 0 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ pub enum Error {
SuggestRawIdentifier(String),
ExpectedRawValue,
ExceededRecursionLimit,
UnsupportedSelfDescribingUnwrappedSomeNewtypeVariant,
}

impl fmt::Display for SpannedError {
Expand Down Expand Up @@ -253,7 +252,6 @@ impl fmt::Display for Error {
),
Error::ExpectedRawValue => f.write_str("Expected a `ron::value::RawValue`"),
Error::ExceededRecursionLimit => f.write_str("Exceeded recursion limit, try increasing the limit and using `serde_stacker` to protect against a stack overflow"),
Error::UnsupportedSelfDescribingUnwrappedSomeNewtypeVariant => f.write_str("Unsupported deserialize of `Some(...)` inside an untyped, self-describing `deserialize_any` call while the `unwrap_variant_newtypes` extension is enabled"),
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -681,17 +681,16 @@ impl<'a, W: io::Write> ser::Serializer for &'a mut Serializer<W> {
if implicit_some {
self.implicit_some_depth += 1;
} else {
self.newtype_variant = self
.extensions()
.contains(Extensions::UNWRAP_VARIANT_NEWTYPES);
// Some is explicitly not a newtype variant, since
// `deserialize_any` cannot handle "Some(a: 42)"
self.newtype_variant = false;
self.output.write_all(b"Some(")?;
}
guard_recursion! { self => value.serialize(&mut *self)? };
if implicit_some {
self.implicit_some_depth = 0;
} else {
self.output.write_all(b")")?;
self.newtype_variant = false;
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion tests/250_variant_newtypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ fn test_deserialise_tuple_newtypes() {
);
assert_eq!(
from_str::<TestEnum>(
r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeOption(Some(a: 4, b: false))"#
r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeOption(Some((a: 4, b: false)))"#
)
.unwrap(),
TestEnum::TupleNewtypeOption(Some(Struct { a: 4, b: false })),
Expand Down

0 comments on commit 80f0156

Please sign in to comment.