piet_cosmic_text/
export_work.rs

1// SPDX-License-Identifier: LGPL-3.0-or-later OR MPL-2.0
2// This file is a part of `piet-cosmic-text`.
3//
4// `piet-cosmic-text` is free software: you can redistribute it and/or modify it under the
5// terms of either:
6//
7// * GNU Lesser General Public License as published by the Free Software Foundation, either
8//   version 3 of the License, or (at your option) any later version.
9// * Mozilla Public License as published by the Mozilla Foundation, version 2.
10//
11// `piet-cosmic-text` is distributed in the hope that it will be useful, but WITHOUT ANY
12// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13// PURPOSE. See the GNU Lesser General Public License or the Mozilla Public License for more
14// details.
15//
16// You should have received a copy of the GNU Lesser General Public License and the Mozilla
17// Public License along with `piet-cosmic-text`. If not, see <https://www.gnu.org/licenses/>.
18
19//! A trait for exporting work to other threads.
20
21/// Trait for exporting work to another thread.
22pub trait ExportWork {
23    /// Run this closure on another thread.
24    fn run(self, f: impl FnOnce() + Send + 'static);
25}
26
27/// Run work on the current thread.
28#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
29pub struct CurrentThread;
30
31impl ExportWork for CurrentThread {
32    fn run(self, f: impl FnOnce() + Send + 'static) {
33        f()
34    }
35}
36
37/// Run work on the `rayon` thread pool.
38#[cfg(feature = "rayon")]
39#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
40pub struct Rayon;
41
42#[cfg(feature = "rayon")]
43impl ExportWork for Rayon {
44    fn run(self, f: impl FnOnce() + Send + 'static) {
45        rayon_core::spawn(f)
46    }
47}