aggregate

Crates.ioaggregate
lib.rsaggregate
version0.3.3
sourcesrc
created_at2023-01-24 01:28:02.586231
updated_at2023-02-07 06:30:50.594643
descriptionAggregate attributes of structs for runtime
homepage
repositoryhttps://github.com/voidentente/aggregate
max_upload_size
id766365
size5,432
__________ (voidentente)

documentation

README

Aggregate

Access attributes of structs, enums, unions, and their fields using a simple Derive macro and without allocating.

About

Aggregate utilizes the power of macros, perfect hash functions, and lazy loading for extreme performance.

Amalgamates are singletons representing a struct/enum/union's attribute structure that lie in shared static memory and can be accessed via a type function call.

Aggregate works recursively by calling aggregate() on nested structures without limiting the types that can be used, and without runtime overhead by simply linking to nested Amalgamates by reference.

Attributes are kept intact, which means that on access, they are represented as syn::Attributes. However, parsing all attributes from tokens can be costly and wasteful, which is why they are lazy-loaded and only parsed when accessed.

Features

By default, all features are enabled.

  • derive re-exports aggregate_derive.

  • impl adds default implementations for common types like Option<T>.

  • fmt implements fmt::Debug for Amalgamate.

Usage

aggregate is extremely simple to use.

In order to aggregate attributes of your struct/enum/union, simply derive it:

// The prelude is not required for derive,
// however in order to use `aggregate`,
// the trait must be in scope
use aggregate::prelude::*;

/// This attribute is paired with the type
#[derive(Aggregate)]
struct Config {
  /// This attribute is paired with the field
  switch: bool,
}

Aggregate supports nesting:

/// This attribute is paired with the type.
#[derive(Aggregate)]
struct ConfigFile {
  /// This attribute is paired with the field
  ///
  /// This field has an `inner`, which will 
  /// include the aggregation of `Config`
  ///
  /// In order for `aggregate_derive` to notice 
  /// nested structures, you must mark the field 
  /// with the `#[aggregate]` attribute:
  #[aggregate]
  my_config: Config,
}

The #[aggregate] attribute is not required on enum variants, but is again required inside enum variant structs and tuples:

#[derive(Aggregate)]
enum MyEnum {
  /// Variants are automatically included
  VariantOne {
    /// Fields must be marked
    #[aggregate]
    field_1: Inner,
  },

  /// Unnamed structs like this are also 
  /// supported; `aggregate` simply enumerates 
  /// the fields for representation
  VariantTwo(#[aggregate] Inner),
}
Commit count: 23

cargo fmt