Merge pull request #19824 from github/redsun82/rust-derive-macro-expansion

Rust: expand derive macros
This commit is contained in:
Paolo Tranquilli
2025-06-23 09:42:49 +02:00
committed by GitHub
62 changed files with 18469 additions and 979 deletions

View File

@@ -321,7 +321,7 @@ drop = object()
def annotate(
annotated_cls: type,
add_bases: _Iterable[type] | None = None,
replace_bases: _Dict[type, type] | None = None,
replace_bases: _Dict[type, type | None] | None = None,
cfg: bool = False,
) -> _Callable[[type], _PropertyModifierList]:
"""
@@ -329,7 +329,8 @@ def annotate(
The name of the class used for annotation must be `_`.
`replace_bases` can be used to replace bases on the annotated class.
`replace_bases` can be used to replace bases on the annotated class. Mapping to
`None` will remove that base class.
"""
def decorator(cls: type) -> _PropertyModifierList:
@@ -341,7 +342,9 @@ def annotate(
_ClassPragma(p, value=v)(annotated_cls)
if replace_bases:
annotated_cls.__bases__ = tuple(
replace_bases.get(b, b) for b in annotated_cls.__bases__
b
for b in (replace_bases.get(b, b) for b in annotated_cls.__bases__)
if b is not None
)
if add_bases:
annotated_cls.__bases__ += tuple(add_bases)

View File

@@ -66,6 +66,20 @@ impl {{name}} {
pub fn emit_{{singular_field_name}}(id: trap::Label<Self>{{^is_predicate}}{{#is_repeated}}{{^is_unordered}}, i: usize{{/is_unordered}}{{/is_repeated}}, value: {{base_type}}{{/is_predicate}}, out: &mut trap::Writer) {
out.add_tuple("{{table_name}}", vec![id.into(){{^is_predicate}}{{#is_repeated}}{{^is_unordered}}, i.into(){{/is_unordered}}{{/is_repeated}}, value.into(){{/is_predicate}}]);
}
{{#is_repeated}}
pub fn emit_{{field_name}}(id: trap::Label<Self>, values: impl IntoIterator<Item={{base_type}}>, out: &mut trap::Writer) {
values
.into_iter()
{{^is_unordered}}
.enumerate()
.for_each(|(i, value)| Self::emit_{{singular_field_name}}(id, i, value, out));
{{/is_unordered}}
{{#is_unordered}}
.for_each(|value| Self::emit_{{singular_field_name}}(id, value, out));
{{/is_unordered}}
}
{{/is_repeated}}
{{/detached_fields}}
}
{{/has_detached_fields}}