#array #struct #iterator #derive #proc-macro

macro no-std struct_as_array

A Rust library that allows you to represent the structure as an array

3 unstable releases

0.2.0 Nov 13, 2023
0.1.1 Nov 13, 2023
0.1.0 Nov 13, 2023

#306 in No standard library

41 downloads per month

MIT license

10KB
62 lines

A Rust library that allows you to represent the structure as an array.
Library works only with named structs whose fields have the same type.

Examples

Basic usage:

use struct_as_array::*;                                                                     
                                                                                            
#[derive(AsArray)]                                                                          
struct TestStruct {                                                                         
    t1: i32,                                                                                
    t2: i32,                                                                                
    t3: i32,                                                                                
}                                                                                           
                                                                                            
let t = TestStruct {                                                                        
    t1: 0,                                                                                  
    t2: 1,                                                                                  
    t3: 2,                                                                                  
};                                                                                          
                                                                                            
// Represent as array of reference
assert_eq!(t.as_array(), [&0, &1, &2]);

// Convert struct to array
assert_eq!(t.to_array(), [0, 1, 2]);

Using as an iterator:

use struct_as_array::*;                                                                     
                                                                                            
#[derive(AsArray)]                                                                          
struct TestStruct {                                                                         
    t1: i32,                                                                                
    t2: i32,                                                                                
    t3: i32,                                                                                
}                                                                                           
                                                                                            
let t = TestStruct {                                                                        
    t1: 0,                                                                                  
    t2: 1,                                                                                  
    t3: 2,                                                                                  
};                                                                                          
                                                                                            
for i in t.as_array() {                                                                     
    println!("{}", i);                                                                      
}                                                                                           

Dependencies

~1.5MB
~40K SLoC