Skip to content

Commit 187016a

Browse files
committed
feat: Add basic generics support for structs
1 parent 6894c1d commit 187016a

File tree

1 file changed

+15
-5
lines changed
  • crates/stackable-versioned-macros/src/codegen/container

1 file changed

+15
-5
lines changed

crates/stackable-versioned-macros/src/codegen/container/struct.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,18 @@ pub(crate) struct Struct {
127127
/// should generate code, is decided by the currently generated version.
128128
pub fields: Vec<VersionedField>,
129129

130-
pub generics: Generics,
131-
132130
/// Common container data which is shared between structs and enums.
133131
pub common: CommonContainerData,
132+
133+
/// Generic types of the struct
134+
pub generics: Generics,
134135
}
135136

136137
// Common token generation
137138
impl Struct {
138139
/// Generates code for the struct definition.
139140
pub(crate) fn generate_definition(&self, version: &VersionDefinition) -> TokenStream {
141+
let (_, type_generics, where_clause) = self.generics.split_for_impl();
140142
let original_attributes = &self.common.original_attributes;
141143
let ident = &self.common.idents.original;
142144
let version_docs = &version.docs;
@@ -153,7 +155,7 @@ impl Struct {
153155
#(#[doc = #version_docs])*
154156
#(#original_attributes)*
155157
#kube_attribute
156-
pub struct #ident {
158+
pub struct #ident #type_generics #where_clause {
157159
#fields
158160
}
159161
}
@@ -172,6 +174,12 @@ impl Struct {
172174

173175
match next_version {
174176
Some(next_version) => {
177+
// TODO (@Techassi): Support generic types which have been removed in newer versions,
178+
// but need to exist for older versions How do we represent that? Because the
179+
// defined struct always represents the latest version. I guess we could generally
180+
// advise against using generic types, but if you have to, avoid removing it in
181+
// later versions.
182+
let (impl_generics, type_generics, where_clause) = self.generics.split_for_impl();
175183
let struct_ident = &self.common.idents.original;
176184
let from_struct_ident = &self.common.idents.from;
177185

@@ -198,8 +206,10 @@ impl Struct {
198206
Some(quote! {
199207
#automatically_derived
200208
#allow_attribute
201-
impl ::std::convert::From<#from_module_ident::#struct_ident> for #for_module_ident::#struct_ident {
202-
fn from(#from_struct_ident: #from_module_ident::#struct_ident) -> Self {
209+
impl #impl_generics ::std::convert::From<#from_module_ident::#struct_ident #type_generics> for #for_module_ident::#struct_ident #type_generics
210+
#where_clause
211+
{
212+
fn from(#from_struct_ident: #from_module_ident::#struct_ident #type_generics) -> Self {
203213
Self {
204214
#fields
205215
}

0 commit comments

Comments
 (0)