Crates.io | impl-opaque |
lib.rs | impl-opaque |
version | 0.5.2 |
source | src |
created_at | 2024-09-26 02:53:26.794685 |
updated_at | 2024-10-18 04:48:54.498734 |
description | Macro for declaring complex struct and initializer |
homepage | |
repository | https://github.com/storycraft/impl-opaque |
max_upload_size | |
id | 1386911 |
size | 3,999 |
impl-opaque
Declare struct fields and initializers in implementation area.
This macro tries to solve separation of field declarations, initializations and usages.
The opaque
attribute generates struct declaration, struct constructor (new
method) by collecting field declarations and initializers inside impl block.
This crate is no_std on runtime and requires alloc to build macro.
#[opaque]
impl Struct {
fn run(&mut self) {
#[field]
let ref mut count: i32 = 0;
*count += 1;
println!("{}", count);
}
}
#[opaque]
impl Struct {
field!(count: i32 = 0);
fn run(&mut self) {
self.count += 1;
println!("{}", self.count);
}
}
#[opaque(pub(self) count: i32)]
impl Struct {
fn run(&mut self) {
self.count += 1;
println!("{}", self.count);
}
}
#[opaque]
impl Iterator for Struct {
type Item = i32;
fn next(&mut self) -> Option<i32> {
#[field]
let ref mut count: i32 = 0;
*count += 1;
Some(*count)
}
}
#[opaque]
impl Struct {
pub fn run(&mut self) {
#[field]
let ref mut running @ true: bool = true else {
return
};
println!("run");
*running = false;
}
}
Attributes below #[opaque]
will be moved to struct declaration.
Attributes below #[field]
and field!()
will be moved to field declaration.
Attribute reference
#[opaque($(as $vis $(const)? ,)? $($($vis)? $ident: $ty),*)]
See examples
for simple example
This crate is licensed under MIT OR Apache-2.0