rayon/lib.rs
1//! Rayon is a data-parallelism library that makes it easy to convert sequential
2//! computations into parallel.
3//!
4//! It is lightweight and convenient for introducing parallelism into existing
5//! code. It guarantees data-race free executions and takes advantage of
6//! parallelism when sensible, based on work-load at runtime.
7//!
8//! # How to use Rayon
9//!
10//! There are two ways to use Rayon:
11//!
12//! - **High-level parallel constructs** are the simplest way to use Rayon and also
13//! typically the most efficient.
14//! - [Parallel iterators] make it easy to convert a sequential iterator to
15//! execute in parallel.
16//! - The [`ParallelIterator`] trait defines general methods for all parallel iterators.
17//! - The [`IndexedParallelIterator`] trait adds methods for iterators that support random
18//! access.
19//! - The [`par_sort`] method sorts `&mut [T]` slices (or vectors) in parallel.
20//! - [`par_extend`] can be used to efficiently grow collections with items produced
21//! by a parallel iterator.
22//! - **Custom tasks** let you divide your work into parallel tasks yourself.
23//! - [`join`] is used to subdivide a task into two pieces.
24//! - [`scope`] creates a scope within which you can create any number of parallel tasks.
25//! - [`ThreadPoolBuilder`] can be used to create your own thread pools or customize
26//! the global one.
27//!
28//! [Parallel iterators]: iter
29//! [`par_sort`]: slice::ParallelSliceMut::par_sort
30//! [`par_extend`]: iter::ParallelExtend::par_extend
31//! [`ParallelIterator`]: iter::ParallelIterator
32//! [`IndexedParallelIterator`]: iter::IndexedParallelIterator
33//!
34//! # Basic usage and the Rayon prelude
35//!
36//! First, you will need to add `rayon` to your `Cargo.toml`.
37//!
38//! Next, to use parallel iterators or the other high-level methods,
39//! you need to import several traits. Those traits are bundled into
40//! the module [`rayon::prelude`]. It is recommended that you import
41//! all of these traits at once by adding `use rayon::prelude::*` at
42//! the top of each module that uses Rayon methods.
43//!
44//! These traits give you access to the `par_iter` method which provides
45//! parallel implementations of many iterative functions such as [`map`],
46//! [`for_each`], [`filter`], [`fold`], and [more].
47//!
48//! [`rayon::prelude`]: prelude
49//! [`map`]: iter::ParallelIterator::map
50//! [`for_each`]: iter::ParallelIterator::for_each
51//! [`filter`]: iter::ParallelIterator::filter
52//! [`fold`]: iter::ParallelIterator::fold
53//! [more]: iter::ParallelIterator#provided-methods
54//!
55//! # Crate Layout
56//!
57//! Rayon extends many of the types found in the standard library with
58//! parallel iterator implementations. The modules in the `rayon`
59//! crate mirror [`std`] itself: so, e.g., the `option` module in
60//! Rayon contains parallel iterators for the `Option` type, which is
61//! found in [the `option` module of `std`]. Similarly, the
62//! `collections` module in Rayon offers parallel iterator types for
63//! [the `collections` from `std`]. You will rarely need to access
64//! these submodules unless you need to name iterator types
65//! explicitly.
66//!
67//! [the `option` module of `std`]: std::option
68//! [the `collections` from `std`]: std::collections
69//!
70//! # Targets without threading
71//!
72//! Rayon has limited support for targets without `std` threading implementations.
73//! See the [`rayon_core`] documentation for more information about its global fallback.
74//!
75//! # Other questions?
76//!
77//! See [the Rayon FAQ][faq].
78//!
79//! [faq]: https://github.com/rayon-rs/rayon/blob/main/FAQ.md
80
81#![deny(missing_debug_implementations)]
82#![deny(missing_docs)]
83#![deny(unreachable_pub)]
84#![warn(rust_2018_idioms)]
85
86#[macro_use]
87mod delegate;
88
89#[macro_use]
90mod private;
91
92mod split_producer;
93
94pub mod array;
95pub mod collections;
96pub mod iter;
97pub mod option;
98pub mod prelude;
99pub mod range;
100pub mod range_inclusive;
101pub mod result;
102pub mod slice;
103pub mod str;
104pub mod string;
105pub mod vec;
106
107mod math;
108mod par_either;
109
110mod compile_fail;
111
112pub use rayon_core::FnContext;
113pub use rayon_core::ThreadBuilder;
114pub use rayon_core::ThreadPool;
115pub use rayon_core::ThreadPoolBuildError;
116pub use rayon_core::ThreadPoolBuilder;
117pub use rayon_core::{broadcast, spawn_broadcast, BroadcastContext};
118pub use rayon_core::{current_num_threads, current_thread_index, max_num_threads};
119pub use rayon_core::{in_place_scope, scope, Scope};
120pub use rayon_core::{in_place_scope_fifo, scope_fifo, ScopeFifo};
121pub use rayon_core::{join, join_context};
122pub use rayon_core::{spawn, spawn_fifo};
123pub use rayon_core::{yield_local, yield_now, Yield};
124
125/// We need to transmit raw pointers across threads. It is possible to do this
126/// without any unsafe code by converting pointers to usize or to AtomicPtr<T>
127/// then back to a raw pointer for use. We prefer this approach because code
128/// that uses this type is more explicit.
129///
130/// Unsafe code is still required to dereference the pointer, so this type is
131/// not unsound on its own, although it does partly lift the unconditional
132/// !Send and !Sync on raw pointers. As always, dereference with care.
133struct SendPtr<T>(*mut T);
134
135// SAFETY: !Send for raw pointers is not for safety, just as a lint
136unsafe impl<T: Send> Send for SendPtr<T> {}
137
138// SAFETY: !Sync for raw pointers is not for safety, just as a lint
139unsafe impl<T: Send> Sync for SendPtr<T> {}
140
141impl<T> SendPtr<T> {
142 // Helper to avoid disjoint captures of `send_ptr.0`
143 fn get(self) -> *mut T {
144 self.0
145 }
146}
147
148// Implement Clone without the T: Clone bound from the derive
149impl<T> Clone for SendPtr<T> {
150 fn clone(&self) -> Self {
151 *self
152 }
153}
154
155// Implement Copy without the T: Copy bound from the derive
156impl<T> Copy for SendPtr<T> {}