Rust Delegate

screenshot of Rust Delegate

Rust method delegation with less boilerplate

Overview

This crate is designed to remove boilerplate for structs that delegate some of their methods to one or more of their fields. It provides the delegate! macro which allows for method calls to be delegated to selected expressions, usually inner fields. This crate is useful for reducing code repetition and making code more concise.

Features

  • Delegate to a method with a different name: Allows for delegation of methods with alternative names.
  • Use an arbitrary inner field expression: Allows for delegation to any inner field expression.
  • Delegate to enum variants: Enables delegation to enum variants.
  • Use modifiers that alter the generated method body: Comes with modifiers that can change how the generated method body behaves.
  • Add additional arguments to method: Allows for additional arguments to be included in the delegated method.
  • Call await on async functions: Enables the use of the #[await(true/false)] attribute to specify whether await should be generated after the delegated expression.
  • Delegate to multiple fields: Allows for delegation to multiple fields.
  • Inserts #[inline(always)] automatically: Automatically inserts the #[inline(always)] attribute, unless specified otherwise.

Summary

The Delegate crate provides a convenient way to reduce boilerplate for structs that delegate methods to their fields. It offers features such as delegation to methods with different names, arbitrary field expressions, enum variants, method body modifiers, additional arguments, async function support, and more. This crate is a useful tool for improving code readability and reducing repetition in Rust projects.