{{! Copyright (c) Meta Platforms, Inc. and its affiliates. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. }} {{#struct:union?}} impl ::std::default::Default for {{struct:rust_name}} { fn default() -> Self { Self::UnknownField(-1) } } impl ::fbthrift::GetTType for {{struct:rust_name}} { const TTYPE: ::fbthrift::TType = ::fbthrift::TType::Struct; } {{!newline}} {{#struct:thrift_uri}} impl ::fbthrift::GetUri for self::{{struct:rust_name}} { fn uri() -> &'static str { "{{struct:thrift_uri}}" } } {{/struct:thrift_uri}} impl

::fbthrift::Serialize

for {{struct:rust_name}} where P: ::fbthrift::ProtocolWriter, { fn write(&self, p: &mut P) { p.write_struct_begin("{{struct:name}}"); match self {{>lib/block}}{{! }}{{#struct:fields}} Self::{{field:rust_name}}(inner) => { p.write_field_begin({{! }}"{{field:name}}", {{! }}{{#field:type}}{{>lib/ttype}}{{/field:type}}, {{! }}{{field:key}}{{! }}); {{#field:type}}{{#field:type_annotation?}}{{>lib/annfieldwrite}}{{/field:type_annotation?}}{{^field:type_annotation?}}{{>lib/write}}{{/field:type_annotation?}}{{/field:type}}({{! }}{{#field:has_adapter?}}&{{>lib/adapter/qualified}}::to_thrift_field::<{{struct:rust_name}}>({{/field:has_adapter?}}{{! }}inner{{! }}{{#field:has_adapter?}}, {{field:key}}){{/field:has_adapter?}}{{! }}, p); p.write_field_end(); }{{! }}{{/struct:fields}} Self::UnknownField(_) => {} } p.write_field_stop(); p.write_struct_end(); } } impl

::fbthrift::Deserialize

for {{struct:rust_name}} where P: ::fbthrift::ProtocolReader, { fn read(p: &mut P) -> ::anyhow::Result { static FIELDS: &[::fbthrift::Field] = &[ {{#struct:fields_by_name}} ::fbthrift::Field::new("{{field:name}}", {{#field:type}}{{>lib/ttype}}{{/field:type}}, {{field:key}}), {{/struct:fields_by_name}} ]; let _ = ::anyhow::Context::context(p.read_struct_begin(|_| ()), "Expected a {{struct:name}}")?; let {{#struct:fields?}}mut {{/struct:fields?}}once = false; let {{#struct:fields?}}mut {{/struct:fields?}}alt = ::std::option::Option::None; loop { let (_, fty, fid) = p.read_field_begin(|_| (), FIELDS)?; match (fty, fid as ::std::primitive::i32, once) { (::fbthrift::TType::Stop, _, _) => break,{{! }}{{#struct:fields}} ({{#field:type}}{{>lib/ttype}}{{/field:type}}, {{field:key}}, false) => { once = true; alt = ::std::option::Option::Some(Self::{{field:rust_name}}({{! }}{{#field:has_adapter?}}{{>lib/adapter/qualified}}::from_thrift_field::<{{struct:rust_name}}>({{/field:has_adapter?}}{{! }}{{#field:type}}{{#field:type_annotation?}}{{>lib/annfieldread}}{{/field:type_annotation?}}{{^field:type_annotation?}}{{>lib/read}}{{/field:type_annotation?}}{{/field:type}}(p)?{{! }}{{#field:has_adapter?}}, {{field:key}})?{{/field:has_adapter?}}{{! }})); }{{! }}{{/struct:fields}} (fty, _, false) => p.skip(fty)?, (badty, badid, true) => return ::std::result::Result::Err(::std::convert::From::from(::fbthrift::ProtocolError::UnwantedExtraUnionField( "{{struct:name}}".to_string(), badty, badid, ))), } p.read_field_end()?; } p.read_struct_end()?; ::std::result::Result::Ok(alt.unwrap_or_default()) } } impl {{struct:rust_name}} { /// Return current union variant name as a tuple of (Rust name, original name). pub fn variant_name(&self) -> Option<(&'static str, &'static str)> { match self { {{#struct:fields}} Self::{{field:rust_name}}(_) => Some(("{{field:rust_name}}", "{{field:name}}")), {{/struct:fields}} Self::UnknownField(_) => None, } } } {{#program:rust_gen_native_metadata?}} {{>lib/metadata/struct}} {{/program:rust_gen_native_metadata?}} {{/struct:union?}} {{!newline}}