Skip to content

Commit f0bf1e9

Browse files
Alex Snastfacebook-github-bot
authored andcommitted
implement variant_names for Thrift unions
Summary: Similar to variant_names but returns a slice of tuples of (rust name, original name) Reviewed By: emersonford Differential Revision: D77227852 fbshipit-source-id: dbc57e05cf9ab93ccb841401218ad720290dad87
1 parent e3a6bb1 commit f0bf1e9

File tree

12 files changed

+195
-0
lines changed

12 files changed

+195
-0
lines changed

third-party/thrift/src/thrift/compiler/generate/templates/rust/lib/unionimpl.mustache

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,15 @@ impl {{struct:rust_name}} {
123123
Self::UnknownField(_) => ::std::option::Option::None,
124124
}
125125
}
126+
127+
/// Return all union variant names as a tuple of (Rust name, original name).
128+
pub fn variant_names() -> &'static [(&'static ::std::primitive::str, &'static ::std::primitive::str)] {
129+
&[
130+
{{#struct:fields}}
131+
("{{field:rust_name}}", "{{field:name}}"),
132+
{{/struct:fields}}
133+
]
134+
}
126135
}
127136
{{#program:rust_gen_native_metadata?}}
128137
{{> lib/metadata/struct}}

third-party/thrift/src/thrift/compiler/test/fixtures/adapter/out/rust/gen-rust/types.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,6 +1629,17 @@ impl Baz {
16291629
Self::UnknownField(_) => ::std::option::Option::None,
16301630
}
16311631
}
1632+
1633+
/// Return all union variant names as a tuple of (Rust name, original name).
1634+
pub fn variant_names() -> &'static [(&'static ::std::primitive::str, &'static ::std::primitive::str)] {
1635+
&[
1636+
("intField", "intField"),
1637+
("setField", "setField"),
1638+
("mapField", "mapField"),
1639+
("binaryField", "binaryField"),
1640+
("longField", "longField"),
1641+
]
1642+
}
16321643
}
16331644

16341645
impl ::fbthrift::metadata::ThriftAnnotations for Baz {
@@ -4294,6 +4305,14 @@ impl AdaptTestUnion {
42944305
Self::UnknownField(_) => ::std::option::Option::None,
42954306
}
42964307
}
4308+
4309+
/// Return all union variant names as a tuple of (Rust name, original name).
4310+
pub fn variant_names() -> &'static [(&'static ::std::primitive::str, &'static ::std::primitive::str)] {
4311+
&[
4312+
("delay", "delay"),
4313+
("custom", "custom"),
4314+
]
4315+
}
42974316
}
42984317

42994318
impl ::fbthrift::metadata::ThriftAnnotations for AdaptTestUnion {

third-party/thrift/src/thrift/compiler/test/fixtures/basic-annotations/out/rust/gen-rust/types.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,12 @@ impl MyUnion {
386386
Self::UnknownField(_) => ::std::option::Option::None,
387387
}
388388
}
389+
390+
/// Return all union variant names as a tuple of (Rust name, original name).
391+
pub fn variant_names() -> &'static [(&'static ::std::primitive::str, &'static ::std::primitive::str)] {
392+
&[
393+
]
394+
}
389395
}
390396

391397
impl ::fbthrift::metadata::ThriftAnnotations for MyUnion {

third-party/thrift/src/thrift/compiler/test/fixtures/basic/out/rust/gen-rust/types.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,16 @@ impl MyUnion {
940940
Self::UnknownField(_) => ::std::option::Option::None,
941941
}
942942
}
943+
944+
/// Return all union variant names as a tuple of (Rust name, original name).
945+
pub fn variant_names() -> &'static [(&'static ::std::primitive::str, &'static ::std::primitive::str)] {
946+
&[
947+
("myEnum", "myEnum"),
948+
("myStruct", "myStruct"),
949+
("myDataItem", "myDataItem"),
950+
("floatSet", "floatSet"),
951+
]
952+
}
943953
}
944954

945955
impl ::fbthrift::metadata::ThriftAnnotations for MyUnion {
@@ -1486,6 +1496,13 @@ impl UnionToBeRenamed {
14861496
Self::UnknownField(_) => ::std::option::Option::None,
14871497
}
14881498
}
1499+
1500+
/// Return all union variant names as a tuple of (Rust name, original name).
1501+
pub fn variant_names() -> &'static [(&'static ::std::primitive::str, &'static ::std::primitive::str)] {
1502+
&[
1503+
("reserved_field", "reserved_field"),
1504+
]
1505+
}
14891506
}
14901507

