borrowed-thread

Crates.ioborrowed-thread
lib.rsborrowed-thread
version0.1.3
sourcesrc
created_at2018-11-16 17:41:59.24287
updated_at2018-11-16 18:04:37.110917
descriptionthread-safe way to pass borrow to thread::spawn
homepagehttps://github.com/Riey/borrowed-thread
repositoryhttps://github.com/Riey/borrowed-thread
max_upload_size
id97088
size6,762
(Riey)

documentation

README

borrowed-thread

thread-safe way to pass borrow to thread::spawn

NEED nightly rust!

benchmark

test bench::bench_borrowed_thread ... bench: 18,902 ns/iter (+/- 690)

test bench::bench_std_thread ... bench: 18,859 ns/iter (+/- 684)

examples

with &mut

use borrowed_thread;

let mut owned = "ABC".to_owned();

let borrowed_handle = borrowed_thread::spawn(|| {
    owned.push('D');
    0
});

let ret = borrowed_handle.join().expect("join err");

assert_eq!(0, ret);
assert_eq!("ABCD", owned);

with &

use borrowed_thread;

let owned = "ABC".to_owned();

let borrowed_handle = borrowed_thread::spawn(|| {
    assert_eq!("ABC", owned);
    0
});

let ret = borrowed_handle.join().expect("join err");

assert_eq!(0, ret);

panic when drop without join

let mut owned = "ABC".to_owned();

let borrowed_handle = borrowed_thread::spawn(|| {
    owned.push('D');
    0
});

// this will cause panic!
drop(borrowed_handle);

assert_eq!("ABCD", owned);
Commit count: 17

cargo fmt