From f1b959615247b084b87bca738f8abcf165cb880e Mon Sep 17 00:00:00 2001 From: Ilya Zlobintsev Date: Sat, 19 Nov 2022 12:32:03 +0200 Subject: [PATCH] Implement IntoIterator for `Features`, `DeviceExtensions` and `InstanceExtensions` (#2090) * Add as_arr functions to Features and DeviceExtensions * make as_arr const * unit tests for as_arr * also add test for as_arr on InstanceExtensions * make return str static * use IntoIterator instead of as_arr --- vulkano/autogen/extensions.rs | 17 +++++++++++++++++ vulkano/autogen/features.rs | 16 ++++++++++++++++ vulkano/src/device/extensions.rs | 15 +++++++++++++++ vulkano/src/device/features.rs | 20 ++++++++++++++++++++ vulkano/src/instance/extensions.rs | 15 +++++++++++++++ 5 files changed, 83 insertions(+) diff --git a/vulkano/autogen/extensions.rs b/vulkano/autogen/extensions.rs index 66a781a0..953e5681 100644 --- a/vulkano/autogen/extensions.rs +++ b/vulkano/autogen/extensions.rs @@ -338,6 +338,13 @@ fn extensions_common_output(struct_name: Ident, members: &[ExtensionsMember]) -> } }); + let arr_items = members.iter().map(|ExtensionsMember { name, raw, .. }| { + quote! { + (#raw, self.#name), + } + }); + let arr_len = members.len(); + let from_str_for_extensions_items = members.iter().map(|ExtensionsMember { name, raw, .. }| { let raw = Literal::string(raw); @@ -540,6 +547,16 @@ fn extensions_common_output(struct_name: Ident, members: &[ExtensionsMember]) -> data } } + + impl IntoIterator for #struct_name { + type Item = (&'static str, bool); + type IntoIter = std::array::IntoIter; + + #[inline] + fn into_iter(self) -> Self::IntoIter { + [#(#arr_items)*].into_iter() + } + } } } diff --git a/vulkano/autogen/features.rs b/vulkano/autogen/features.rs index 6ae9837e..ec8105d7 100644 --- a/vulkano/autogen/features.rs +++ b/vulkano/autogen/features.rs @@ -216,6 +216,13 @@ fn features_output(members: &[FeaturesMember]) -> TokenStream { } }); + let arr_items = members.iter().map(|FeaturesMember { name, raw, .. }| { + quote! { + (#raw, self.#name), + } + }); + let arr_len = members.len(); + let write_items = members.iter().map( |FeaturesMember { name, @@ -508,6 +515,15 @@ fn features_output(members: &[FeaturesMember]) -> TokenStream { } } + impl IntoIterator for Features { + type Item = (&'static str, bool); + type IntoIter = std::array::IntoIter; + + #[inline] + fn into_iter(self) -> Self::IntoIter { + [#(#arr_items)*].into_iter() + } + } } } diff --git a/vulkano/src/device/extensions.rs b/vulkano/src/device/extensions.rs index 124c1c7e..a4b29c1d 100644 --- a/vulkano/src/device/extensions.rs +++ b/vulkano/src/device/extensions.rs @@ -22,4 +22,19 @@ mod tests { let d: Vec = (&DeviceExtensions::empty()).into(); assert!(d.get(0).is_none()); } + + #[test] + fn into_iter() { + let extensions = DeviceExtensions { + khr_swapchain: true, + ..DeviceExtensions::empty() + }; + for (name, enabled) in extensions { + if name == "VK_KHR_swapchain" { + assert!(enabled); + } else { + assert!(!enabled); + } + } + } } diff --git a/vulkano/src/device/features.rs b/vulkano/src/device/features.rs index 4a210fa1..592ce3b7 100644 --- a/vulkano/src/device/features.rs +++ b/vulkano/src/device/features.rs @@ -66,3 +66,23 @@ impl Display for FeatureRestriction { } } } + +#[cfg(test)] +mod tests { + use super::Features; + + #[test] + fn into_iter() { + let features = Features { + tessellation_shader: true, + ..Features::empty() + }; + for (name, enabled) in features { + if name == "tessellationShader" { + assert!(enabled); + } else { + assert!(!enabled); + } + } + } +} diff --git a/vulkano/src/instance/extensions.rs b/vulkano/src/instance/extensions.rs index 6be3a644..3aac8447 100644 --- a/vulkano/src/instance/extensions.rs +++ b/vulkano/src/instance/extensions.rs @@ -22,4 +22,19 @@ mod tests { let i: Vec = (&InstanceExtensions::empty()).into(); assert!(i.get(0).is_none()); } + + #[test] + fn into_iter() { + let extensions = InstanceExtensions { + khr_display: true, + ..InstanceExtensions::empty() + }; + for (name, enabled) in extensions { + if name == "VK_KHR_display" { + assert!(enabled); + } else { + assert!(!enabled); + } + } + } }