Crates.io | structx |
lib.rs | structx |
version | 0.1.11 |
source | src |
created_at | 2021-03-16 16:15:14.297161 |
updated_at | 2024-01-15 12:57:15.861942 |
description | Simulating anonymous struct and named arguments in Rust. |
homepage | |
repository | https://github.com/oooutlk/structx |
max_upload_size | |
id | 369739 |
size | 21,712 |
See RFC as Readme-For-Crate for more.
This project provides simulation of anonymous struct and named arguments in
Rust, using proc macros structx!{}
, Structx!{}
, #[named_args]
and
args!{}
.
Add the following in your Cargo.toml file:
[dependencies]
structx = "0.1"
[build-dependencies]
inwelling = "0.5"
[package.metadata.inwelling]
structx = true
Add the following in your build.rs file:
inwelling::to( "structx" );
Add the following in your .rs files:
use structx::*;
If you want to use named arguments, add the following:
use structx::named_args::*;
Anonymous structs are struct
s without the needs of providing struct names.
However, the field names are mandatory. Anonymous structs are of the same type
if and only if they are composed of the same set of field names. The order of
fields are irrelevant.
The notation of an anonymous struct's value is structx!{}
.
let foo = structx!{ i: 3, b: true };
let bar = structx!{ x, y };
The notation of an anonymous struct's type is Structx!{}
.
fn foo( x: i32, y: i32 ) -> Structx!{ x: i32, y: i32 } {
structx!{ x, y: y+1 }
}
Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash.
let a = structx!{ width : 800, height: 600 };
let b = structx!{ height: 600, width : 800 };
let c = structx!{ width : 1024, height: 768 };
assert_eq!( a, b );
assert_ne!( a, c );
At definition site, add attributes #[named_args]
to functions.
#[named_args]
fn set_size( width: u32, height: u32 ) { todo!() }
At call site, wrap arguments with args!{}
.
set_size( args!{ width: 1024, height: 768 });
Under Apache License 2.0 or MIT License, at your will.