14911508
impl ::fbthrift::metadata::ThriftAnnotations for UnionToBeRenamed {

third-party/thrift/src/thrift/compiler/test/fixtures/complex-union/out/rust/gen-rust/types.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,18 @@ impl ComplexUnion {
215215
Self::UnknownField(_) => ::std::option::Option::None,
216216
}
217217
}
218+
219+
/// Return all union variant names as a tuple of (Rust name, original name).
220+
pub fn variant_names() -> &'static [(&'static ::std::primitive::str, &'static ::std::primitive::str)] {
221+
&[
222+
("intValue", "intValue"),
223+
("stringValue", "stringValue"),
224+
("intListValue", "intListValue"),
225+
("stringListValue", "stringListValue"),
226+
("typedefValue", "typedefValue"),
227+
("stringRef", "stringRef"),
228+
]
229+
}
218230
}
219231

220232
impl ::fbthrift::metadata::ThriftAnnotations for ComplexUnion {
@@ -350,6 +362,14 @@ impl ListUnion {
350362
Self::UnknownField(_) => ::std::option::Option::None,
351363
}
352364
}
365+
366+
/// Return all union variant names as a tuple of (Rust name, original name).
367+
pub fn variant_names() -> &'static [(&'static ::std::primitive::str, &'static ::std::primitive::str)] {
368+
&[
369+
("intListValue", "intListValue"),
370+
("stringListValue", "stringListValue"),
371+
]
372+
}
353373
}
354374

355375
impl ::fbthrift::metadata::ThriftAnnotations for ListUnion {
@@ -467,6 +487,14 @@ impl DataUnion {
467487
Self::UnknownField(_) => ::std::option::Option::None,
468488
}
469489
}
490+
491+
/// Return all union variant names as a tuple of (Rust name, original name).
492+
pub fn variant_names() -> &'static [(&'static ::std::primitive::str, &'static ::std::primitive::str)] {
493+
&[
494+
("binaryData", "binaryData"),
495+
("stringData", "stringData"),
496+
]
497+
}
470498
}
471499

472500
impl ::fbthrift::metadata::ThriftAnnotations for DataUnion {
@@ -705,6 +733,14 @@ impl ValUnion {
705733
Self::UnknownField(_) => ::std::option::Option::None,
706734
}
707735
}
736+
737+
/// Return all union variant names as a tuple of (Rust name, original name).
738+
pub fn variant_names() -> &'static [(&'static ::std::primitive::str, &'static ::std::primitive::str)] {
739+
&[
740+
("v1", "v1"),
741+
("v2", "v2"),
742+
]
743+
}
708744
}
709745

710746
impl ::fbthrift::metadata::ThriftAnnotations for ValUnion {
@@ -822,6 +858,14 @@ impl VirtualComplexUnion {
822858
Self::UnknownField(_) => ::std::option::Option::None,
823859
}
824860
}
861+
862+
/// Return all union variant names as a tuple of (Rust name, original name).
863+
pub fn variant_names() -> &'static [(&'static ::std::primitive::str, &'static ::std::primitive::str)] {
864+
&[
865+
("thingOne", "thingOne"),
866+
("thingTwo", "thingTwo"),
867+
]
868+
}
825869
}
826870

827871
impl ::fbthrift::metadata::ThriftAnnotations for VirtualComplexUnion {
@@ -1031,6 +1075,13 @@ impl NonCopyableUnion {
10311075
Self::UnknownField(_) => ::std::option::Option::None,
10321076
}
10331077
}
1078+
1079+
/// Return all union variant names as a tuple of (Rust name, original name).
1080+
pub fn variant_names() -> &'static [(&'static ::std::primitive::str, &'static ::std::primitive::str)] {
1081+
&[
1082+
("s", "s"),
1083+
]
1084+
}
10341085
}
10351086

10361087
impl ::fbthrift::metadata::ThriftAnnotations for NonCopyableUnion {

third-party/thrift/src/thrift/compiler/test/fixtures/constants/out/rust/gen-rust/types.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,6 +1313,14 @@ impl union1 {
13131313
Self::UnknownField(_) => ::std::option::Option::None,
13141314
}
13151315
}
1316+
1317+
/// Return all union variant names as a tuple of (Rust name, original name).
1318+
pub fn variant_names() -> &'static [(&'static ::std::primitive::str, &'static ::std::primitive::str)] {
1319+
&[
1320+
("i", "i"),
1321+
("d", "d"),
1322+
]
1323+
}
13161324
}
13171325

