Giter Club home page Giter Club logo

rust-adorn's Introduction

rust-adorn

Build Status

Python-style function decorators for Rust

Decorate functions

Example usage:

use adorn::{adorn, make_decorator};

#[adorn(bar)]
fn foo(a: &mut u8, b: &mut u8, (c, _): (u8, u8)) {
    assert!(c == 4);
    *a = c;
    *b = c;
}


fn bar<F>(f: F, a: &mut u8, b: &mut u8, (c, d): (u8, u8)) where F: Fn(&mut u8, &mut u8, (u8, u8)) {
    assert!(c == 0 && d == 0);
    f(a, b, (4, 0));
    *b = 100;
}

fn main() {
    let mut x = 0;
    let mut y = 1;
    foo(&mut x, &mut y, (0, 0));
    assert!(x == 4 && y == 100);
}

In this case, foo will become:

fn foo(a: &mut u8, b: &mut u8, (c, d): (u8, u8)) {
    fn foo_inner(a: &mut u8, b: &mut u8, (c, _): (u8, u8)) {
        assert!(c == 4);
        *a = c;
        *b = c;
    }
    bar(foo_inner, a, b, (c, d))
}

In other words, calling foo() will actually call bar() wrapped around foo().

There is a #[make_decorator] attribute to act as sugar for creating decorators. For example,

#[make_decorator(f)]
fn bar(a: &mut u8, b: &mut u8, (c, d): (u8, u8)) {
    assert!(c == 0 && d == 0);
    f(a, b, (4, 0)); // `f` was declared in the `make_decorator` annotation
    *b = 100;
}

desugars to

fn bar<F>(f: F, a: &mut u8, b: &mut u8, (c, d): (u8, u8)) where F: Fn(&mut u8, &mut u8, (u8, u8)) {
    assert!(c == 0 && d == 0);
    f(a, b, (4, 0));
    *b = 100;
}

Decorate nonstatic methods

Example usage:

use adorn::{adorn_method, make_decorator_method};

pub struct Test {
    a: u8,
    b: u8
}

impl Test {
    #[adorn_method(bar)]
    fn foo(&mut self, a: u8, b: u8) {
        assert!(a == 0 && b == 0);
        self.a = a;
        self.b = b;
    }
    
    fn bar<F>(&mut self, f: F, a: u8, b: u8) where F: Fn(Self, u8, u8) {
        assert!(a == 0 && b == 0);
        f(self, a, b);
        self.b = 100;
    }
}

fn main() {
    let mut t = Test {
        a: 1,
        b: 1,
    };
    t.foo(0, 0);
    assert!(t.a == 0 && t.b == 100);
}

In this case, foo will become:

impl Test {
    fn foo(&mut self, a: u8, b: u8) {
        let foo_inner = |s: &mut Self, a: u8, b: u8| {
            assert!(a == 0 && b == 0);
            s.a = a;
            s.b = b;
        };
        self.bar(foo_inner, a, b, (c, d))
    }
}

Similarly, a #[make_decorator_method] attribute is provided to create decorators. For example,

impl Test {
    #[make_decorator_method(f)]
    fn bar(&mut self, a: u8, b: u8) {
        assert!(a == 0 && b == 0);
        f(self, a, b); // `f` was declared in the `make_decorator_method` annotation
        self.b = 100;
    }
}

desugars to

impl Test{
    fn bar<F>(&mut self, f: F, a: u8, b: u8) where F: Fn(Self, u8, u8) {
        assert!(a == 0 && b == 0);
        f(self, a, b);
        self.b = 100;
    }
}

Decorate static methods

Use #[make_decorator_static] and #[adorn_static] to make a static decorator and then use it to decorate a static method, for example

use adorn::{adorn_method, make_decorator_method};

pub struct Test {
    a: u8,
    b: u8
}

impl Test {
    #[adorn_static(bar)]
    fn foo(a: u8, b: u8) -> Self {
        assert!(a == 0 && b == 0);
        Self {
            a,
            b
        }
    }

    #[make_decorator_static(f)]
    fn bar(a: u8, b: u8) -> Self {
        assert!(a == 0 && b == 0);
        let mut retval = f(a, b);
        retval.b = 100;
        retval
    }
}

fn main() {
    let t = Test::foo(0, 0);
    assert!(t.a == 0 && t.b == 100);
}

The two static methods desugar to

impl Test {
    fn foo(a: u8, b: u8) -> Self {
        let foo_inner = |a: u8, b: u8| -> Self {
            assert!(a == 0 && b == 0);
            Self {
                a,
                b
            }
        };
        Self::bar(foo, a, b)
    }

    fn bar(f: F, a: u8, b: u8) -> Self where F: Fn(u8, u8) -> Self {
        assert!(a == 0 && b == 0);
        let mut retval = f(a, b);
        retval.b = 100;
        retval
    }
}

rust-adorn's People

Contributors

manishearth avatar aidanhs avatar ids1024 avatar donkeyteethux avatar cychen2021 avatar victor-savu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.