-
Notifications
You must be signed in to change notification settings - Fork 131
/
rayoff.rs
77 lines (61 loc) · 1.66 KB
/
rayoff.rs
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
76
77
use once_cell::unsync::OnceCell;
use std::slice::ChunksMut;
pub(crate) struct ThreadLocal<T>(OnceCell<T>);
impl<T> ThreadLocal<T> {
#[inline(always)]
pub fn new() -> Self {
Self(OnceCell::new())
}
#[inline(always)]
pub fn get_or(&self, f: impl FnOnce() -> T) -> &T {
self.0.get_or_init(f)
}
#[inline(always)]
pub fn get_or_try<E>(&self, f: impl FnOnce() -> Result<T, E>) -> Result<&T, E> {
self.0.get_or_try_init(f)
}
}
impl<T> IntoIterator for ThreadLocal<T> {
type IntoIter = std::option::IntoIter<T>;
type Item = T;
#[inline(always)]
fn into_iter(mut self) -> Self::IntoIter {
self.0.take().into_iter()
}
}
pub(crate) trait FakeRayonIter: Sized {
fn par_bridge(self) -> Self;
}
impl<T> FakeRayonIter for T where Self: Sized {
fn par_bridge(self) -> Self { self }
}
pub(crate) trait FakeRayonIntoIter<T> {
fn par_chunks_mut(&mut self, chunk_size: usize) -> ChunksMut<T>;
}
impl<'a, T> FakeRayonIntoIter<T> for &'a mut [T] {
#[inline(always)]
fn par_chunks_mut(&mut self, chunk_size: usize) -> ChunksMut<T> {
self.chunks_mut(chunk_size)
}
}
impl<'a, T> FakeRayonIntoIter<T> for Box<[T]> {
#[inline(always)]
fn par_chunks_mut(&mut self, chunk_size: usize) -> ChunksMut<T> {
self.chunks_mut(chunk_size)
}
}
pub(crate) struct SpawnMock;
impl SpawnMock {
#[inline(always)]
pub fn spawn<F, R>(&self, f: F) -> R where F: FnOnce(SpawnMock) -> R {
f(SpawnMock)
}
}
#[inline(always)]
pub(crate) fn scope<F, R>(f: F) -> R where F: FnOnce(SpawnMock) -> R {
f(SpawnMock)
}
#[inline(always)]
pub(crate) fn num_cpus() -> usize {
1
}