qiniu_upload_manager/concurrency_provider/
fixed.rs

1use std::num::NonZeroUsize;
2
3use super::{Concurrency, ConcurrencyProvider, ConcurrencyProviderFeedback};
4
5/// 固定并发数提供者
6#[derive(Debug, Clone, Copy)]
7pub struct FixedConcurrencyProvider(NonZeroUsize);
8
9impl FixedConcurrencyProvider {
10    /// 创建固定并发数提供者
11    ///
12    /// 如果传入 `0` 将返回 [`None`]。
13    #[inline]
14    pub fn new(concurrency: usize) -> Option<Self> {
15        NonZeroUsize::new(concurrency).map(Self::new_with_non_zero_concurrency)
16    }
17
18    /// 创建固定并发数提供者
19    ///
20    /// 提供 [`NonZeroUsize`] 作为并发数类型。
21    #[inline]
22    pub const fn new_with_non_zero_concurrency(concurrency: NonZeroUsize) -> Self {
23        Self(concurrency)
24    }
25
26    /// 获取固定并发数
27    #[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}