Crates.io | const-builder |
lib.rs | const-builder |
version | 0.1.3 |
created_at | 2025-07-15 08:09:41.929882+00 |
updated_at | 2025-08-19 13:46:17.652459+00 |
description | Derive macro for const-compatible compile-time checked builders. |
homepage | |
repository | https://github.com/DPlayer234/const-builder |
max_upload_size | |
id | 1752784 |
size | 72,733 |
Creates const-compatible builders for your structs:
use const_builder::ConstBuilder;
#[derive(ConstBuilder)]
pub struct Person<'a> {
// fields are required by default
pub name: &'a str,
// optional fields have a default specified
// the value is required even when the type implements `Default`!
#[builder(default = 0)]
pub age: u32,
}
let steve = const {
Person::builder()
.name("steve smith")
// keep the default for age
.build()
};
You can initialize fields in any order, however compile-time checks prevent calling build
before all required field have been set. It is also impossible to set the same field more than once per builder.
By default, the builder type and *::builder
function have the same visibility as the declared struct.
The generated code is supported in #![no_std]
crates.
This crate was inspired by typed-builder and created due to the need to be able to provide a stable way to create non-exhaustive structs across crates in const
code.
const
code.#[builder(default = ...)]
.*::builder
function, builder types, and field setters.MaybeUninit
and unsafe functions, wrapping them in a safe interface.build
and duplicate set fields aren't easy to understand and will mostly refer to the type not having the specified function.#[builder(default = ...)]
accepts any Rust expression, however when a string literal is provided, it is parsed again. Setting defaults for &str
values therefore requires specifying them similar to #[builder(default = r#""default value""#)]
.Default
implementations.Licensed under the MIT license.