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
This commit is contained in:
Ilya Zlobintsev 2022-11-19 12:32:03 +02:00 committed by GitHub
parent 708c7799b1
commit f1b9596152
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 83 additions and 0 deletions

View File

@ -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<Self::Item, #arr_len>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
[#(#arr_items)*].into_iter()
}
}
}
}

View File

@ -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<Self::Item, #arr_len>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
[#(#arr_items)*].into_iter()
}
}
}
}

View File

@ -22,4 +22,19 @@ mod tests {
let d: Vec<CString> = (&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);
}
}
}
}

View File

@ -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);
}
}
}
}

View File

@ -22,4 +22,19 @@ mod tests {
let i: Vec<CString> = (&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);
}
}
}
}