Crates.io | moretypes |
lib.rs | moretypes |
version | 0.1.3 |
source | src |
created_at | 2022-02-27 18:38:52.56181 |
updated_at | 2022-03-08 20:02:48.850156 |
description | Named tuples, records, and more! |
homepage | https://sr.ht/~hutzdog/moretypes-rs |
repository | https://git.sr.ht/~hutzdog/moretypes-rs |
max_upload_size | |
id | 540462 |
size | 31,979 |
This library is a successor to my records
library, designed to make it more modular and comosable. Instead of one record
attribute, moretypes
provides several attributes that can be commposed to give objects certain properties. This is different from traits, as these properties are often implementations of multiple traits, constructors, behavioural changes, etc.
Records in MoreTypes are structs with all fields public (like records::record
, but without most of the methods). They are useful for cases like configuration structs, where placing pub
before every field is annoying boilerplate at best and logically erroneous at worst. Private fields preceeded by underscores remain private.
use moretypes::record;
#[record]
pub struct Config {
option1: String,
option2: u32,
}
pub fn main() {
let cfg = Config {
option1: String::from("Foo"),
option2: 69,
};
println!("option1 = {}, option2 = {}", cfg.option1, cfg.option2);
}
Named tuples are structs with named fields that are able to be used similar to tuple structs. They provide both the ability to convert to/from tuples and a way to construct them like tuples (both of these can be switched off using flags). They are useful as a replacement for tuple structs, where you want named fields and ordering at the same time. The fields are ordered based on where they are declared.
use moretypes::named_tuple;
#[named_tuple]
pub struct Vec3<T> {
x: T,
y: T,
z: T,
}
pub fn main() {
let pos = Vec3::new(1.0, 2.0, 3.0);
println!("{:?}", pos.as_tuple());
}