Skip to content

Commit e1a1ad6

Browse files
hchokshimeta-codesync[bot]
authored andcommitted
Add serde option to @rust.Adapter annotation
Summary: The Rust generator has validations to disallow the combination of adapter + serde on the same type. However, those validations are not currently enforced correctly - the Rust generator initializes its options in `generate_program`. This means the options are *not* initialized when `validate_program` runs. https://www.internalfb.com/code/fbsource/[017165f964e6]/xplat/thrift/compiler/compiler.cc?lines=526-536 This means the Rust validators don't get the true view of options, which currently allows invalid programs to pass validation. E.g. `validate_struct_annotations` disallows the `rust.Adapter` annotation with the `serde` compiler/generator option, but because `rust_codegen_options::serde` is not initialized until `generate_program` is called, `serde` will always be `false` when the validator runs. This adds a new `serde` option on the `rust.Adapter` annotation, which can be set to inform the compiler/generator "this adapter will implement Serialize/Deserialize traits". A follow-up change will update the validation to allow `rust.Adapter` in combination with serde if this option is set to true. Reviewed By: dtolnay Differential Revision: D98007932 fbshipit-source-id: 6ce3eb479fbc26d749ff817cc76d81587c47e419
1 parent 39380e1 commit e1a1ad6

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

thrift/annotation/rust.thrift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,17 @@ struct Mod {
344344
@scope.Struct
345345
struct Adapter {
346346
1: string name;
347+
/**
348+
* If true, the adapter's `AdaptedType` is expected to implement
349+
* `serde::Serialize` and `serde::Deserialize`. This allows the adapted field
350+
* to be used in types that derive serde traits (via the `serde` codegen
351+
* option or `@rust.Serde`).
352+
*
353+
* If false (default), using this adapter on a field with serde enabled will
354+
* produce a validation error, since the compiler cannot verify that the
355+
* `AdaptedType` implements the required serde traits.
356+
*/
357+
2: bool serde = false;
347358
}
348359

349360
/**

0 commit comments

Comments
 (0)