4 releases

0.1.3 Mar 8, 2022
0.1.2 Feb 27, 2022
0.1.1 Feb 27, 2022
0.1.0 Feb 27, 2022

#1533 in Data structures

MPL-2.0 license

11KB
157 lines

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());
}

Dependencies

~1.5MB
~33K SLoC