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
use std::num::NonZeroUsize;
use super::{Concurrency, ConcurrencyProvider, ConcurrencyProviderFeedback};
#[derive(Debug, Clone, Copy)]
pub struct FixedConcurrencyProvider(NonZeroUsize);
impl FixedConcurrencyProvider {
#[inline]
pub fn new(concurrency: usize) -> Option<Self> {
NonZeroUsize::new(concurrency).map(Self::new_with_non_zero_concurrency)
}
#[inline]
pub const fn new_with_non_zero_concurrency(concurrency: NonZeroUsize) -> Self {
Self(concurrency)
}
#[inline]
pub const fn fixed_concurrency(&self) -> NonZeroUsize {
self.0
}
}
impl Default for FixedConcurrencyProvider {
#[inline]
fn default() -> Self {
Self::new_with_non_zero_concurrency(default_non_zero_concurrency())
}
}
impl ConcurrencyProvider for FixedConcurrencyProvider {
#[inline]
fn concurrency(&self) -> Concurrency {
self.fixed_concurrency().into()
}
#[inline]
fn feedback(&self, _feedback: ConcurrencyProviderFeedback<'_>) {}
}
impl From<NonZeroUsize> for FixedConcurrencyProvider {
#[inline]
fn from(concurrency: NonZeroUsize) -> Self {
Self(concurrency)
}
}
fn default_non_zero_concurrency() -> NonZeroUsize {
#[allow(unsafe_code)]
unsafe {
NonZeroUsize::new_unchecked(4)
}
}