13181326
impl ::fbthrift::metadata::ThriftAnnotations for union1 {
@@ -1452,6 +1460,16 @@ impl union2 {
14521460
Self::UnknownField(_) => ::std::option::Option::None,
14531461
}
14541462
}
1463+
1464+
/// Return all union variant names as a tuple of (Rust name, original name).
1465+
pub fn variant_names() -> &'static [(&'static ::std::primitive::str, &'static ::std::primitive::str)] {
1466+
&[
1467+
("i", "i"),
1468+
("d", "d"),
1469+
("s", "s"),
1470+
("u", "u"),
1471+
]
1472+
}
14551473
}
14561474

14571475
impl ::fbthrift::metadata::ThriftAnnotations for union2 {

third-party/thrift/src/thrift/compiler/test/fixtures/doctext/out/rust/gen-rust/types.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,14 @@ impl U {
402402
Self::UnknownField(_) => ::std::option::Option::None,
403403
}
404404
}
405+
406+
/// Return all union variant names as a tuple of (Rust name, original name).
407+
pub fn variant_names() -> &'static [(&'static ::std::primitive::str, &'static ::std::primitive::str)] {
408+
&[
409+
("i", "i"),
410+
("s", "s"),
411+
]
412+
}
405413
}
406414

407415
impl ::fbthrift::metadata::ThriftAnnotations for U {

third-party/thrift/src/thrift/compiler/test/fixtures/empty-struct/out/rust/gen-rust/types.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@ impl Nada {
190190
Self::UnknownField(_) => ::std::option::Option::None,
191191
}
192192
}
193+
194+
/// Return all union variant names as a tuple of (Rust name, original name).
195+
pub fn variant_names() -> &'static [(&'static ::std::primitive::str, &'static ::std::primitive::str)] {
196+
&[
197+
]
198+
}
193199
}
194200

195201
impl ::fbthrift::metadata::ThriftAnnotations for Nada {

third-party/thrift/src/thrift/compiler/test/fixtures/refs/out/rust/gen-rust/types.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,14 @@ impl MyUnion {
578578
Self::UnknownField(_) => ::std::option::Option::None,
579579
}
580580
}
581+
582+
/// Return all union variant names as a tuple of (Rust name, original name).
583+
pub fn variant_names() -> &'static [(&'static ::std::primitive::str, &'static ::std::primitive::str)] {
584+
&[
585+
("anInteger", "anInteger"),
586+
("aString", "aString"),
587+
]
588+
}
581589
}
582590

583591
impl ::fbthrift::metadata::ThriftAnnotations for MyUnion {
@@ -722,6 +730,13 @@ impl NonTriviallyDestructibleUnion {
722730
Self::UnknownField(_) => ::std::option::Option::None,
723731
}
724732
}
733+
734+
/// Return all union variant names as a tuple of (Rust name, original name).
735+
pub fn variant_names() -> &'static [(&'static ::std::primitive::str, &'static ::std::primitive::str)] {
736+
&[
737+
("int_field", "int_field"),
738+
]
739+
}
725740
}
726741

727742
impl ::fbthrift::metadata::ThriftAnnotations for NonTriviallyDestructibleUnion {

third-party/thrift/src/thrift/compiler/test/fixtures/rust-annotations/out/rust/gen-rust/types.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2217,6 +2217,15 @@ impl U11 {
22172217
Self::UnknownField(_) => ::std::option::Option::None,
22182218
}
22192219
}
2220+
2221+
/// Return all union variant names as a tuple of (Rust name, original name).
2222+
pub fn variant_names() -> &'static [(&'static ::std::primitive::str, &'static ::std::primitive::str)] {
2223+
&[
2224+
("string", "str"),
2225+
("integer", "integer"),
2226+
("btreeset", "btreeset"),
2227+
]
2228+
}
22202229
}
22212230

22222231
impl ::fbthrift::metadata::ThriftAnnotations for U11 {
@@ -2699,6 +2708,14 @@ impl Bar {
26992708
Self::UnknownField(_) => ::std::option::Option::None,
27002709
}
27012710
}
2711+
2712+
/// Return all union variant names as a tuple of (Rust name, original name).
2713+
pub fn variant_names() -> &'static [(&'static ::std::primitive::str, &'static ::std::primitive::str)] {
2714+
&[
2715+
("Annotated", "WithAnnotation"),
2716+
("WithoutAnnotation", "WithoutAnnotation"),
2717+
]
2718+
}
27022719
}
27032720

27042721
impl ::fbthrift::metadata::ThriftAnnotations for Bar {

0 commit comments

Comments
 (0)