1- use  proc_macro2:: { Literal ,  Ident ,  TokenStream } ; 
1+ use  crate :: column:: column_codec:: * ; 
2+ use  crate :: macro_utils; 
3+ use  crate :: macro_utils:: InnerKind ; 
4+ use  proc_macro2:: { Ident ,  Literal ,  TokenStream } ; 
25use  quote:: quote; 
3- use  syn:: { parse_str,  Attribute ,  Path ,  Type } ; 
46use  syn:: punctuated:: Punctuated ; 
57use  syn:: token:: Comma ; 
6- use  crate :: column:: column_codec:: * ; 
7- use  crate :: macro_utils:: InnerKind ; 
8+ use  syn:: { parse_str,  Attribute ,  FieldsNamed ,  Path ,  Type } ; 
89
910pub  fn  generate_column_impls ( 
1011    struct_ident :  & Ident , 
1112    new_type :  & Type , 
1213    inner_type :  & Type , 
1314    binary_encoding_opt :  Option < String > , 
1415)  -> ( TokenStream ,  Option < Attribute > ,  Punctuated < Path ,  Comma > )  { 
15-     let  kind = crate :: macro_utils:: classify_inner_type ( inner_type) ; 
16+     let  kind = macro_utils:: classify_inner_type ( inner_type) ; 
1617
1718    let  binary_encoding = binary_encoding_opt. unwrap_or_else ( || "hex" . to_string ( ) ) ; 
1819    let  mut  schema_example = quote !  {  vec![ Some ( json!( #struct_ident:: default ( ) . url_encode( ) ) ) ]  } ; 
@@ -21,7 +22,7 @@ pub fn generate_column_impls(
2122    let  mut  schema_type = quote !  {  SchemaType :: Type ( Type :: String )  } ; 
2223    let  mut  default_code = quote !  {  Self ( Default :: default ( ) )  } ; 
2324    let  mut  url_encoded_code = quote !  {  format!( "{}" ,  self . 0 )  } ; 
24-     let  mut  iterable_code = quote !  {  compile_error!( "IterableColumn ::next is not supported for this type." )  } ; 
25+     let  mut  iterable_code = quote !  {  compile_error!( "Sampleable ::next is not supported for this type." )  } ; 
2526    let  mut  custom_db_codec = quote !  { } ; 
2627    let  mut  cache_key_codec = quote !  { } ; 
2728
@@ -180,7 +181,7 @@ pub fn generate_column_impls(
180181            } 
181182        } 
182183
183-         impl  IterableColumn  for  #struct_ident { 
184+         impl  Sampleable  for  #struct_ident { 
184185            fn  next_value( & self )  -> Self  { 
185186                #iterable_code
186187            } 
@@ -210,3 +211,61 @@ pub fn generate_column_impls(
210211
211212    ( impls,  struct_attr,  extra_derive_impls) 
212213} 
214+ 
215+ fn  make_zero_or_default_field_expr ( field_ty :  & Type )  -> TokenStream  { 
216+     if  macro_utils:: classify_integer_type ( field_ty) . is_some ( )  { 
217+         quote !  {  0  as  #field_ty } 
218+     }  else  { 
219+         quote !  {  Default :: default ( )  } 
220+     } 
221+ } 
222+ 
223+ fn  gen_field_inits_zero ( fields :  & FieldsNamed )  -> Vec < TokenStream >  { 
224+     fields. named . iter ( ) . map ( |f| { 
225+         let  name = f. ident . as_ref ( ) . expect ( "named field" ) ; 
226+         let  ty   = & f. ty ; 
227+         let  expr = make_zero_or_default_field_expr ( ty) ; 
228+         quote !  {  #name:  #expr,  } 
229+     } ) . collect ( ) 
230+ } 
231+ 
232+ fn  make_next_field_expr ( field_ident :  & syn:: Ident ,  field_ty :  & Type )  -> TokenStream  { 
233+     if  macro_utils:: classify_integer_type ( field_ty) . is_some ( )  { 
234+         quote !  {  self . #field_ident. wrapping_add( 1 )  } 
235+     }  else  { 
236+         quote !  {  self . #field_ident. clone( )  } 
237+     } 
238+ } 
239+ 
240+ fn  gen_next_field_inits ( fields :  & FieldsNamed )  -> Vec < TokenStream >  { 
241+     fields. named . iter ( ) . map ( |f| { 
242+         let  name = f. ident . as_ref ( ) . expect ( "named field" ) ; 
243+         let  ty   = & f. ty ; 
244+         let  expr = make_next_field_expr ( name,  ty) ; 
245+         quote !  {  #name:  #expr,  } 
246+     } ) . collect ( ) 
247+ } 
248+ 
249+ pub  fn  gen_default_impl ( struct_ident :  & Ident ,  struct_type :  & Type ,  fields :  & FieldsNamed )  -> TokenStream  { 
250+     let  default_inits = gen_field_inits_zero ( fields) ; 
251+ 
252+     quote !  { 
253+         impl  :: core:: default :: Default  for  #struct_type { 
254+             fn  default ( )  -> Self  { 
255+                 #struct_ident {  #( #default_inits) *  } 
256+             } 
257+         } 
258+     } 
259+ } 
260+ 
261+ pub  fn  gen_sampleable_impl ( struct_ident :  & Ident ,  struct_type :  & Type ,  fields :  & FieldsNamed )  -> TokenStream  { 
262+     let  next_inits = gen_next_field_inits ( fields) ; 
263+ 
264+     quote !  { 
265+         impl  Sampleable  for  #struct_type { 
266+             fn  next_value( & self )  -> Self  { 
267+                 #struct_ident {  #( #next_inits) *  } 
268+             } 
269+         } 
270+     } 
271+ } 
0 commit comments