GithubHelp home page GithubHelp logo

chayimfriedman2 / makeit Goto Github PK

View Code? Open in Web Editor NEW

This project forked from estebank/makeit

0.0 0.0 0.0 14 KB

Zero-overhead type-safe builder pattern `derive` macro for your Rust structs

Rust 100.00%

makeit's Introduction

Compile-time checked Builder pattern derive macro with zero-memory overhead

This is very much a work-in-progress. PRs welcome to bring this to production quality welcome.

The Builder Pattern is a design pattern to allow the construction of complex types one field at a time by calling methods on a builder type. This crate provides a derive macro that allows you to annotate any struct to create a type-level state machine that requires all mandatory fields to be set once and only at compile-time (otherwise you won't be able to call .build() on it).

use makeit::{Buildable, Builder};

#[derive(Builder)]
struct Foo<'a, T: std::fmt::Debug> {
    bar: &'a T,
    #[default(42)]
    baz: i32,
}

// This is the expected use
let x = Foo::builder().set_bar(&()).set_baz(42).build();
// The following builds because of the `default` annotation on `baz`
let x = Foo::builder().set_bar(&()).build();
// The following won't build because `bar` hasn't been set
let x = Foo::builder().set_baz(0).build();

You can look at the examples directory for a showcase of the available features.

The created Builder type has zero-memory overhead because it uses a MaybeUninit backed field of the type to be built.

Error messages

One of the main downsides of the typestate pattern are inscrutable error messages. However, the since this crate generates type-state parameters with readable names, rustc can produce readable error messages: image

makeit's People

Contributors

chayimfriedman2 avatar estebank avatar rcoh avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.