Skip to content

Commit

Permalink
generator: Replace Extends{Root} with StructExtends<Root> generic
Browse files Browse the repository at this point in the history
Instead of emitting a new `trait` for every `Root` struct that is being
implemented by one or more "child" structs (those that have `Root` in
their `structextends`), create one trait that takes the root struct as a
generic parameter, and implement that directly instead.

This not only saves on having to define the `trait` for every
`Root` struct but also paves the way towards providing default trait
implementations for any pair of root and child struct, such as the
`p_next` builder methods.
  • Loading branch information
MarijnS95 committed Dec 8, 2024
1 parent 92a0ee6 commit 1cf5047
Show file tree
Hide file tree
Showing 5 changed files with 1,793 additions and 1,964 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `get_pipeline_executable_statistics()`.
The expected length of this array can be queried with the respective `*_len()` variant of these functions.
- `push_next()` has been renamed to `extend()` and marked as `unsafe`. Users are encouraged to call `push()` for singular structs instead. (#909)
- All `Extends{Root}` traits have been replaced with a single `Extends<Root>` trait using generics. (#971)

### Removed

Expand Down
53 changes: 53 additions & 0 deletions ash/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,57 @@ mod tests {
};
assert_eq!(chain, chain2);
}
#[test]
#[should_panic]
fn disallow_nested_ptr_chains() {
let mut generated_commands =
vk::PhysicalDeviceDeviceGeneratedCommandsFeaturesEXT::default();
let mut private_data = vk::PhysicalDevicePrivateDataFeatures {
p_next: <*mut _>::cast(&mut generated_commands),
..Default::default()
};
let _device_create_info = vk::DeviceCreateInfo::default().push(&mut private_data);
}
#[test]
fn test_nested_ptr_chains() {
let mut generated_commands =
vk::PhysicalDeviceDeviceGeneratedCommandsFeaturesEXT::default();
let mut private_data = vk::PhysicalDevicePrivateDataFeatures {
p_next: <*mut _>::cast(&mut generated_commands),
..Default::default()
};
let mut variable_pointers = vk::PhysicalDeviceVariablePointerFeatures::default();
let mut corner = vk::PhysicalDeviceCornerSampledImageFeaturesNV::default();
let chain = alloc::vec![
<*mut _>::cast(&mut private_data),
<*mut _>::cast(&mut generated_commands),
<*mut _>::cast(&mut variable_pointers),
<*mut _>::cast(&mut corner),
];
let mut device_create_info = vk::DeviceCreateInfo::default()
.push(&mut corner)
.push(&mut variable_pointers);
// Insert private_data->generated_commands into the chain, such that generate_commands->variable_pointers->corner:
device_create_info = unsafe { device_create_info.extend(&mut private_data) };
let chain2: Vec<*mut vk::BaseOutStructure<'_>> = unsafe {
vk::ptr_chain_iter(&mut device_create_info)
.skip(1)
.collect()
};
assert_eq!(chain, chain2);
}
#[test]
fn test_dynamic_add_to_ptr_chain() {
let mut variable_pointers = vk::PhysicalDeviceVariablePointerFeatures::default();
let variable_pointers: &mut dyn vk::Extends<vk::DeviceCreateInfo<'_>> =
&mut variable_pointers;
let chain = alloc::vec![<*mut _>::cast(variable_pointers)];
let mut device_create_info = vk::DeviceCreateInfo::default().push(variable_pointers);
let chain2: Vec<*mut vk::BaseOutStructure<'_>> = unsafe {
vk::ptr_chain_iter(&mut device_create_info)
.skip(1)
.collect()
};
assert_eq!(chain, chain2);
}
}
Loading

0 comments on commit 1cf5047

Please sign in to comment.