• Stars
    star
    153
  • Rank 242,456 (Top 5 %)
  • Language
    Rust
  • Created over 9 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Python-style function decorators for Rust

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
    }
}

More Repositories

1

rust-gc

Simple tracing (mark and sweep) garbage collector for Rust
Rust
862
star
2

compiletest-rs

An extraction of the compiletest utility from the Rust compiler
Rust
215
star
3

elsa

Append-only collections for Rust where borrows to entries can outlive insertions
Rust
171
star
4

triomphe

Fork of std::sync::Arc with lots of utilities useful for FFI
Rust
154
star
5

absolution

"Freedom from syn": Proc macro tools for operating on token trees
Rust
107
star
6

mitosis

Spawn processes with arbitrary closures in rust
Rust
105
star
7

oreutils

Installer for various Rust reimaginations of coreutils. These are not drop-in replacements.
Rust
93
star
8

array-init

Safe wrapper for initializing arrays
Rust
67
star
9

ChatExchange

A Python API for talking to Stack Exchange chat
Python
65
star
10

namespacing-rfc

RFC for Packages as Optional Namespaces
46
star
11

rust-tenacious

Lint to disallow the moving of marked types in Rust
Rust
34
star
12

pathdiff

Rust
28
star
13

cs733

CS 733 assignments
Go
25
star
14

rust-internals-docs

Deprecated, please use the official docs at https://doc.rust-lang.org/nightly/nightly-rustc
HTML
24
star
15

IIT-Timetable

Timetable generator for IIT Bombay
HTML
14
star
16

humpty_dumpty

Implicit Drop/move protection for Rust (linear types)
Rust
12
star
17

AnnoTabe

Annotations for tabs in Chrome
JavaScript
11
star
18

dash

Distributed Shell: A way to replicate system administration commands across machines using Raft
Go
10
star
19

handlebars-fluent

Rust
10
star
20

webgl-to-webvr

Example of turning a WebGL application into WebVR
JavaScript
8
star
21

Manish-Codes

JavaScript
8
star
22

webgl-to-webxr

Webxr version of https://github.com/Manishearth/webgl-to-webvr
JavaScript
5
star
23

manishearth.github.io

HTML
5
star
24

error-index-localization

Rust error index localization
Rust
4
star
25

trashmap

A HashMap and HashSet that operate directly on hashes instead of keys, avoiding rehashing
Rust
4
star
26

rust-extensible

Extensible enums for Rust
Rust
4
star
27

hashglobe

A stable fork of std::HashMap with fallible APIs. Untested.
Rust
3
star
28

reiterate

Iterator adaptor with caching that allows reiterating over the same iterator through the cache
Rust
3
star
29

sudoku-zkp

JavaScript
2
star
30

media-capture

playing around with gstreamer media capture (temporary repo)
Rust
2
star
31

typing-cheatsheets

Cheatsheets for various keyboards
HTML
2
star
32

HostelNoticeboard

Electronic noticeboard for IIT Bombay hostels. Meant to run on an Raspberry Pi.
PHP
2
star
33

sudoku

sudoku annotation/solving tool
JavaScript
2
star
34

webxr-three

three.js fork for webxr testing stuff.
JavaScript
2
star
35

Kapi

Math notebook for Windows 8 Metro
JavaScript
2
star
36

freestring

CString, but with malloc and free
Rust
2
star
37

MathToTeX

JavaScript
2
star
38

css-properties-list

HTML
2
star
39

testcrate

Rust
1
star
40

gstreamer-presentation

JavaScript
1
star
41

rustconf-2017

Website for RustConf 2017
CSS
1
star
42

ChatExchange-Scripts

Scripts that use ChatExchange
Python
1
star
43

ElectionPortal

The worst PHP code I have ever written. Hey, I was in a hurry.
CSS
1
star
44

stylo-flat

Copy of http://hg.mozilla.org/incubator/stylo with history removed for easy cloning and contribution
1
star
45

servo-gecko-try

Service for forwarding servo PRs to gecko's try infra
Python
1
star
46

iterm-git-add-hunk

lol
Shell
1
star
47

StackExchange-ChatBot

Generic Stack Exchange chatbot, using ChatExchange
Python
1
star
48

Presentations

Slides and presentations
JavaScript
1
star
49

rustfest-slides

Slides for my RustFest 2017 (Kiev) keynote
JavaScript
1
star
50

bell-inequality-paper

Term paper on Bell's inequality
TeX
1
star
51

starcon-presentation

JavaScript
1
star