pub struct PartSize(_);
Expand description

分片大小

Implementations§

创建分片大小

如果传入 0 将返回 None

创建分片大小

提供 NonZeroU64 作为并发数类型。

获取并发数

返回 NonZeroU64 作为并发数类型。

Examples found in repository?
src/data_partition_provider/mod.rs (line 52)
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
78
79
    pub fn as_u64(&self) -> u64 {
        self.as_non_zero_u64().get()
    }
}

impl Default for PartSize {
    #[inline]
    fn default() -> Self {
        Self(
            #[allow(unsafe_code)]
            unsafe {
                NonZeroU64::new_unchecked(1 << 22)
            },
        )
    }
}

impl From<NonZeroU64> for PartSize {
    #[inline]
    fn from(size: NonZeroU64) -> Self {
        Self(size)
    }
}

impl From<PartSize> for NonZeroU64 {
    #[inline]
    fn from(size: PartSize) -> Self {
        size.as_non_zero_u64()
    }
More examples
Hide additional examples
src/data_partition_provider/limited.rs (line 63)
62
63
64
65
    fn part_size(&self) -> PartSize {
        let base_partition = self.base.part_size().as_non_zero_u64();
        base_partition.clamp(self.min, self.max).into()
    }
src/data_partition_provider/multiply.rs (line 52)
51
52
53
54
55
56
    fn part_size(&self) -> PartSize {
        let base_partition = self.base.part_size().as_non_zero_u64();
        let multiply = self.multiply.get();
        let partition = base_partition.max(self.multiply).get() / multiply * multiply;
        NonZeroU64::new(partition).unwrap().into()
    }

获取并发数

Examples found in repository?
src/data_partition_provider/mod.rs (line 85)
84
85
86
    fn from(size: PartSize) -> Self {
        size.as_u64()
    }
More examples
Hide additional examples
src/resumable_policy/multiple_partitions.rs (line 83)
82
83
84
    fn threshold(&self) -> u64 {
        self.base_partition_provider.part_size().as_u64() * self.multiply.get()
    }
src/data_source/seekable.rs (line 44)
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
    fn slice(&self, size: PartSize) -> IoResult<Option<DataSourceReader>> {
        let mut cur = self.current.lock().unwrap();
        if cur.offset < self.size {
            let size = size.as_u64();
            let source_reader = DataSourceReader::seekable(
                cur.part_number,
                self.source.clone_with_new_offset_and_length(cur.offset, size),
            );
            cur.offset += size;
            cur.part_number = NonZeroUsize::new(cur.part_number.get() + 1).expect("Page number is too big");
            Ok(Some(source_reader))
        } else {
            Ok(None)
        }
    }
src/data_source/unseekable.rs (line 55)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
    fn slice(&self, size: PartSize) -> IoResult<Option<DataSourceReader>> {
        let mut buf = Vec::new();
        let guard = &mut self.0.lock().unwrap();
        let have_read = (&mut guard.reader).take(size.as_u64()).read_to_end(&mut buf)?;
        if have_read > 0 {
            let source_reader = DataSourceReader::unseekable(guard.current_part_number, buf, guard.current_offset);
            guard.current_offset += have_read as u64;
            guard.current_part_number =
                NonZeroUsize::new(guard.current_part_number.get() + 1).expect("Page number is too big");
            Ok(Some(source_reader))
        } else {
            Ok(None)
        }
    }

    #[inline]
    fn reset(&self) -> IoResult<()> {
        Err(unsupported_reset_error())
    }

    #[inline]
    fn source_key(&self) -> IoResult<Option<SourceKey<A>>> {
        Ok(self.0.lock().unwrap().source_key.to_owned())
    }

    #[inline]
    fn total_size(&self) -> IoResult<Option<u64>> {
        Ok(None)
    }
}

impl<R: Read + Debug + Send + Sync + 'static, A: Digest> Debug for UnseekableDataSourceInner<R, A> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("UnseekableDataSourceInner")
            .field("reader", &self.reader)
            .field("current_offset", &self.current_offset)
            .field("current_part_number", &self.current_part_number)
            .field("source_key", &self.source_key)
            .finish()
    }
}

#[cfg(feature = "async")]
mod async_unseekable {
    use super::{
        super::{AsyncDataSource, AsyncDataSourceReader},
        *,
    };
    use futures::{
        future::{self, BoxFuture},
        lock::Mutex,
        AsyncRead, AsyncReadExt,
    };

