@@ -9,8 +9,20 @@ pub struct SeaCow<'a, T> {
9
9
unsafe impl < T : Send > Send for SeaCowInner < ' _ , T > { }
10
10
unsafe impl < T : Sync > Sync for SeaCowInner < ' _ , T > { }
11
11
12
- unsafe impl < T : Send > Send for SeaCow < ' _ , * const T > { }
13
- unsafe impl < T : Sync > Sync for SeaCow < ' _ , * const T > { }
12
+ /// Rust assumes `*const T` is never `Send`/`Sync`, but it can be.
13
+ /// This is fudge for https://github.com/rust-lang/rust/issues/93367
14
+ #[ repr( transparent) ]
15
+ #[ derive( Copy , Clone ) ]
16
+ pub ( crate ) struct Pointer < T > ( pub * const T ) ;
17
+
18
+ #[ derive( Copy , Clone ) ]
19
+ #[ repr( transparent) ]
20
+ pub ( crate ) struct PointerMut < T > ( pub * mut T ) ;
21
+
22
+ unsafe impl < T : Send + Sync > Send for Pointer < T > { }
23
+ unsafe impl < T : Send + Sync > Sync for Pointer < T > { }
24
+ unsafe impl < T : Send + Sync > Send for PointerMut < T > { }
25
+ unsafe impl < T : Send + Sync > Sync for PointerMut < T > { }
14
26
15
27
impl < ' a , T > SeaCow < ' a , T > {
16
28
#[ inline]
@@ -78,13 +90,13 @@ impl<'a, T> SeaCow<'a, T> {
78
90
}
79
91
80
92
pub ( crate ) struct RowBitmap < ' a , T > {
81
- rows : & ' a [ * const T ] ,
93
+ rows : & ' a [ Pointer < T > ] ,
82
94
width : usize ,
83
95
}
84
96
unsafe impl < T : Send + Sync > Send for RowBitmap < ' _ , T > { }
85
97
86
98
pub ( crate ) struct RowBitmapMut < ' a , T > {
87
- rows : MutCow < ' a , [ * mut T ] > ,
99
+ rows : MutCow < ' a , [ PointerMut < T > ] > ,
88
100
width : usize ,
89
101
}
90
102
unsafe impl < T : Send + Sync > Send for RowBitmapMut < ' _ , T > { }
@@ -94,16 +106,16 @@ impl<'a, T> RowBitmapMut<'a, MaybeUninit<T>> {
94
106
pub ( crate ) unsafe fn assume_init < ' maybeowned > ( & ' maybeowned mut self ) -> RowBitmap < ' maybeowned , T > {
95
107
RowBitmap {
96
108
width : self . width ,
97
- rows : std:: mem:: transmute :: < & ' maybeowned [ * mut MaybeUninit < T > ] , & ' maybeowned [ * const T ] > ( self . rows . borrow ( ) ) ,
109
+ rows : std:: mem:: transmute :: < & ' maybeowned [ PointerMut < MaybeUninit < T > > ] , & ' maybeowned [ Pointer < T > ] > ( self . rows . borrow ( ) ) ,
98
110
}
99
111
}
100
112
}
101
113
102
114
impl < ' a , T > RowBitmap < ' a , T > {
103
115
pub fn rows ( & self ) -> impl Iterator < Item = & [ T ] > {
104
116
let width = self . width ;
105
- self . rows . iter ( ) . map ( move |& row| {
106
- unsafe { std:: slice:: from_raw_parts ( row, width) }
117
+ self . rows . iter ( ) . map ( move |row| {
118
+ unsafe { std:: slice:: from_raw_parts ( row. 0 , width) }
107
119
} )
108
120
}
109
121
}
@@ -127,7 +139,7 @@ impl<'a, T: Sync + Send + Copy + 'static> RowBitmapMut<'a, T> {
127
139
#[ inline]
128
140
pub fn new_contiguous ( data : & mut [ T ] , width : usize ) -> Self {
129
141
Self {
130
- rows : MutCow :: Owned ( data. chunks_exact_mut ( width) . map ( |r| r. as_mut_ptr ( ) ) . collect ( ) ) ,
142
+ rows : MutCow :: Owned ( data. chunks_exact_mut ( width) . map ( |r| PointerMut ( r. as_mut_ptr ( ) ) ) . collect ( ) ) ,
131
143
width,
132
144
}
133
145
}
@@ -137,18 +149,14 @@ impl<'a, T: Sync + Send + Copy + 'static> RowBitmapMut<'a, T> {
137
149
#[ cfg( feature = "_internal_c_ffi" ) ]
138
150
pub unsafe fn new ( rows : & ' a mut [ * mut T ] , width : usize ) -> Self {
139
151
Self {
140
- rows : MutCow :: Borrowed ( rows) ,
152
+ rows : MutCow :: Borrowed ( std :: mem :: transmute :: < & ' a mut [ * mut T ] , & ' a mut [ PointerMut < T > ] > ( rows) ) ,
141
153
width,
142
154
}
143
155
}
144
156
145
157
pub fn rows_mut ( & mut self ) -> impl Iterator < Item = & mut [ T ] > + Send {
146
158
let width = self . width ;
147
- // Rust is pessimistic about `*mut` pointers
148
- struct ItIsSync < T > ( * mut T ) ;
149
- unsafe impl < T : Send + Sync > Sync for ItIsSync < T > { }
150
- let send_slice = unsafe { std:: mem:: transmute :: < & mut [ * mut T ] , & mut [ ItIsSync < T > ] > ( self . rows . borrow ( ) ) } ;
151
- send_slice. iter ( ) . map ( move |row| {
159
+ self . rows . borrow ( ) . iter ( ) . map ( move |row| {
152
160
unsafe { std:: slice:: from_raw_parts_mut ( row. 0 , width) }
153
161
} )
154
162
}
0 commit comments