Skip to content

Commit 72a4067

Browse files
lrh2000cchanging
authored andcommitted
Implement missing Send and Sync traits
1 parent ebede39 commit 72a4067

3 files changed

Lines changed: 64 additions & 0 deletions

File tree

src/borrow.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ pub(super) struct DormantMutRef<'a, T> {
1010
_marker: PhantomData<&'a mut T>,
1111
}
1212

13+
// SAFETY: `DormantMutRef<'a, T>` represents a value of `&'a mut T`.
14+
unsafe impl<'a, T> Send for DormantMutRef<'a, T> where &'a mut T: Send {}
15+
unsafe impl<'a, T> Sync for DormantMutRef<'a, T> where &'a mut T: Sync {}
16+
1317
impl<'a, T> DormantMutRef<'a, T> {
1418
/// Creates a dormant mutable reference and returns both the original reference and the dormant
1519
/// reference, so that the original reference can continue to be used.
@@ -75,6 +79,10 @@ pub(super) struct DestroyableRef<'a, T> {
7579
_marker: PhantomData<&'a T>,
7680
}
7781

82+
// SAFETY: `DestroyableRef<'a, T>` represents a value of `&'a T`.
83+
unsafe impl<'a, T> Send for DestroyableRef<'a, T> where &'a T: Send {}
84+
unsafe impl<'a, T> Sync for DestroyableRef<'a, T> where &'a T: Sync {}
85+
7886
impl<'a, T> DestroyableRef<'a, T> {
7987
/// Creates a destroyable reference from an immutable reference.
8088
pub fn new(val: &'a T) -> Self {
@@ -99,6 +107,10 @@ pub(super) struct DestroyableMutRef<'a, T> {
99107
_marker: PhantomData<&'a mut T>,
100108
}
101109

110+
// SAFETY: `DestroyableMutRef<'a, T>` represents a value of `&'a mut T`.
111+
unsafe impl<'a, T> Send for DestroyableMutRef<'a, T> where &'a mut T: Send {}
112+
unsafe impl<'a, T> Sync for DestroyableMutRef<'a, T> where &'a mut T: Sync {}
113+
102114
impl<'a, T> DestroyableMutRef<'a, T> {
103115
/// Creates a destroyable reference from an immutable reference.
104116
pub fn new(val: &'a mut T) -> Self {

src/entry.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ where
164164
_marker: core::marker::PhantomData<(Arc<XNode<I>>, I)>,
165165
}
166166

167+
// SAFETY: `XEntry<I>` represents a value of either `Arc<XNode<I>>` or `I`.
168+
unsafe impl<I: ItemEntry> Send for XEntry<I> where (Arc<XNode<I>>, I): Send {}
169+
unsafe impl<I: ItemEntry> Sync for XEntry<I> where (Arc<XNode<I>>, I): Sync {}
170+
167171
#[derive(PartialEq, Eq, Debug)]
168172
#[repr(usize)]
169173
enum EntryType {

src/test.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ extern crate std;
22
use crate::*;
33
use std::boxed::Box;
44
use std::sync::Arc;
5+
use std::thread;
56

67
extern crate test;
78
use test::Bencher;
@@ -481,6 +482,53 @@ fn test_range() {
481482
assert_eq!(count, n!(5));
482483
}
483484

485+
#[test]
486+
fn test_threads() {
487+
let mut xarray_arc: XArray<Arc<i32>> = XArray::new();
488+
xarray_arc.store(2, Arc::new(3));
489+
490+
thread::scope(|scope| {
491+
scope.spawn(|| {
492+
assert_eq!(xarray_arc.load(1), None);
493+
assert_eq!(*xarray_arc.load(2).unwrap().as_ref(), 3);
494+
});
495+
scope.spawn(|| {
496+
assert_eq!(*xarray_arc.load(2).unwrap().as_ref(), 3);
497+
assert_eq!(xarray_arc.load(1), None);
498+
});
499+
});
500+
501+
thread::spawn(move || {
502+
assert_eq!(*xarray_arc.load(2).unwrap().as_ref(), 3);
503+
})
504+
.join()
505+
.unwrap();
506+
}
507+
508+
#[test]
509+
fn test_cursor_threads() {
510+
let mut xarray_arc: XArray<Arc<i32>> = XArray::new();
511+
xarray_arc.store(2, Arc::new(3));
512+
513+
let cursor = xarray_arc.cursor(2);
514+
thread::scope(|scope| {
515+
scope.spawn(|| {
516+
assert_eq!(cursor.index(), 2);
517+
});
518+
scope.spawn(|| {
519+
assert_eq!(cursor.index(), 2);
520+
});
521+
});
522+
drop(cursor);
523+
524+
thread::scope(|scope| {
525+
let cursor = xarray_arc.cursor(3);
526+
scope.spawn(move || {
527+
assert_eq!(cursor.index(), 3);
528+
});
529+
});
530+
}
531+
484532
#[bench]
485533
fn benchmark_cursor_load(b: &mut Bencher) {
486534
b.iter(|| test_cursor_load());

0 commit comments

Comments
 (0)