@@ -218,6 +218,45 @@ cfg_if! {
218
218
}
219
219
}
220
220
221
+ #[ derive( Debug , Default ) ]
222
+ pub struct SharedWorkerLocal <T >( T ) ;
223
+
224
+ impl <T > SharedWorkerLocal <T > {
225
+ /// Creates a new worker local where the `initial` closure computes the
226
+ /// value this worker local should take for each thread in the thread pool.
227
+ #[ inline]
228
+ pub fn new<F : FnMut ( usize ) -> T >( mut f: F ) -> SharedWorkerLocal <T > {
229
+ SharedWorkerLocal ( f( 0 ) )
230
+ }
231
+
232
+ #[ inline]
233
+ pub fn iter( & self ) -> impl Iterator <Item =& T > {
234
+ Some ( & self . 0 ) . into_iter( )
235
+ }
236
+
237
+ /// Returns the worker-local value for each thread
238
+ #[ inline]
239
+ pub fn into_inner( self ) -> Vec <T > {
240
+ vec![ self . 0 ]
241
+ }
242
+ }
243
+
244
+ impl <T > Deref for SharedWorkerLocal <T > {
245
+ type Target = T ;
246
+
247
+ #[ inline( always) ]
248
+ fn deref( & self ) -> & T {
249
+ & self . 0
250
+ }
251
+ }
252
+
253
+ impl <T > DerefMut for SharedWorkerLocal <T > {
254
+ #[ inline( always) ]
255
+ fn deref_mut( & mut self ) -> & mut T {
256
+ & mut self . 0
257
+ }
258
+ }
259
+
221
260
pub type MTRef <' a, T > = & ' a mut T ;
222
261
223
262
#[ derive( Debug , Default ) ]
@@ -337,6 +376,54 @@ cfg_if! {
337
376
}
338
377
339
378
pub use rayon_core:: WorkerLocal ;
379
+ pub use rayon_core:: Registry ;
380
+ use rayon_core:: current_thread_index;
381
+
382
+ #[ derive( Debug ) ]
383
+ pub struct SharedWorkerLocal <T >( Vec <T >) ;
384
+
385
+ impl <T > SharedWorkerLocal <T > {
386
+ /// Creates a new worker local where the `initial` closure computes the
387
+ /// value this worker local should take for each thread in the thread pool.
388
+ #[ inline]
389
+ pub fn new<F : FnMut ( usize ) -> T >( mut f: F ) -> SharedWorkerLocal <T > {
390
+ SharedWorkerLocal ( ( 0 ..Registry :: current_num_threads( ) ) . map( |i| f( i) ) . collect( ) )
391
+ }
392
+
393
+ #[ inline]
394
+ pub fn iter( & self ) -> impl Iterator <Item =& T > {
395
+ self . 0 . iter( )
396
+ }
397
+
398
+ /// Returns the worker-local value for each thread
399
+ #[ inline]
400
+ pub fn into_inner( self ) -> Vec <T > {
401
+ self . 0
402
+ }
403
+ }
404
+
405
+ impl <T : Default > Default for SharedWorkerLocal <T > {
406
+ #[ inline]
407
+ fn default ( ) -> Self {
408
+ SharedWorkerLocal :: new( |_| Default :: default ( ) )
409
+ }
410
+ }
411
+
412
+ impl <T > Deref for SharedWorkerLocal <T > {
413
+ type Target = T ;
414
+
415
+ #[ inline( always) ]
416
+ fn deref( & self ) -> & T {
417
+ & self . 0 [ current_thread_index( ) . unwrap( ) ]
418
+ }
419
+ }
420
+
421
+ impl <T > DerefMut for SharedWorkerLocal <T > {
422
+ #[ inline( always) ]
423
+ fn deref_mut( & mut self ) -> & mut T {
424
+ & mut self . 0 [ current_thread_index( ) . unwrap( ) ]
425
+ }
426
+ }
340
427
341
428
pub use rayon:: iter:: ParallelIterator ;
342
429
use rayon:: iter:: IntoParallelIterator ;
0 commit comments