Skip to content

Commit

Permalink
Make ZeroOrMore take Iterator instead of AsRef<[T]> (#149)
Browse files Browse the repository at this point in the history
* ZeroOrMore takes Iterator instead of AsRef<[T]>

This is useful if the user has an iterator instead of a slice/vec, it
saves an allocation.
  • Loading branch information
khyperia authored Aug 22, 2020
1 parent eadc601 commit 16265b8
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 283 deletions.
18 changes: 9 additions & 9 deletions autogen/dr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn get_param_list(
structs::Quantifier::One => quote! { #name: #kind },
structs::Quantifier::ZeroOrOne => quote! { #name: Option<#kind> },
structs::Quantifier::ZeroOrMore => {
type_generics = quote! { <T: AsRef<[#kind]>> };
type_generics = quote! { <T: IntoIterator<Item = #kind>> };
quote! { #name: T }
}
})
Expand All @@ -57,7 +57,7 @@ fn get_param_list(
// The last operand may require additional parameters.
if let Some(o) = params.last() {
if operand_has_additional_params(o, kinds) {
type_generics = quote! { <T: AsRef<[dr::Operand]>> };
type_generics = quote! { <T: IntoIterator<Item = dr::Operand>> };
list.push(quote! { additional_params: T });
}
}
Expand Down Expand Up @@ -140,29 +140,29 @@ fn get_push_extras(
// still doesn't solve 64-bit selectors in OpSwitch.
if param.kind == "PairLiteralIntegerIdRef" {
Some(quote! {
for v in #name.as_ref() {
for v in #name {
#container.push(dr::Operand::LiteralInt32(v.0));
#container.push(dr::Operand::IdRef(v.1));
}
})
} else if param.kind == "PairIdRefLiteralInteger" {
Some(quote! {
for v in #name.as_ref() {
for v in #name {
#container.push(dr::Operand::IdRef(v.0));
#container.push(dr::Operand::LiteralInt32(v.1));
}
})
} else if param.kind == "PairIdRefIdRef" {
Some(quote! {
for v in #name.as_ref() {
for v in #name {
#container.push(dr::Operand::IdRef(v.0));
#container.push(dr::Operand::IdRef(v.1));
}
})
} else {
let kind = get_dr_operand_kind(&param.kind);
Some(quote! {
#container.extend(#name.as_ref().iter().cloned().map(dr::Operand::#kind));
#container.extend(#name.into_iter().map(dr::Operand::#kind));
})
}
}
Expand All @@ -173,7 +173,7 @@ fn get_push_extras(
if let Some(o) = params.last() {
if operand_has_additional_params(o, kinds) {
list.push(quote! {
#container.extend_from_slice(additional_params.as_ref());
#container.extend(additional_params);
});
}
}
Expand All @@ -182,7 +182,7 @@ fn get_push_extras(

/// Returns the generated dr::Operand and its fmt::Display implementation by
/// walking the given SPIR-V operand kinds `grammar`.
pub fn gen_dr_operand_kinds(grammar: &Vec<structs::OperandKind>) -> TokenStream {
pub fn gen_dr_operand_kinds(grammar: &[structs::OperandKind]) -> TokenStream {
let kinds: Vec<_> = grammar
.iter()
.map(|element| element.kind.as_str())
Expand Down Expand Up @@ -265,7 +265,7 @@ pub fn gen_dr_operand_kinds(grammar: &Vec<structs::OperandKind>) -> TokenStream
.map(as_ident),
);
let cases = kinds.iter().map(|element| {
if &element.to_string() == &"Dim" {
if element.to_string() == "Dim" {
// Skip the "Dim" prefix, which is only used in the API to
// avoid having an enumerant name starting with a number
quote! {
Expand Down
36 changes: 18 additions & 18 deletions rspirv/dr/build/autogen_annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

impl Builder {
#[doc = "Appends an OpDecorate instruction."]
pub fn decorate<T: AsRef<[dr::Operand]>>(
pub fn decorate<T: IntoIterator<Item = dr::Operand>>(
&mut self,
target: spirv::Word,
decoration: spirv::Decoration,
Expand All @@ -20,11 +20,11 @@ impl Builder {
dr::Operand::Decoration(decoration),
],
);
inst.operands.extend_from_slice(additional_params.as_ref());
inst.operands.extend(additional_params);
self.module.annotations.push(inst);
}
#[doc = "Appends an OpMemberDecorate instruction."]
pub fn member_decorate<T: AsRef<[dr::Operand]>>(
pub fn member_decorate<T: IntoIterator<Item = dr::Operand>>(
&mut self,
structure_type: spirv::Word,
member: u32,
Expand All @@ -42,11 +42,11 @@ impl Builder {
dr::Operand::Decoration(decoration),
],
);
inst.operands.extend_from_slice(additional_params.as_ref());
inst.operands.extend(additional_params);
self.module.annotations.push(inst);
}
#[doc = "Appends an OpGroupDecorate instruction."]
pub fn group_decorate<T: AsRef<[spirv::Word]>>(
pub fn group_decorate<T: IntoIterator<Item = spirv::Word>>(
&mut self,
decoration_group: spirv::Word,
targets: T,
Expand All @@ -59,11 +59,11 @@ impl Builder {
vec![dr::Operand::IdRef(decoration_group)],
);
inst.operands
.extend(targets.as_ref().iter().cloned().map(dr::Operand::IdRef));
.extend(targets.into_iter().map(dr::Operand::IdRef));
self.module.annotations.push(inst);
}
#[doc = "Appends an OpGroupMemberDecorate instruction."]
pub fn group_member_decorate<T: AsRef<[(spirv::Word, u32)]>>(
pub fn group_member_decorate<T: IntoIterator<Item = (spirv::Word, u32)>>(
&mut self,
decoration_group: spirv::Word,
targets: T,
Expand All @@ -75,14 +75,14 @@ impl Builder {
None,
vec![dr::Operand::IdRef(decoration_group)],
);
for v in targets.as_ref() {
for v in targets {
inst.operands.push(dr::Operand::IdRef(v.0));
inst.operands.push(dr::Operand::LiteralInt32(v.1));
}
self.module.annotations.push(inst);
}
#[doc = "Appends an OpDecorateId instruction."]
pub fn decorate_id<T: AsRef<[dr::Operand]>>(
pub fn decorate_id<T: IntoIterator<Item = dr::Operand>>(
&mut self,
target: spirv::Word,
decoration: spirv::Decoration,
Expand All @@ -98,11 +98,11 @@ impl Builder {
dr::Operand::Decoration(decoration),
],
);
inst.operands.extend_from_slice(additional_params.as_ref());
inst.operands.extend(additional_params);
self.module.annotations.push(inst);
}
#[doc = "Appends an OpDecorateString instruction."]
pub fn decorate_string<T: AsRef<[dr::Operand]>>(
pub fn decorate_string<T: IntoIterator<Item = dr::Operand>>(
&mut self,
target: spirv::Word,
decoration: spirv::Decoration,
Expand All @@ -118,11 +118,11 @@ impl Builder {
dr::Operand::Decoration(decoration),
],
);
inst.operands.extend_from_slice(additional_params.as_ref());
inst.operands.extend(additional_params);
self.module.annotations.push(inst);
}
#[doc = "Appends an OpDecorateStringGOOGLE instruction."]
pub fn decorate_string_google<T: AsRef<[dr::Operand]>>(
pub fn decorate_string_google<T: IntoIterator<Item = dr::Operand>>(
&mut self,
target: spirv::Word,
decoration: spirv::Decoration,
Expand All @@ -138,11 +138,11 @@ impl Builder {
dr::Operand::Decoration(decoration),
],
);
inst.operands.extend_from_slice(additional_params.as_ref());
inst.operands.extend(additional_params);
self.module.annotations.push(inst);
}
#[doc = "Appends an OpMemberDecorateString instruction."]
pub fn member_decorate_string<T: AsRef<[dr::Operand]>>(
pub fn member_decorate_string<T: IntoIterator<Item = dr::Operand>>(
&mut self,
struct_type: spirv::Word,
member: u32,
Expand All @@ -160,11 +160,11 @@ impl Builder {
dr::Operand::Decoration(decoration),
],
);
inst.operands.extend_from_slice(additional_params.as_ref());
inst.operands.extend(additional_params);
self.module.annotations.push(inst);
}
#[doc = "Appends an OpMemberDecorateStringGOOGLE instruction."]
pub fn member_decorate_string_google<T: AsRef<[dr::Operand]>>(
pub fn member_decorate_string_google<T: IntoIterator<Item = dr::Operand>>(
&mut self,
struct_type: spirv::Word,
member: u32,
Expand All @@ -182,7 +182,7 @@ impl Builder {
dr::Operand::Decoration(decoration),
],
);
inst.operands.extend_from_slice(additional_params.as_ref());
inst.operands.extend(additional_params);
self.module.annotations.push(inst);
}
}
22 changes: 6 additions & 16 deletions rspirv/dr/build/autogen_constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl Builder {
id
}
#[doc = "Appends an OpConstantComposite instruction."]
pub fn constant_composite<T: AsRef<[spirv::Word]>>(
pub fn constant_composite<T: IntoIterator<Item = spirv::Word>>(
&mut self,
result_type: spirv::Word,
constituents: T,
Expand All @@ -39,13 +39,8 @@ impl Builder {
Some(id),
vec![],
);
inst.operands.extend(
constituents
.as_ref()
.iter()
.cloned()
.map(dr::Operand::IdRef),
);
inst.operands
.extend(constituents.into_iter().map(dr::Operand::IdRef));
self.module.types_global_values.push(inst);
id
}
Expand Down Expand Up @@ -108,7 +103,7 @@ impl Builder {
id
}
#[doc = "Appends an OpSpecConstantComposite instruction."]
pub fn spec_constant_composite<T: AsRef<[spirv::Word]>>(
pub fn spec_constant_composite<T: IntoIterator<Item = spirv::Word>>(
&mut self,
result_type: spirv::Word,
constituents: T,
Expand All @@ -121,13 +116,8 @@ impl Builder {
Some(id),
vec![],
);
inst.operands.extend(
constituents
.as_ref()
.iter()
.cloned()
.map(dr::Operand::IdRef),
);
inst.operands
.extend(constituents.into_iter().map(dr::Operand::IdRef));
self.module.types_global_values.push(inst);
id
}
Expand Down
Loading

0 comments on commit 16265b8

Please sign in to comment.