qiniu_upload_manager/concurrency_provider/
fixed.rs1use std::num::NonZeroUsize;
2
3use super::{Concurrency, ConcurrencyProvider, ConcurrencyProviderFeedback};
4
5#[derive(Debug, Clone, Copy)]
7pub struct FixedConcurrencyProvider(NonZeroUsize);
8
9impl FixedConcurrencyProvider {
10 #[inline]
14 pub fn new(concurrency: usize) -> Option<Self> {
15 NonZeroUsize::new(concurrency).map(Self::new_with_non_zero_concurrency)
16 }
17
18 #[inline]
22 pub const fn new_with_non_zero_concurrency(concurrency: NonZeroUsize) -> Self {
23 Self(concurrency)
24 }
25
26 #[inline]
28 pub const fn fixed_concurrency(&self) -> NonZeroUsize {
29 self.0
30 }
31}
32
33impl Default for FixedConcurrencyProvider {
34 #[inline]
35 fn default() -> Self {
36 Self::new_with_non_zero_concurrency(default_non_zero_concurrency())
37 }
38}
39
40impl ConcurrencyProvider for FixedConcurrencyProvider {
41 #[inline]
42 fn concurrency(&self) -> Concurrency {
43 self.fixed_concurrency().into()
44 }
45
46 #[inline]
47 fn feedback(&self, _feedback: ConcurrencyProviderFeedback<'_>) {}
48}
49
50impl From<NonZeroUsize> for FixedConcurrencyProvider {
51 #[inline]
52 fn from(concurrency: NonZeroUsize) -> Self {
53 Self(concurrency)
54 }
55}
56
57fn default_non_zero_concurrency() -> NonZeroUsize {
58 #[allow(unsafe_code)]
59 unsafe {
60 NonZeroUsize::new_unchecked(4)
61 }
62}