struct_as_array2

Crates.iostruct_as_array2
lib.rsstruct_as_array2
version0.3.1
created_at2025-03-06 15:20:32.175172+00
updated_at2025-03-07 07:28:50.361401+00
descriptionA Rust library that allows you to represent the structure as an array. Ported from https://github.com/Nikitamuraviev10/struct_as_array.git to proc_macro2, syn 2.0 and quote 1.0
homepagehttps://github.com/Affinator/struct_as_array.git
repositoryhttps://github.com/Affinator/struct_as_array.git
max_upload_size
id1581261
size11,076
(Affinator)

documentation

https://docs.rs/struct_as_array2

README

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_array2::*;                                                                     
                                                                                            
#[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_array2::*;                                                                     
                                                                                            
#[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);                                                                      
}                                                                                           
Commit count: 0

cargo fmt