Crates.io | quick-impl |
lib.rs | quick-impl |
version | |
source | src |
created_at | 2024-06-23 08:48:34.291982+00 |
updated_at | 2025-04-07 10:40:06.242423+00 |
description | A Rust procedural macro auto-generating common methods on enums and structs. |
homepage | https://github.com/makcandrov/quick-impl |
repository | https://github.com/makcandrov/quick-impl |
max_upload_size | |
id | 1281053 |
Cargo.toml error: | TOML parse error at line 17, column 1 | 17 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
quick-impl
is a Rust procedural macro that simplifies working with enums and structs by generating common methods and traits for each variant or field. This helps reduce boilerplate code and enhances the ergonomics of using enums and structs in your Rust projects.
use quick_impl::QuickImpl;
#[derive(QuickImpl)]
enum YourEnum {
#[quick_impl(pub const is)]
Variant1,
#[quick_impl(pub as_ref, pub(crate) as_ref_mut, impl From)]
Variant2(i32),
}
fn main() {
let instance1 = YourEnum::Variant1;
assert!(instance1.is_variant1());
let instance2 = YourEnum::from(42);
assert_eq!(*instance2.as_variant2().unwrap(), 42);
}
More examples can be found in the examples folder.
as_ref
- Returns an immutable reference to the associated data of the enum variant.as_ref_mut
- Returns a mutable reference to the associated data of the enum variant.from
- Creates an instance of the enum variant from the associated data.inspect
- Calls a function with a reference to the associated data if the instance is of the enum variant.into
- Converts the enum into the associated data of the variant, returning an Option
.is
- Returns true
if the enum matches the specified variant.is_and
- Returns true
if the enum matches the specified variant and the associated data matches a predicate.set
- Replaces the current instance with a new instance of the specified variant.try_into
- Converts the enum into the associated data of the variant, returning a Result
.Default
- Implements the Default
trait on the enum.From
- Implements the From
trait on the enum.TryInto
- Implements the TryInto
trait on the enum.TryFrom
- Implements the TryFrom
trait on the associated data.get
- A getter for the field. Returns a reference to the field.get_clone
- A getter for the field. Returns a clone of the field.get_mut
- A mutable getter for a field.into
- Converts the struct into the field.from
- Creates an instance from the field. Sets the other fields to their default value.replace
- A setter for the field, returning the previous value.set
- A setter for the field, returning a mutable reference to the instance.take
- Returns the field and replaces it with its default value.with
- Returns the struct with the field modified.AsRef
- Implements the AsRef
trait for the struct.AsMut
- Implements the AsMut
trait for the struct.Borrow
- Implements the Borrow
trait for the struct.BorrowMut
- Implements the BorrowMut
trait for the struct.Deref
- Implements the Deref
trait for the struct.DerefMut
- Implements the DerefMut
trait for the struct.From
- Implements the From
trait for the struct, allowing it to be created from the field value while setting the other fields to their default values.Into
- Implements the Into
trait for the struct, converting the structure instance into the field value.new
- Constructs a new instance from the given field values.from_tuple
- Constructs a new instance from a tuple of the given field values.into_parts
- Destructures the instance into its fields values.From
- Implements the From
trait for the struct, allowing it to be created from a tuple of its field values.Into
- Implements the Into
trait for the struct, converting the structure instance into a tuple of its field values.name
- Sets the name of the generated method. If not set, a default name is used.#[derive(quick_impl::QuickImpl)]
struct Foo {
#[quick_impl(pub get_clone = { name = "get_{}_unchecked"})]
bar: usize,
#[quick_impl(pub get_clone = "get_{}_unchecked")] // Shorter version
baz: usize,
}
let instance = Foo { bar: 1, baz: 2 };
assert_eq!(instance.get_bar_unchecked(), 1);
assert_eq!(instance.get_baz_unchecked(), 2);
doc
- Sets the documentation for the generated method. If not set, a default documentation is generated.#[derive(quick_impl::QuickImpl)]
#[quick_impl(pub const new = { doc = "Generates an awesome instance of [`Foo`]." })]
struct Foo {
bar: usize,
baz: usize,
}
doc
- Sets the documentation of the generated trait method. If not set, a default documentation is generated.#[derive(quick_impl::QuickImpl)]
enum Foo {
#[quick_impl(impl TryFrom = { doc = "Attempts to extract the associated data from a [`Foo::Bar`] variant." })]
Bar(usize),
Baz(isize),
}
Add quick-impl
to your Cargo.toml
:
[dependencies]
quick-impl = "0.1"
Or run the following command:
cargo add quick-impl