quick-impl
quick-impl is a Rust procedural macro that generates usual methods and trait implementations for enums and structs, reducing boilerplate and improving ergonomics.
Quick start
use quick_impl;
let circle = Circle;
assert!;
let rect = from;
assert_eq!;
let square = Square;
assert_eq!;
More examples can be found in the examples folder.
Features
Enum variant methods
| Attribute | Description |
|---|---|
as_ref |
Returns a reference to the associated data if the enum is the specified variant. |
as_ref_mut |
Returns a mutable reference to the associated data if the enum is the specified variant. |
from |
Creates the specified variant from the provided data. |
inspect |
Calls a closure with a reference to the associated data if the enum is the specified variant, and returns self. |
into |
Extracts the associated data if the enum is the specified variant, returning an Option. |
is |
Returns true if the enum is the specified variant. |
is_and |
Returns true if the enum is the specified variant and the associated data satisfies a predicate. |
set |
Sets self to the specified variant with the given data, returning the previous value. |
try_into |
Extracts the associated data if the enum is the specified variant, returning a Result. |
Enum variant traits
| Attribute | Description |
|---|---|
Default |
Implements Default, constructing the specified variant. |
From |
Implements From to construct the specified variant. |
TryFrom |
Implements TryFrom to extract the associated data. |
TryInto |
Implements TryInto to extract the associated data. |
Struct field methods
| Attribute | Description |
|---|---|
get |
Returns a reference to the field. |
get_clone |
Returns a clone of the field. |
get_mut |
Returns a mutable reference to the field. |
into |
Consumes self and returns the field. |
from |
Creates an instance from the field, setting the remaining fields to their default values. |
replace |
Replaces the field with the given value, returning the previous value. |
set |
Sets the field and returns &mut self for chaining. |
take |
Takes the field, replacing it with its default value. |
with |
Returns self with the field set to the given value. |
Struct field traits
| Attribute | Description |
|---|---|
AsRef |
Implements AsRef to return a reference to the field. |
AsMut |
Implements AsMut to return a mutable reference to the field. |
Borrow |
Implements Borrow for the field type. |
BorrowMut |
Implements BorrowMut for the field type. |
Deref |
Implements Deref with the field as the target. |
DerefMut |
Implements DerefMut with the field as the target. |
From |
Implements From to create the struct from the field, defaulting the remaining fields. |
Into |
Implements Into to convert the struct into the field value. |
Struct global methods
| Attribute | Description |
|---|---|
new |
Constructs a new instance from the given field values. |
from_tuple |
Constructs a new instance from a tuple of field values. |
into_parts |
Decomposes the instance into a tuple of its field values. |
Struct global traits
| Attribute | Description |
|---|---|
From |
Implements From to create the struct from a tuple of its field values. |
Into |
Implements Into to convert the struct into a tuple of its field values. |
Reducing duplication with quick_impl_all
quick_impl_all applies the specified attributes to all variants (for enums) or all fields (for structs). You can combine it with per-variant/per-field quick_impl attributes:
use quick_impl_all;
Configuration
Method configuration
name— Override the generated method name. Use{}as a placeholder for the variant/field name.
doc— Override the generated documentation string.
Trait configuration
doc— Override the generated documentation for the trait method.
Comparison with derive_more
This crate is not intended to replace derive_more. While derive_more focuses on deriving standard traits, quick-impl focuses on generating common methods like is_*, as_*, and set_*. Trait implementations are included where they complement the method generation, but matching the breadth of derive_more is a non-goal.
Installation
Add quick-impl to your Cargo.toml:
[]
= "0.2"
Or run:
cargo add quick-impl
License
Licensed under either of Apache License, Version 2.0 or MIT License, at your option.