]>
Commit | Line | Data |
---|---|---|
f20569fa XL |
1 | // Copyright (c) 2017 Xidorn Quan <me@upsuper.org> |
2 | // Copyright (c) 2017 Martijn Rijkeboer <mrr@sru-systems.com> | |
3 | // | |
4 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
5 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
6 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
7 | // option. This file may not be copied, modified, or distributed | |
8 | // except according to those terms. | |
9 | ||
10 | /// The thread mode used to perform the hashing. | |
11 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] | |
12 | pub enum ThreadMode { | |
13 | /// Run in one thread. | |
14 | Sequential, | |
15 | ||
16 | /// Run in the same number of threads as the number of lanes. | |
17 | Parallel, | |
18 | } | |
19 | ||
20 | impl ThreadMode { | |
21 | /// Create a thread mode from the threads count. | |
22 | pub fn from_threads(threads: u32) -> ThreadMode { | |
23 | if threads > 1 { | |
24 | ThreadMode::Parallel | |
25 | } else { | |
26 | ThreadMode::Sequential | |
27 | } | |
28 | } | |
29 | } | |
30 | ||
31 | impl Default for ThreadMode { | |
32 | fn default() -> ThreadMode { | |
33 | ThreadMode::Sequential | |
34 | } | |
35 | } | |
36 | ||
37 | ||
38 | #[cfg(test)] | |
39 | mod tests { | |
40 | ||
41 | use crate::thread_mode::ThreadMode; | |
42 | ||
43 | #[test] | |
44 | fn default_returns_correct_thread_mode() { | |
45 | assert_eq!(ThreadMode::default(), ThreadMode::Sequential); | |
46 | } | |
47 | ||
48 | #[test] | |
49 | fn from_threads_returns_correct_thread_mode() { | |
50 | assert_eq!(ThreadMode::from_threads(0), ThreadMode::Sequential); | |
51 | assert_eq!(ThreadMode::from_threads(1), ThreadMode::Sequential); | |
52 | assert_eq!(ThreadMode::from_threads(2), ThreadMode::Parallel); | |
53 | assert_eq!(ThreadMode::from_threads(10), ThreadMode::Parallel); | |
54 | assert_eq!(ThreadMode::from_threads(100), ThreadMode::Parallel); | |
55 | } | |
56 | } |