    /// 不可寻址的异步数据源
    ///
    /// 基于一个不可寻址的异步阅读器实现了异步数据源接口
    pub struct AsyncUnseekableDataSource<R: AsyncRead + Debug + Unpin + Send + Sync + 'static + ?Sized, A: Digest = Sha1>(
        Arc<Mutex<AsyncUnseekableDataSourceInner<R, A>>>,
    );

    impl<R: AsyncRead + Debug + Unpin + Send + Sync + 'static, A: Digest> Debug for AsyncUnseekableDataSource<R, A> {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            f.debug_tuple("AsyncUnseekableDataSource").field(&self.0).finish()
        }
    }

    impl<R: AsyncRead + Debug + Unpin + Send + Sync + 'static, A: Digest> Clone for AsyncUnseekableDataSource<R, A> {
        #[inline]
        fn clone(&self) -> Self {
            Self(self.0.clone())
        }
    }

    struct AsyncUnseekableDataSourceInner<R: AsyncRead + Debug + Unpin + Send + Sync + 'static + ?Sized, A: Digest> {
        current_offset: u64,
        current_part_number: NonZeroUsize,
        source_key: Option<SourceKey<A>>,
        reader: R,
    }

    impl<R: AsyncRead + Debug + Unpin + Send + Sync + 'static, A: Digest> AsyncUnseekableDataSource<R, A> {
        /// 创建不可寻址的异步数据源
        pub fn new(reader: R) -> Self {
            Self(Arc::new(Mutex::new(AsyncUnseekableDataSourceInner {
                reader,
                current_offset: 0,
                current_part_number: first_part_number(),
                source_key: None,
            })))
        }
    }

    impl<R: AsyncRead + Debug + Unpin + Send + Sync + 'static, A: Digest> AsyncDataSource<A>
        for AsyncUnseekableDataSource<R, A>
    {
        fn slice(&self, size: PartSize) -> BoxFuture<IoResult<Option<AsyncDataSourceReader>>> {
            Box::pin(async move {
                let mut buf = Vec::new();
                let guard = &mut self.0.lock().await;
                let have_read = (&mut guard.reader).take(size.as_u64()).read_to_end(&mut buf).await?;
                if have_read > 0 {
                    let source_reader =
                        AsyncDataSourceReader::unseekable(guard.current_part_number, buf, guard.current_offset);
                    guard.current_offset += have_read as u64;
                    guard.current_part_number =
                        NonZeroUsize::new(guard.current_part_number.get() + 1).expect("Page number is too big");
                    Ok(Some(source_reader))
                } else {
                    Ok(None)
                }
            })
        }
src/data_source/file.rs (line 196)
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
        fn slice(&self, size: PartSize) -> BoxFuture<IoResult<Option<AsyncDataSourceReader>>> {
            Box::pin(async move {
                match self.get_async_seekable_source().await? {
                    AsyncSource::Seekable {
                        source,
                        current,
                        file_size,
                        ..
                    } => {
                        let mut cur = current.lock().await;
                        if cur.offset < *file_size {
                            let size = size.as_u64();
                            let source_reader = AsyncDataSourceReader::seekable(
                                cur.part_number,
                                source.clone_with_new_offset_and_length(cur.offset, size),
                            );
                            cur.offset += size;
                            cur.part_number =
                                NonZeroUsize::new(cur.part_number.get() + 1).expect("Page number is too big");
                            Ok(Some(source_reader))
                        } else {
                            Ok(None)
                        }
                    }
                    AsyncSource::Unseekable(source) => source.slice(size).await,
                }
            })
        }

Methods from Deref<Target = NonZeroU64>§

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
The resulting type after dereferencing.
Dereferences the value.
Mutably dereferences the value.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Converts self into T using Into<T>. Read more
Compare self to key and return true if they are equal.

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Pipes by value. This is generally the method you want to use. Read more
Borrows self and passes that borrow into the pipe function. Read more
Mutably borrows self and passes that borrow into the pipe function. Read more
Borrows self, then passes self.borrow() into the pipe function. Read more
Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more
Borrows self, then passes self.as_ref() into the pipe function.
Mutably borrows self, then passes self.as_mut() into the pipe function.
Borrows self, then passes self.deref() into the pipe function.
Mutably borrows self, then passes self.deref_mut() into the pipe function.
The alignment of pointer.
The type for initializers.
Initializes a with the given initializer. Read more
Dereferences the given pointer. Read more
Mutably dereferences the given pointer. Read more
Drops the object pointed to by the given pointer. Read more
Should always be Self
Immutable access to a value. Read more
Mutable access to a value. Read more
Immutable access to the Borrow<B> of a value. Read more
Mutable access to the BorrowMut<B> of a value. Read more
Immutable access to the AsRef<R> view of a value. Read more
Mutable access to the AsMut<R> view of a value. Read more
Immutable access to the Deref::Target of a value. Read more
Mutable access to the Deref::Target of a value. Read more
Calls .tap() only in debug builds, and is erased in release builds.
Calls .tap_mut() only in debug builds, and is erased in release builds.
Calls .tap_borrow() only in debug builds, and is erased in release builds.
Calls .tap_borrow_mut() only in debug builds, and is erased in release builds.
Calls .tap_ref() only in debug builds, and is erased in release builds.
Calls .tap_ref_mut() only in debug builds, and is erased in release builds.
Calls .tap_deref() only in debug builds, and is erased in release builds.
Calls .tap_deref_mut() only in debug builds, and is erased in release builds.
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Attempts to convert self into T using TryInto<T>. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more