Skip to content

Commit 80f0156

Browse files
committed
Alternative to #413: Some is explicitly not a newtype variant
1 parent 5642e6c commit 80f0156

File tree

5 files changed

+7
-26
lines changed

5 files changed

+7
-26
lines changed

fuzz/fuzz_targets/bench/lib.rs

-6
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,6 @@ pub fn roundtrip_arbitrary_typed_ron_or_panic(data: &[u8]) -> Option<TypedSerdeD
3434
match err.code {
3535
// Erroring on deep recursion is better than crashing on a stack overflow
3636
ron::error::Error::ExceededRecursionLimit => return None,
37-
// FIXME: deserialising `Some(...)` inside `deserialize_any` with
38-
// `unwrap_variant_newtypes` enabled is unsupported, since `Some`
39-
// is special-cased by `deserialize_any`
40-
ron::error::Error::UnsupportedSelfDescribingUnwrappedSomeNewtypeVariant => {
41-
return None
42-
}
4337
// FIXME: temporarily allow unimplemented cases to pass
4438
ron::error::Error::Message(msg) if msg == "fuzz-unimplemented-fuzz" => return None,
4539
// Everything else is actually a bug we want to find

src/de/mod.rs

+3-13
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,6 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
227227
} else if self.bytes.consume_ident("false") {
228228
return visitor.visit_bool(false);
229229
} else if self.bytes.check_ident("Some") {
230-
if self
231-
.bytes
232-
.exts
233-
.contains(Extensions::UNWRAP_VARIANT_NEWTYPES)
234-
{
235-
return Err(Error::UnsupportedSelfDescribingUnwrappedSomeNewtypeVariant);
236-
}
237230
return self.deserialize_option(visitor);
238231
} else if self.bytes.consume_ident("None") {
239232
return visitor.visit_none();
@@ -447,15 +440,12 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
447440
} {
448441
self.bytes.skip_ws()?;
449442

450-
self.newtype_variant = self
451-
.bytes
452-
.exts
453-
.contains(Extensions::UNWRAP_VARIANT_NEWTYPES);
443+
// Some is explicitly not a newtype variant, since
444+
// `deserialize_any` cannot handle "Some(a: 42)"
445+
self.newtype_variant = false;
454446

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

457-
self.newtype_variant = false;
458-
459449
self.bytes.comma()?;
460450

461451
if self.bytes.consume(")") {

src/error.rs

-2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ pub enum Error {
9292
SuggestRawIdentifier(String),
9393
ExpectedRawValue,
9494
ExceededRecursionLimit,
95-
UnsupportedSelfDescribingUnwrappedSomeNewtypeVariant,
9695
}
9796

9897
impl fmt::Display for SpannedError {
@@ -253,7 +252,6 @@ impl fmt::Display for Error {
253252
),
254253
Error::ExpectedRawValue => f.write_str("Expected a `ron::value::RawValue`"),
255254
Error::ExceededRecursionLimit => f.write_str("Exceeded recursion limit, try increasing the limit and using `serde_stacker` to protect against a stack overflow"),
256-
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"),
257255
}
258256
}
259257
}

src/ser/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -681,17 +681,16 @@ impl<'a, W: io::Write> ser::Serializer for &'a mut Serializer<W> {
681681
if implicit_some {
682682
self.implicit_some_depth += 1;
683683
} else {
684-
self.newtype_variant = self
685-
.extensions()
686-
.contains(Extensions::UNWRAP_VARIANT_NEWTYPES);
684+
// Some is explicitly not a newtype variant, since
685+
// `deserialize_any` cannot handle "Some(a: 42)"
686+
self.newtype_variant = false;
687687
self.output.write_all(b"Some(")?;
688688
}
689689
guard_recursion! { self => value.serialize(&mut *self)? };
690690
if implicit_some {
691691
self.implicit_some_depth = 0;
692692
} else {
693693
self.output.write_all(b")")?;
694-
self.newtype_variant = false;
695694
}
696695

697696
Ok(())

tests/250_variant_newtypes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ fn test_deserialise_tuple_newtypes() {
229229
);
230230
assert_eq!(
231231
from_str::<TestEnum>(
232-
r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeOption(Some(a: 4, b: false))"#
232+
r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeOption(Some((a: 4, b: false)))"#
233233
)
234234
.unwrap(),
235235
TestEnum::TupleNewtypeOption(Some(Struct { a: 4, b: false })),

0 commit comments

Comments
 (0)