Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Implement extension chaining #183

Merged
merged 21 commits into from
Mar 10, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions examples/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,7 @@ impl ExampleBase {
.variable_pointers(true)
.build();
let mut corner = vk::PhysicalDeviceCornerSampledImageFeaturesNV::builder()
.corner_sampled_image(true)
.build();
.corner_sampled_image(true);
let mut device_create_info = vk::DeviceCreateInfo::builder()
.push_next(&mut corner)
.push_next(&mut variable_pointers)
Expand Down
20 changes: 15 additions & 5 deletions generator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1591,14 +1591,13 @@ pub fn derive_setters(
name_to_tokens(&format!("Extends{}", name_to_tokens(&extends)))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we be implementing the extension trait of every struct that this struct extends, rather than just an arbitrary one?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by arbitrary one? Only root create infos will create a new trait, and only non root create infos that extend a root create info will implement this trait.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I'm missing something, this seems to be used for deciding when to implement the trait, not when to define it. A single extension struct (e.g. VkPhysicalDeviceVariablePointerFeatures) can extend multiple root structs, and must implement all of their extension traits.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes that is what I wanted to implement. I just chose the first one.

});


// We only implement a next methods for root create infos
let next_function = if has_next && next_extends.is_none() {
quote! {
/// Prepend
pub fn push_next<T: #extends_name>(mut self, next: &'a mut T) -> #name_builder<'a> {
pub fn push_next<T: #extends_name>(mut self, next: &mut T) -> #name_builder<'a> {
unsafe{
let next_ptr: *mut BaseOutStructure = ::std::mem::transmute(next);
let next_ptr = next.as_ptr_mut();
(*next_ptr).p_next = self.inner.p_next as _;
self.inner.p_next = next_ptr as _;
}
Expand All @@ -1613,7 +1612,9 @@ pub fn derive_setters(
// implement
let next_trait = if has_next && _struct.extends.is_none() {
quote! {
pub unsafe trait #extends_name {}
pub unsafe trait #extends_name {
unsafe fn as_ptr_mut(&mut self) -> *mut BaseOutStructure;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of generating more code here, we should mark all builders as #[repr(transparent)] and coerce the pointers directly in push_next. This is legal because the builders contain only one non-zero-sized member.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a great idea, I completely forgot about #[repr(transparent)].

}
}
} else {
quote! {}
Expand All @@ -1622,7 +1623,16 @@ pub fn derive_setters(
// If the struct extends something we need to implement the root create info trait.
let impl_extend_trait = if let Some(next_extends) = next_extends {
quote! {
unsafe impl #next_extends for #name {}
unsafe impl #next_extends for #name_builder<'_> {
unsafe fn as_ptr_mut(&mut self) -> *mut BaseOutStructure{
::std::mem::transmute(&mut self.inner)
}
}
unsafe impl #next_extends for #name {
unsafe fn as_ptr_mut(&mut self) -> *mut BaseOutStructure{
::std::mem::transmute(self)
}
}
}
} else {
quote! {}
Expand Down