Skip to content

Commit

Permalink
update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
valsteen committed Sep 24, 2022
1 parent 0f538e2 commit 8d6da66
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[package]
name = "sort_by_derive"
version = "0.1.9"
version = "0.1.10"
edition = "2021"
license = "Unlicense"
description = "Derive macro SortBy and helper macros EnumAccessor and EnumSequence, deriving traits `Ord`, `PartialOrd`, `Eq`, `PartialEq` and `Hash` for structs and enums that can't automatically derive from those traits."
homepage = "https://github.com/valsteen/sort_by_derive"
documentation = "https://github.com/valsteen/sort_by_derive/#readme"
repository = "https://github.com/valsteen/sort_by_derive"
keywords = ["macro", "derive", "ordering", "sorting"]
categories = ["development-tools"]
keywords = ["macro", "derive", "ordering", "sorting", "enum"]
categories = ["development-tools","rust-patterns","development-tools::procedural-macro-helpers"]
readme = "README.md"

[lib]
Expand Down
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This crate provides 3 derive macros `SortBy`, `EnumAccessor` and `EnumSequence`.

- `SortBy` derives the traits `Ord`, `PartialOrd`, `Eq`, `PartialEq` and `Hash` on structs that can't automatically derive those traits because they contain unorderable fields such as `f32`.
- On enums and structs, `SortBy` can also implement a `Ord` trait that calls arbitrary methods - this is particularly useful in combination with enum variant accessor methods derived by `EnumAccessor` an `EnumSequence`
- `EnumAccessor` derives accessor methods to common fields in variants - so you don't need to write yourself `match` statements to access a field with the same name and type on different variants.
- `EnumAccessor` derives accessor methods to common fields in variants - so you don't need to write yourself `match` statements to access a field with the same name and type on different variants. This feature is similar to [enum_dispatch](https://crates.io/crates/enum_dispatch), but takes a different approach where structs don't need to implement a trait.
- `EnumSequence` provides a `enum_sequence` method where the first variant returns `0`, the second `1`, etc. This is useful is you want to implement a custom sorting, while the order of declaration of variant is still relevant as a secondary ordering criteria.

## Usage
Expand Down Expand Up @@ -65,12 +65,14 @@ impl core::cmp::Ord for Something {

### EnumAccessor

Attributes are declared at top-level.
This derive macro is similar to [enum_dispatch](https://crates.io/crates/enum_dispatch). `enum_dispatch` requires structs to implement a common trait, which can be useful if a common set of functions applies to all variants . `EnumAccessor` takes the opposite approach: common fields and methods are declared at enum level, and you can have variants that don't have a given field or method. This may be more practical if there is a large amount of variants and your only concern is accessing fields, because individual structs just hold data. This is typical for events - they represent a state change and are generally consumed as a whole, individual structs have no code of their own.

After adding `derive(EnumAccessor)` to the enum, fields are declared as `accessor(field: type)` attributes:

```rust
#[derive(EnumAccessor)]
#[accessor(name_of_the_field: type_of_the_field)]
#[accessor2(name_of_other_field: type_of_the_other_field)]
#[accessor(name_of_other_field: type_of_the_other_field)]
enum E {
Variant1(X),
Variant2(Y),
Expand All @@ -88,9 +90,11 @@ fn do_something(some_e: &mut E) {
}
```

Use `Except` or `Only` if not all variants have a given field:

```rust
#[derive(EnumAccessor)]
#[accessor(name: type, except(Variant3,Variant4))]
#[accessor(name: type, Except(Variant3,Variant4))]
enum E {
Variant1(X), // calling `name` on a E::Variant1 returns Some(&X.type)
Variant2(Y), // calling `name` on a E::Variant2 returns Some(&Y.type)
Expand Down Expand Up @@ -203,7 +207,7 @@ impl NoteAccessor for Note {
}
```

#### Example of method call
#### Method accessor

The General form is `#[accessor(method():type)]` :

Expand Down
6 changes: 3 additions & 3 deletions src/enum_variant_accessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,11 +564,11 @@ mod test {
#[test]
fn test_enum() {
let input = syn::parse_quote! {
#[accessor(acc1: usize, only(D,G,H))]
#[accessor(acc1: usize, Only(D,G,H))]
#[accessor(acc2 as get_u8: u8)]
#[accessor(acc3: String, except(D))]
#[accessor(acc3: String, Except(D))]
#[accessor(acc4(): String, (D))]
#[accessor(acc5() as other: String, (D))]
#[accessor(acc5() as other: String, Except(D))]
enum SomeEnum {
A(b),
C(d),
Expand Down

0 comments on commit 8d6da66

Please sign in to comment.