#closure #traits #clone #eq #unboxed

nightly no-std closures

Abstraction for seperating code and state in closures

3 releases

Uses old Rust 2015

0.1.2 Jul 17, 2017
0.1.1 Jul 16, 2017
0.1.0 Jul 16, 2017

#6 in #eq

MIT/Apache

14KB
277 lines

Closures

This crate provides an abstraction for seperating code and state. Among other things it allows for recursive closures and for trait implementations like PartialEq and Clone.

See the examples directory for some usage examples.

A nightly version of Rust is required because this crate relies on unboxed closures.

Documentation


lib.rs:

extern crate closures;

use std::thread;
use std::sync::mpsc;
use closures::Closure;

struct State {
    id: i32,
    messages: Vec<&'static str>,
    tx: mpsc::Sender<(i32, &'static str)>,
}

fn main() {
    let (tx, rx) = mpsc::channel();
    
    let state = State {
        id: 0,
        messages: vec!["hello", "rusty", "world"],
        tx: tx.clone(),
    };
    thread::spawn(Closure::new(state, thread));
    
    let state = State {
        id: 1,
        messages: vec!["veni", "vidi", "vici"],
        tx: tx,
    };
    thread::spawn(Closure::new(state, thread));
    
    for (id, msg) in rx {
        println!("Thread {} sent: {}", id, msg);
    }
}

fn thread(this: &State) {
    for msg in &this.messages {
        this.tx.send((this.id, msg)).unwrap();
    }
}

No runtime deps

kY