All Projects → chancancode → Rust Delegate

chancancode / Rust Delegate

Licence: other
Rust method delegation with less boilerplate

Programming Languages

rust
11053 projects

Projects that are alternatives of or similar to Rust Delegate

Imgui Rs
Rust bindings for Dear ImGui
Stars: ✭ 1,258 (+610.73%)
Mutual labels:  rust-library
Roperator
Experimental Kubernetes Operator kit written in Rust
Stars: ✭ 146 (-17.51%)
Mutual labels:  rust-library
Pdb
A parser for Microsoft PDB (Program Database) debugging information
Stars: ✭ 156 (-11.86%)
Mutual labels:  rust-library
Grammers
(tele)gramme.rs - use Telegram's API from Rust
Stars: ✭ 109 (-38.42%)
Mutual labels:  rust-library
Kernel Roulette
A kernel module written in Rust
Stars: ✭ 124 (-29.94%)
Mutual labels:  rust-library
Tentacle
A multiplexed p2p network framework that supports custom protocols
Stars: ✭ 150 (-15.25%)
Mutual labels:  rust-library
Bitmatch
A Rust crate that allows you to match, bind, and pack the individual bits of integers.
Stars: ✭ 82 (-53.67%)
Mutual labels:  rust-library
Wither
An ODM for MongoDB built on the official MongoDB Rust driver.
Stars: ✭ 174 (-1.69%)
Mutual labels:  rust-library
Lua Rs
Pure Rust implementation of Lua compiler.
Stars: ✭ 130 (-26.55%)
Mutual labels:  rust-library
Atty
are you or are you not a tty?
Stars: ✭ 153 (-13.56%)
Mutual labels:  rust-library
Gust
A small charting/visualization tool and partial vega implementation for Rust
Stars: ✭ 116 (-34.46%)
Mutual labels:  rust-library
Tera
A template engine for Rust based on Jinja2/Django
Stars: ✭ 1,873 (+958.19%)
Mutual labels:  rust-library
Mtpng
A parallelized PNG encoder in Rust
Stars: ✭ 151 (-14.69%)
Mutual labels:  rust-library
Rustplotlib
A pure Rust visualization library inspired by D3.js
Stars: ✭ 87 (-50.85%)
Mutual labels:  rust-library
Grex
A command-line tool and library for generating regular expressions from user-provided test cases
Stars: ✭ 4,847 (+2638.42%)
Mutual labels:  rust-library
Askama
Type-safe, compiled Jinja-like templates for Rust
Stars: ✭ 1,255 (+609.04%)
Mutual labels:  rust-library
Sm
🚀 SM – a static State Machine library
Stars: ✭ 149 (-15.82%)
Mutual labels:  rust-library
Rust S3
Rust library for interfacing with AWS S3 and other API compatible services
Stars: ✭ 177 (+0%)
Mutual labels:  rust-library
Duckscript
Simple, extendable and embeddable scripting language.
Stars: ✭ 169 (-4.52%)
Mutual labels:  rust-library
Neovim Lib
Rust library for Neovim clients
Stars: ✭ 152 (-14.12%)
Mutual labels:  rust-library

Method delegation with less boilerplate

Build Status Crates.io

This crate removes some boilerplate for structs that simply delegate some of their methods to one or more of their fields.

It gives you the delegate! macro, which delegates method calls to selected expressions (usually inner fields).

Example:

A Stack data structure implemented using an inner Vec via delegation.

use delegate::delegate;

#[derive(Clone, Debug)]
struct Stack<T> {
    inner: Vec<T>,
}
impl<T> Stack<T> {
    pub fn new() -> Self<T> {
        Self { inner: vec![] }
    }

    delegate! {
        to self.inner {
            pub fn is_empty(&self) -> bool;
            pub fn push(&mut self, value: T);
            pub fn pop(&mut self) -> Option<T>;
            pub fn clear(&mut self);

            #[call(len)]
            pub fn size(&self) -> usize;

            #[call(last)]
            pub fn peek(&self) -> Option<&T>;

        }
    }
}

Features:

  • Delegate to a method with a different name
struct Stack { inner: Vec<u32> }
impl Stack {
    delegate! {
        to self.inner {
            #[call(push)]
            pub fn add(&mut self, value: u32);
        }
    }
}
  • Use an arbitrary inner field expression
struct Wrapper { inner: Rc<RefCell<Vec<u32>>> }
impl Wrapper {
    delegate! {
        to self.inner.deref().borrow_mut() {
            pub fn push(&mut self, val: u32);
        }
    }
}
  • Change the return type of the delegated method using a From impl or omit it altogether
struct Inner;
impl Inner {
    pub fn method(&self, num: u32) -> u32 { num }
}
struct Wrapper { inner: Inner }
impl Wrapper {
    delegate! {
        to self.inner {
            // calls method, converts result to u64
            #[into]
            pub fn method(&self, num: u32) -> u64;

            // calls method, returns ()
            #[call(method)]
            pub fn method_noreturn(&self, num: u32);
        }
    }
}
  • Delegate to multiple fields
struct MultiStack {
    left: Vec<u32>,
    right: Vec<u32>,
}
impl MultiStack {
    delegate! {
        to self.left {
            /// Push an item to the top of the left stack
            #[call(push)]
            pub fn push_left(&mut self, value: u32);
        }
        to self.right {
            /// Push an item to the top of the right stack
            #[call(push)]
            pub fn push_right(&mut self, value: u32);
        }
    }
}
  • Delegation of generic methods
  • Inserts #[inline(always)] automatically (unless you specify #[inline] manually on the method)

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Conduct

Please follow the Rust Code of Conduct. For escalation or moderation issues please contact the crate author(s) listed in Cargo.toml.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].