Skip to content

Commit 2740311

Browse files
committed
Add annotations to ensure that readers are Sync.
1 parent 54f2453 commit 2740311

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

src/lib.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1395,6 +1395,16 @@ where
13951395
}
13961396
}
13971397

1398+
/// Ensure that readers are Sync.
1399+
#[allow(dead_code)]
1400+
const fn assert_readers_are_sync() {
1401+
const fn takes_sync<T: Sync>() {}
1402+
takes_sync::<ShardReader<usize, DefaultSort>>();
1403+
takes_sync::<RangeIter<'static, usize, DefaultSort>>();
1404+
takes_sync::<ShardIter<'static, usize, DefaultSort>>();
1405+
takes_sync::<MergeIterator<'static, usize, DefaultSort>>();
1406+
}
1407+
13981408
#[cfg(test)]
13991409
mod shard_tests {
14001410
use super::*;
@@ -1658,7 +1668,7 @@ mod shard_tests {
16581668
where
16591669
T: 'static + Serialize + DeserializeOwned + Clone + Send + Eq + Debug + Hash,
16601670
S: SortKey<T>,
1661-
<S as SortKey<T>>::Key: 'static + Send + Serialize + DeserializeOwned,
1671+
<S as SortKey<T>>::Key: 'static + Send + Sync + Serialize + DeserializeOwned,
16621672
{
16631673
let mut files = Vec::new();
16641674

src/unsorted.rs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ where
3333
{
3434
/// Iterator over the shard files in the set, opening readers.
3535
shard_reader_iter:
36-
Box<dyn Iterator<Item = Result<UnsortedShardFileReader<T, S>, Error>> + Send>,
36+
Box<dyn Iterator<Item = Result<UnsortedShardFileReader<T, S>, Error>> + Send + Sync>,
3737

3838
/// Iterator over the current shard file.
3939
active_shard_reader: Option<UnsortedShardFileReader<T, S>>,
@@ -42,22 +42,16 @@ where
4242
impl<T, S> UnsortedShardReader<T, S>
4343
where
4444
T: DeserializeOwned,
45-
<S as SortKey<T>>::Key: Clone + Ord + DeserializeOwned + Send,
45+
<S as SortKey<T>>::Key: Clone + Ord + DeserializeOwned + Send + Sync + 'static,
4646
S: SortKey<T>,
4747
{
4848
/// Open a single shard file.
49-
pub fn open<P: AsRef<Path>>(shard_file: P) -> Self
50-
where
51-
<S as SortKey<T>>::Key: 'static,
52-
{
49+
pub fn open<P: AsRef<Path>>(shard_file: P) -> Self {
5350
Self::open_set(&[shard_file])
5451
}
5552

5653
/// Open a set of shard files.
57-
pub fn open_set<P: AsRef<Path>>(shard_files: &[P]) -> Self
58-
where
59-
<S as SortKey<T>>::Key: 'static,
60-
{
54+
pub fn open_set<P: AsRef<Path>>(shard_files: &[P]) -> Self {
6155
let reader_iter = shard_files
6256
.iter()
6357
.map(|f| f.as_ref().into())
@@ -71,10 +65,7 @@ where
7165
}
7266

7367
/// Compute the total number of elements in this set of shard files.
74-
pub fn len<P: AsRef<Path>>(shard_files: &[P]) -> Result<usize, Error>
75-
where
76-
<S as SortKey<T>>::Key: 'static,
77-
{
68+
pub fn len<P: AsRef<Path>>(shard_files: &[P]) -> Result<usize, Error> {
7869
// Create a set reader, and consume all the files, just getting counts.
7970
let files_reader = Self::open_set(shard_files);
8071
let mut count = 0;
@@ -138,7 +129,7 @@ where
138129
impl<T, S> Iterator for UnsortedShardReader<T, S>
139130
where
140131
T: DeserializeOwned,
141-
<S as SortKey<T>>::Key: Clone + Ord + DeserializeOwned + Send,
132+
<S as SortKey<T>>::Key: Clone + Ord + DeserializeOwned + Send + Sync + 'static,
142133
S: SortKey<T>,
143134
{
144135
type Item = Result<T, Error>;
@@ -178,23 +169,20 @@ where
178169
S: SortKey<T>,
179170
{
180171
count: usize,
181-
file_index_iter: Box<dyn Iterator<Item = KeylessShardRecord> + Send>,
172+
file_index_iter: Box<dyn Iterator<Item = KeylessShardRecord> + Send + Sync>,
182173
shard_iter: Option<UnsortedShardIter<T>>,
183174
phantom: PhantomData<S>,
184175
}
185176

186177
impl<T, S> UnsortedShardFileReader<T, S>
187178
where
188179
T: DeserializeOwned,
189-
<S as SortKey<T>>::Key: Clone + Ord + DeserializeOwned + Send,
180+
<S as SortKey<T>>::Key: Clone + Ord + DeserializeOwned + Send + Sync + 'static,
190181
S: SortKey<T>,
191182
{
192183
/// Create a unsorted reader for a single shard file.
193184
/// Return Ok(None) if the specified shard file is empty.
194-
pub fn new(path: &Path) -> Result<Option<Self>, Error>
195-
where
196-
<S as SortKey<T>>::Key: 'static,
197-
{
185+
pub fn new(path: &Path) -> Result<Option<Self>, Error> {
198186
let reader = ShardReaderSingle::<T, S>::open(path)?;
199187
let count = reader.len();
200188
let mut file_index_iter = reader.index.into_iter().map(|r| KeylessShardRecord {
@@ -348,3 +336,11 @@ struct SkipResult {
348336
skipped: usize,
349337
exhausted: bool,
350338
}
339+
340+
/// Ensure that readers are Sync.
341+
#[allow(dead_code)]
342+
const fn assert_readers_are_sync() {
343+
const fn takes_sync<T: Sync>() {}
344+
takes_sync::<UnsortedShardFileReader<usize, DefaultSort>>();
345+
takes_sync::<UnsortedShardReader<usize, DefaultSort>>();
346+
}

0 commit comments

Comments
 (0)