Skip to content

Commit

Permalink
Factor out annotated type generics to separate template
Browse files Browse the repository at this point in the history
Summary:
The new `{{> lib/type_generics}}` template generates the `<T>` in Vec<T> and BTreeSet<T>, and the `<T, U>` in BTreeMap<T, U>, 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
  • Loading branch information
David Tolnay authored and facebook-github-bot committed Jan 31, 2025
1 parent 5a84514 commit 9b4470f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 30 deletions.
19 changes: 3 additions & 16 deletions thrift/compiler/generate/templates/rust/lib/annfieldtype.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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?}}{{!
}}
17 changes: 3 additions & 14 deletions thrift/compiler/generate/templates/rust/lib/rawtype.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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?}}{{!
}}
43 changes: 43 additions & 0 deletions thrift/compiler/generate/templates/rust/lib/type_generics.mustache
Original file line number Diff line number Diff line change
@@ -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?}}{{!
}}

0 comments on commit 9b4470f

Please sign in to comment.