quick-impl

Crates.ioquick-impl
lib.rsquick-impl
version
sourcesrc
created_at2024-06-23 08:48:34.291982
updated_at2024-12-11 13:28:56.178088
descriptionA Rust procedural macro auto-generating common methods on enums and structs.
homepagehttps://github.com/makcandrov/quick-impl
repositoryhttps://github.com/makcandrov/quick-impl
max_upload_size
id1281053
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`
size0
(makcandrov)

documentation

https://docs.rs/quick-impl

README

quick-impl

github crates.io docs.rs

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.

Features

Enums methods

  • 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.
  • into - Converts the enum into the associated data of the variant, returning an Option.
  • is - Checks if the enum variant matches a specified variant.
  • 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.

Enums traits

  • 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.

Structures methods

  • 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.
  • set - A setter for the field.
  • take - Returns the field and replaces it with its default value.
  • with - Returns the struct with the field modified.

Structures traits

  • AsRef - Implements the AsRef trait on the struct.
  • AsMut - Implements the AsMut trait on the struct.
  • Borrow - Implements the Borrow trait on the struct.
  • BorrowMut - Implements the BorrowMut trait on the struct.
  • Deref - Implements the Deref trait on the struct.
  • DerefMut - Implements the DerefMut trait on the struct.
  • Into - Implements the Into trait on the struct.
  • From - Implements the From trait on the struct.

Usage

Add quick-impl to your Cargo.toml:

[dependencies]
quick-impl = "0.1"

In your Rust code:

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),
    // ... add attributes to other variants as needed
}

fn main() {
    let instance = YourEnum::Variant1;

    // Use generated methods on enum instances
    assert!(instance.is_variant1());

    let variant2_instance = YourEnum::from(42);
    assert_eq!(*variant2_instance.as_variant2().unwrap(), 42);
}

More examples can be found in the examples.

Commit count: 44

cargo fmt