Crates.io | struct-index |
lib.rs | struct-index |
version | 1.0.0 |
source | src |
created_at | 2024-10-05 07:58:41.532314 |
updated_at | 2024-10-05 07:58:41.532314 |
description | structure implement index trait |
homepage | |
repository | https://github.com/lipogem/struct-index |
max_upload_size | |
id | 1397500 |
size | 16,361 |
structure implement index trait the order in which the structure is defined is the same
Examples
use std::{
any::Any,
ops::{Index, Not},
};
use struct_index::StructIndex;
#[derive(StructIndex)]
struct Student {
name: String,
age: u32,
height: f64,
address: String,
}
fn fmt_str<'a, T>(ival: &'a T) -> String
where
T: Index<usize, Output = dyn Any>,
&'a T: Not<Output = (&'static str, &'static [&'static str])>,
{
let (name, fnames) = !ival;
let mut content = name.to_string() + "::";
for i in 0..fnames.len() {
if let Some(v) = ival[i].downcast_ref::<u32>() {
content += fnames[i];
content += ":";
content += &v.to_string();
} else if let Some(v) = ival[i].downcast_ref::<f64>() {
content += fnames[i];
content += ":";
content += &v.to_string();
} else if let Some(v) = ival[i].downcast_ref::<String>() {
content += fnames[i];
content += ":\"";
content += &v;
content += "\"";
}
if i < fnames.len() - 1 {
content += ",";
}
}
content
}
fn main() {
let student = Student {
name: "Alice".to_string(),
age: 18,
height: 1.72,
address: "New York".to_string(),
};
let content = fmt_str(&student);
assert_eq!(
content,
"Student::name:\"Alice\",age:18,height:1.72,address:\"New York\""
);
}
struct-index is provided under the MIT license. See LICENSE.