From 9b4470f9e5f6f39a08e19b11724d04c48ad65c97 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Thu, 30 Jan 2025 18:23:22 -0800 Subject: [PATCH] Factor out annotated type generics to separate template Summary: The new `{{> lib/type_generics}}` template generates the `` in Vec and BTreeSet, and the `` in BTreeMap, and in custom types specified through `rust.Type`. Beyond deduplicating the existing 2 uses (field types `{{field:type_rust}}`, service method argument types `{{type:rust}}`), I need to use this same logic a third time for typedef types `{{typedef:rust_type}}` in {D68909054}. Reviewed By: emersonford Differential Revision: D68927538 fbshipit-source-id: 9ceee895f092de98d97bf3222749cb86f1b3f618 --- .../templates/rust/lib/annfieldtype.mustache | 19 ++------ .../templates/rust/lib/rawtype.mustache | 17 ++------ .../templates/rust/lib/type_generics.mustache | 43 +++++++++++++++++++ 3 files changed, 49 insertions(+), 30 deletions(-) create mode 100644 thrift/compiler/generate/templates/rust/lib/type_generics.mustache diff --git a/thrift/compiler/generate/templates/rust/lib/annfieldtype.mustache b/thrift/compiler/generate/templates/rust/lib/annfieldtype.mustache index 247224affbd..99290f2a0bb 100644 --- a/thrift/compiler/generate/templates/rust/lib/annfieldtype.mustache +++ b/thrift/compiler/generate/templates/rust/lib/annfieldtype.mustache @@ -27,25 +27,12 @@ }}{{/type:struct}}{{! }}{{#type:enum}}{{enum:package}}::{{enum:rust_name}}{{/type:enum}}{{! }}{{#type:list?}} - }}::std::vec::Vec<{{#type:list_elem_type}}{{> lib/type}}{{/type:list_elem_type}}>{{! + }}::std::vec::Vec{{> lib/type_generics}}{{! }}{{/type:list?}}{{! }}{{#type:set?}}{{! - }}::{{field:type_rust}}{{! - }}<{{! - }}{{#type:set_elem_type}}{{! - }}{{#type:floating_point?}}::fbthrift::export::OrderedFloat<{{> lib/type}}>{{/type:floating_point?}}{{! - }}{{^type:floating_point?}}{{> lib/type}}{{/type:floating_point?}}{{! - }}{{/type:set_elem_type}}{{! - }}>{{! + }}::{{field:type_rust}}{{> lib/type_generics}}{{! }}{{/type:set?}}{{! }}{{#type:map?}}{{! - }}::{{field:type_rust}}{{! - }}<{{! - }}{{#type:key_type}}{{! - }}{{#type:floating_point?}}::fbthrift::export::OrderedFloat<{{> lib/type}}>{{/type:floating_point?}}{{! - }}{{^type:floating_point?}}{{> lib/type}}{{/type:floating_point?}}{{! - }}{{/type:key_type}}, {{! - }}{{#type:value_type}}{{> lib/type}}{{/type:value_type}}{{! - }}>{{! + }}::{{field:type_rust}}{{> lib/type_generics}}{{! }}{{/type:map?}}{{! }} diff --git a/thrift/compiler/generate/templates/rust/lib/rawtype.mustache b/thrift/compiler/generate/templates/rust/lib/rawtype.mustache index ab02fc490e1..208abc7a3a7 100644 --- a/thrift/compiler/generate/templates/rust/lib/rawtype.mustache +++ b/thrift/compiler/generate/templates/rust/lib/rawtype.mustache @@ -48,27 +48,16 @@ }}{{enum:package}}::{{enum:rust_name}}{{! }}{{/type:enum}}{{! }}{{#type:list?}}{{! - }}::std::vec::Vec<{{#type:list_elem_type}}{{> lib/type}}{{/type:list_elem_type}}>{{! + }}::std::vec::Vec{{> lib/type_generics}}{{! }}{{/type:list?}}{{! }}{{#type:set?}}{{! }}{{#type:rust}}::{{type:rust}}{{/type:rust}}{{! }}{{^type:rust}}::std::collections::BTreeSet{{/type:rust}}{{! - }}<{{! - }}{{#type:set_elem_type}}{{! - }}{{#type:floating_point?}}::fbthrift::export::OrderedFloat<{{> lib/type}}>{{/type:floating_point?}}{{! - }}{{^type:floating_point?}}{{> lib/type}}{{/type:floating_point?}}{{! - }}{{/type:set_elem_type}}{{! - }}>{{! + }}{{> lib/type_generics}}{{! }}{{/type:set?}}{{! }}{{#type:map?}}{{! }}{{#type:rust}}::{{type:rust}}{{/type:rust}}{{! }}{{^type:rust}}::std::collections::BTreeMap{{/type:rust}}{{! - }}<{{! - }}{{#type:key_type}}{{! - }}{{#type:floating_point?}}::fbthrift::export::OrderedFloat<{{> lib/type}}>{{/type:floating_point?}}{{! - }}{{^type:floating_point?}}{{> lib/type}}{{/type:floating_point?}}{{! - }}{{/type:key_type}}, {{! - }}{{#type:value_type}}{{> lib/type}}{{/type:value_type}}{{! - }}>{{! + }}{{> lib/type_generics}}{{! }}{{/type:map?}}{{! }} diff --git a/thrift/compiler/generate/templates/rust/lib/type_generics.mustache b/thrift/compiler/generate/templates/rust/lib/type_generics.mustache new file mode 100644 index 00000000000..9b2b10e4eb9 --- /dev/null +++ b/thrift/compiler/generate/templates/rust/lib/type_generics.mustache @@ -0,0 +1,43 @@ +{{! + + Copyright (c) Meta Platforms, Inc. and 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. + +}}{{#type:list?}}{{! + }}<{{! + }}{{#type:list_elem_type}}{{! + }}{{> lib/type}}{{! + }}{{/type:list_elem_type}}{{! + }}>{{! +}}{{/type:list?}}{{! +}}{{#type:set?}}{{! + }}<{{! + }}{{#type:set_elem_type}}{{! + }}{{#type:floating_point?}}::fbthrift::export::OrderedFloat<{{> lib/type}}>{{/type:floating_point?}}{{! + }}{{^type:floating_point?}}{{> lib/type}}{{/type:floating_point?}}{{! + }}{{/type:set_elem_type}}{{! + }}>{{! +}}{{/type:set?}}{{! +}}{{#type:map?}}{{! + }}<{{! + }}{{#type:key_type}}{{! + }}{{#type:floating_point?}}::fbthrift::export::OrderedFloat<{{> lib/type}}>{{/type:floating_point?}}{{! + }}{{^type:floating_point?}}{{> lib/type}}{{/type:floating_point?}}{{! + }}{{/type:key_type}}, {{! + }}{{#type:value_type}}{{! + }}{{> lib/type}}{{! + }}{{/type:value_type}}{{! + }}>{{! +}}{{/type:map?}}{{! +}}