https://github.com/alorel/fancy_constructor-rs
Derive a highly configurable constructor for your struct
https://github.com/alorel/fancy_constructor-rs
Last synced: 8 days ago
JSON representation
Derive a highly configurable constructor for your struct
- Host: GitHub
- URL: https://github.com/alorel/fancy_constructor-rs
- Owner: Alorel
- License: apache-2.0
- Created: 2023-09-04T23:00:56.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-10-28T12:52:41.000Z (6 months ago)
- Last Synced: 2024-11-01T23:42:28.252Z (6 months ago)
- Language: Rust
- Size: 33.2 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
Derive a highly configurable constructor for your struct
[](https://github.com/Alorel/fancy_constructor-rs/actions/workflows/test.yml?query=branch%3Amaster)
[](https://crates.io/crates/fancy_constructor)
[](https://libraries.io/cargo/fancy_constructor)
[](https://coveralls.io/github/Alorel/fancy_constructor-rs)# Examples
Basic
```rust
use fancy_constructor::new;
#[derive(new, PartialEq, Eq, Debug)]
struct MyStruct {
foo: String,
bar: u8,
}let a = MyStruct::new("#[derive(new)]".into(), 55);
let b = MyStruct { foo: "#[derive(new)]".into(), bar: 55 };
assert_eq!(a, b);
```Outputs:
```rust
impl MyStruct {
pub fn new(foo: String, bar: u8) -> Self {
Self { foo, bar }
}
}
````Options showcase
```rust
#[derive(new, PartialEq, Eq, Debug)]
#[new(vis(pub(crate)), name(construct), comment("Foo"), bounds(T: Clone))]
struct MyStruct {
#[new(into)]
a: T,#[new(val("Bar".into()))]
b: String,#[new(clone)]
c: Arc,#[new(default)]
d: Vec,
}let we = Arc::new(Whatever::default());
let a = MyStruct::::construct("A", &we);
let b = MyStruct {a: "A".into(), b: "Bar".into(), c: we, d: vec![]};
assert_eq!(a, b);
```Outputs:
```rust
impl MyStruct {
/// Foo
pub(crate) fn construct(a: impl Into, c: &Arc) -> Self where T: Clone {
Self {
a: a.into(),
b: "Bar".into(),
c: c.clone(),
d: Default::default(),
}
}
}
````Private const fn
```rust
#[derive(new, PartialEq, Eq, Debug)]
#[new(const_fn, vis())]
struct Foo(u8);const FOO: Foo = Foo::new(128);
assert_eq!(FOO, Foo(128));
```Outputs:
```rust
impl Foo {
const fn new(f1: u8) -> Self {
Self(f1)
}
}
````Computed values
```rust
#[derive(new)]
struct Foo {
is_bar: bool,
#[new(val(if is_bar { 100 } else { 5 }))]
barness_level: u8,
}assert_eq!(Foo::new(true).barness_level, 100);
assert_eq!(Foo::new(false).barness_level, 5);
```Custom constructor args
```rust
#[derive(new)]
#[new(args(input_string: &str))]
struct Foo {
#[new(val(input_string.to_lowercase()))]
pub lowercase: String,#[new(val(input_string.to_uppercase()))]
pub uppercase: String,
}let foo = Foo::new("Foo");
assert_eq!(foo.lowercase.as_str(), "foo");
assert_eq!(foo.uppercase.as_str(), "FOO");
```Renaming constructor args
```rust
#[derive(new)]
struct MyNewtype(#[new(name(my_value))] u8);
```Outputs:
```rust
impl MyNewtype {
pub fn new(my_value: u8) -> Self {
Self(my_value)
}
}
````Enums
```rust
#[derive(new, Eq, PartialEq, Debug)]
enum MyEnum {
#[new]
Foo { #[new(into)] bar: u8 },
Qux,
}assert_eq!(MyEnum::new(5), MyEnum::Foo { bar: 5 });
```Outputs:
```rust
impl MyEnum {
pub fn new(bar: Into) -> Self {
Self::Foo { bar: bar.into() }
}
}
````Invalid inputs
```rust
#[derive(fancy_constructor::new)]
enum Foo {
Bar, // no variants marked with `#[new]`
}
``````rust
#[derive(fancy_constructor::new)]
enum Foo {
#[new] Bar, // multiple variants marked with `#[new]`
#[new] Qux,
}
``````rust
#[derive(fancy_constructor::new)]
union Foo { // Unions not supported
bar: u8,
qux: u8,
}
```