macro_rules! structs_from_decl {
    ($vis:vis struct $s_name:ident; $($tail:tt)*) => { ... };
    ($vis:vis struct $s_name:ident {} $($tail:tt)*) => { ... };
    ($(#[$attr:meta])* $vis:vis struct $name:ident $(<$($generic:tt),*>)? ($from:ident: $from_type:ty) $(where $($id:tt: $limit:tt),*)? {
        $($field_vis:vis $field:ident: $type:ty = $val:expr),* $(,)?
    }
    $($tail:tt)*) => { ... };
    () => { ... };
}
Expand description

Struct::from(): assigning user-defined values to fields directly.

Principles

Text replacement, automatic function generation.

Examples

use aoko::{structs_from_decl, assert_eqs};
use std::fmt::Display;
 
structs_from_decl!(
    #[derive(Debug, Clone)]
    pub struct A<T>(from: Option<T>) where T: Display {
        pub foo: T = from.unwrap(),
    }
    struct B {}
    struct C;
);
 
let a = A::from(Some(233));
 
assert_eqs!(
    233, a.foo;
);