Skip to main content

Crate quick_impl

Crate quick_impl 

Source
Expand description

§quick-impl

Crates.io docs.rs License

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::quick_impl;

#[quick_impl]
enum Shape {
    #[quick_impl(pub const is)]
    Circle(f64),

    #[quick_impl(pub as_ref, as_ref_mut, impl From)]
    Rectangle(f64, f64),

    #[quick_impl(pub as_ref, pub into)]
    Square(f64),
}

let circle = Shape::Circle(5.0);
assert!(circle.is_circle());

let rect = Shape::from((3.0, 4.0));
assert_eq!(rect.as_rectangle().unwrap(), (&3.0, &4.0));

let square = Shape::Square(10.0);
assert_eq!(square.into_square().unwrap(), 10.0);

More examples can be found in the examples folder.

§Features

§Enum variant methods

AttributeDescription
as_refReturns a reference to the associated data if the enum is the specified variant.
as_ref_mutReturns a mutable reference to the associated data if the enum is the specified variant.
fromCreates the specified variant from the provided data.
inspectCalls a closure with a reference to the associated data if the enum is the specified variant, and returns self.
intoExtracts the associated data if the enum is the specified variant, returning an Option.
isReturns true if the enum is the specified variant.
is_andReturns true if the enum is the specified variant and the associated data satisfies a predicate.
setSets self to the specified variant with the given data, returning the previous value.
try_intoExtracts the associated data if the enum is the specified variant, returning a Result.

§Enum variant traits

AttributeDescription
DefaultImplements Default, constructing the specified variant.
FromImplements From to construct the specified variant.
TryFromImplements TryFrom to extract the associated data.
TryIntoImplements TryInto to extract the associated data.

§Struct field methods

AttributeDescription
getReturns a reference to the field.
get_cloneReturns a clone of the field.
get_mutReturns a mutable reference to the field.
intoConsumes self and returns the field.
fromCreates an instance from the field, setting the remaining fields to their default values.
replaceReplaces the field with the given value, returning the previous value.
setSets the field and returns &mut self for chaining.
takeTakes the field, replacing it with its default value.
withReturns self with the field set to the given value.

§Struct field traits

AttributeDescription
AsRefImplements AsRef to return a reference to the field.
AsMutImplements AsMut to return a mutable reference to the field.
BorrowImplements Borrow for the field type.
BorrowMutImplements BorrowMut for the field type.
DerefImplements Deref with the field as the target.
DerefMutImplements DerefMut with the field as the target.
FromImplements From to create the struct from the field, defaulting the remaining fields.
IntoImplements Into to convert the struct into the field value.

§Struct global methods

AttributeDescription
newConstructs a new instance from the given field values.
from_tupleConstructs a new instance from a tuple of field values.
into_partsDecomposes the instance into a tuple of its field values.

§Struct global traits

AttributeDescription
FromImplements From to create the struct from a tuple of its field values.
IntoImplements 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::quick_impl_all;

#[quick_impl_all(pub const is)]
enum MyEnum {
    VariantA,

    #[quick_impl(pub from)]
    VariantB(i32),
}

fn main() {
    let variant = MyEnum::from_variant_b(10);
    assert!(variant.is_variant_b());
    assert!(!variant.is_variant_a());
}

§Configuration

§Method configuration

  • name — Override the generated method name. Use {} as a placeholder for the variant/field name.
#[quick_impl::quick_impl]
struct Foo {
    #[quick_impl(pub get_clone = { name = "get_{}_unchecked" })]
    bar: usize,
    #[quick_impl(pub get_clone = "get_{}_unchecked")] // shorthand
    baz: usize,
}

fn main() {
    let foo = Foo { bar: 1, baz: 2 };
    assert_eq!(foo.get_bar_unchecked(), 1);
    assert_eq!(foo.get_baz_unchecked(), 2);
}
  • doc — Override the generated documentation string.
#[quick_impl::quick_impl(pub const new = { doc = "Generates an awesome instance of [`Foo`]." })]
struct Foo {
    bar: usize,
    baz: usize,
}

§Trait configuration

  • doc — Override the generated documentation for the trait method.
#[quick_impl::quick_impl]
enum Foo {
    #[quick_impl(impl TryFrom = { doc = "Attempts to extract the data from a [`Foo::Bar`] variant." })]
    Bar(usize),
    Baz(isize),
}

§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:

[dependencies]
quick-impl = "0.2"

Or run:

cargo add quick-impl

§License

Licensed under either of Apache License, Version 2.0 or MIT License, at your option.

Attribute Macros§

quick_impl
Quickly generate common methods and trait implementations on enums or structs.
quick_impl_all
Quickly generate common methods and trait implementations on enums or structs on all the variants or fields.