moretypes

Crates.iomoretypes
lib.rsmoretypes
version0.1.3
sourcesrc
created_at2022-02-27 18:38:52.56181
updated_at2022-03-08 20:02:48.850156
descriptionNamed tuples, records, and more!
homepagehttps://sr.ht/~hutzdog/moretypes-rs
repositoryhttps://git.sr.ht/~hutzdog/moretypes-rs
max_upload_size
id540462
size31,979
Danielle (enderger)

documentation

README

MoreTypes

More types for your types!

Rationale

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

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

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());
}
Commit count: 0

cargo fmt