generator: Use Self as return type in builder functions (#418)

Do not Repeat Yourself in `impl` blocks, especially with the extra
lifetime these are unnecessarily verbose.
This commit is contained in:
Marijn Suijten 2021-04-17 22:25:06 +02:00 committed by GitHub
parent 14d69087cb
commit 9dcfbd2bf1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 2264 additions and 5813 deletions

File diff suppressed because it is too large Load diff

View file

@ -1733,7 +1733,7 @@ pub fn derive_setters(
// Unique cases // Unique cases
if name == "pCode" { if name == "pCode" {
return Some(quote!{ return Some(quote!{
pub fn code(mut self, code: &'a [u32]) -> #name_builder<'a> { pub fn code(mut self, code: &'a [u32]) -> Self {
self.inner.code_size = code.len() * 4; self.inner.code_size = code.len() * 4;
self.inner.p_code = code.as_ptr() as *const u32; self.inner.p_code = code.as_ptr() as *const u32;
self self
@ -1743,7 +1743,7 @@ pub fn derive_setters(
if name == "pSampleMask" { if name == "pSampleMask" {
return Some(quote!{ return Some(quote!{
pub fn sample_mask(mut self, sample_mask: &'a [SampleMask]) -> #name_builder<'a> { pub fn sample_mask(mut self, sample_mask: &'a [SampleMask]) -> Self {
self.inner.p_sample_mask = sample_mask.as_ptr() as *const SampleMask; self.inner.p_sample_mask = sample_mask.as_ptr() as *const SampleMask;
self self
} }
@ -1752,7 +1752,7 @@ pub fn derive_setters(
if name == "ppGeometries" { if name == "ppGeometries" {
return Some(quote!{ return Some(quote!{
pub fn geometries_ptrs(mut self, geometries: &'a [*const AccelerationStructureGeometryKHR]) -> #name_builder<'a> { pub fn geometries_ptrs(mut self, geometries: &'a [*const AccelerationStructureGeometryKHR]) -> Self {
self.inner.geometry_count = geometries.len() as _; self.inner.geometry_count = geometries.len() as _;
self.inner.pp_geometries = geometries.as_ptr(); self.inner.pp_geometries = geometries.as_ptr();
self self
@ -1767,7 +1767,7 @@ pub fn derive_setters(
assert!(field.null_terminate); assert!(field.null_terminate);
assert_eq!(field.size, None); assert_eq!(field.size, None);
return Some(quote!{ return Some(quote!{
pub fn #param_ident_short(mut self, #param_ident_short: &'a ::std::ffi::CStr) -> #name_builder<'a> { pub fn #param_ident_short(mut self, #param_ident_short: &'a ::std::ffi::CStr) -> Self {
self.inner.#param_ident = #param_ident_short.as_ptr(); self.inner.#param_ident = #param_ident_short.as_ptr();
self self
} }
@ -1812,7 +1812,7 @@ pub fn derive_setters(
}; };
return Some(quote! { return Some(quote! {
pub fn #param_ident_short(mut self, #param_ident_short: &'a #mutable #slice_param_ty_tokens) -> #name_builder<'a> { pub fn #param_ident_short(mut self, #param_ident_short: &'a #mutable #slice_param_ty_tokens) -> Self {
#set_size_stmt #set_size_stmt
self.inner.#param_ident = #param_ident_short#ptr; self.inner.#param_ident = #param_ident_short#ptr;
self self
@ -1825,7 +1825,7 @@ pub fn derive_setters(
if field.basetype == "VkBool32" { if field.basetype == "VkBool32" {
return Some(quote!{ return Some(quote!{
pub fn #param_ident_short(mut self, #param_ident_short: bool) -> #name_builder<'a> { pub fn #param_ident_short(mut self, #param_ident_short: bool) -> Self {
self.inner.#param_ident = #param_ident_short.into(); self.inner.#param_ident = #param_ident_short.into();
self self
} }
@ -1840,7 +1840,7 @@ pub fn derive_setters(
}; };
Some(quote!{ Some(quote!{
pub fn #param_ident_short(mut self, #param_ident_short: #param_ty_tokens) -> #name_builder<'a> { pub fn #param_ident_short(mut self, #param_ident_short: #param_ty_tokens) -> Self {
self.inner.#param_ident = #param_ident_short; self.inner.#param_ident = #param_ident_short;
self self
} }
@ -1869,7 +1869,7 @@ pub fn derive_setters(
/// valid extension structs can be pushed into the chain. /// valid extension structs can be pushed into the chain.
/// If the chain looks like `A -> B -> C`, and you call `builder.push_next(&mut D)`, then the /// If the chain looks like `A -> B -> C`, and you call `builder.push_next(&mut D)`, then the
/// chain will look like `A -> D -> B -> C`. /// chain will look like `A -> D -> B -> C`.
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: &'a mut T) -> Self {
unsafe{ unsafe{
let next_ptr = next as *mut T as *mut BaseOutStructure; let next_ptr = next as *mut T as *mut BaseOutStructure;
// `next` here can contain a pointer chain. This means that we must correctly // `next` here can contain a pointer chain. This means that we must correctly