1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#![feature(unsafe_destructor)]
#![cfg_attr(not(test), no_std)]

//! Thread-local and thread-safe shared slice types, like `&[T]` but
//! without lifetimes.
//!
//! This library depends only on `alloc` and `core`, so can be used in
//! environments without `std`.
//!
//! # Examples
//!
//! Alice has a long list of numbers which she needs to sum up before
//! she's allowed to enter Wonderland. She's impatient to get there
//! and she has a computer with many cores so she wants to use all of
//! them.
//!
//! Using a `ArcSlice`, she can manually divide up the numbers into
//! chunks and distribute them across some threads.
//!
//! ```rust
//! # #![allow(unstable)]
//! use shared_slice::arc::ArcSlice;
//! use std::{cmp, rand, sync};
//!
//! // Alice's numbers (the Mad Hatter doesn't care which numbers,
//! // just that they've been summed up).
//! let numbers = (0..10_000)
//!     .map(|_| rand::random::<u64>() % 100)
//!     .collect::<Vec<_>>();
//!
//!
//! const NTHREADS: usize = 10;
//!
//! let numbers = ArcSlice::new(numbers.into_boxed_slice());
//!
//! // number of elements per thread (rounded up)
//! let per_thread = (numbers.len() + NTHREADS - 1) / NTHREADS;
//!
//! let mut futures = (0..NTHREADS).map(|i| {
//!     // compute the bounds
//!     let lo = i * per_thread;
//!     let hi = cmp::min(numbers.len(), lo + per_thread);
//!
//!     // extract the subsection of the vector that we care about,
//!     // note that the `clone` (which just increases the reference
//!     // counts) is necessary because `ArcSlice::slice` consumes
//!     // the receiver (in order to minimise unnecessary reference
//!     // count modifications).
//!     let my_numbers: ArcSlice<_> = numbers.clone().slice(lo, hi);
//!
//!     // do this part of the sum:
//!     sync::Future::spawn(move || {
//!         my_numbers.iter().fold(0, |a, &b| a + b)
//!     })
//! }).collect::<Vec<sync::Future<u64>>>();
//!
//! // sum up the results from each subsum.
//! let sum = futures.iter_mut().fold(0, |a, b| a + b.get());
//!
//! println!("the sum is {}", sum);
//! ```
//!
//! (NB. `ArcSlice` may become unnecessary for situations like this if
//! [`Send` stops implying
//! `'static`](https://github.com/rust-lang/rfcs/pull/458), since it
//! is likely that one will be able to use conventional borrowed
//! `&[T]` slices directly.)

#![feature(core, hash, alloc)]

extern crate alloc;
extern crate core;

pub mod rc;
pub mod arc;