15
15
16
16
use iter:: * ;
17
17
use iter:: plumbing:: * ;
18
- use slice;
19
18
use split_producer:: * ;
20
19
21
20
@@ -79,6 +78,11 @@ pub trait ParallelString {
79
78
80
79
/// Returns a parallel iterator over the bytes of a string.
81
80
///
81
+ /// Note that multi-byte sequences (for code points greater than `U+007F`)
82
+ /// are produced as separate items, but will not be split across threads.
83
+ /// If you would prefer an indexed iterator without that guarantee, consider
84
+ /// `string.as_bytes().par_iter().cloned()` instead.
85
+ ///
82
86
/// # Examples
83
87
///
84
88
/// ```
@@ -87,13 +91,12 @@ pub trait ParallelString {
87
91
/// assert_eq!(Some(b'o'), max);
88
92
/// ```
89
93
fn par_bytes ( & self ) -> Bytes {
90
- let bytes = self . as_parallel_string ( ) . as_bytes ( ) ;
91
- Bytes { inner : bytes. par_iter ( ) . cloned ( ) }
94
+ Bytes { chars : self . as_parallel_string ( ) }
92
95
}
93
96
94
97
/// Returns a parallel iterator over a string encoded as UTF-16.
95
98
///
96
- /// Note that surrogate pairs (for codepoints greater than `U+FFFF`) are
99
+ /// Note that surrogate pairs (for code points greater than `U+FFFF`) are
97
100
/// produced as separate items, but will not be split across threads.
98
101
///
99
102
/// # Examples
@@ -445,14 +448,43 @@ impl<'ch> UnindexedProducer for CharIndicesProducer<'ch> {
445
448
/// Parallel iterator over the bytes of a string
446
449
#[ derive( Debug , Clone ) ]
447
450
pub struct Bytes < ' ch > {
448
- inner : Cloned < slice :: Iter < ' ch , u8 > > ,
451
+ chars : & ' ch str ,
449
452
}
450
453
451
- delegate_indexed_iterator ! {
452
- Bytes <' ch> => u8 ,
453
- impl <' ch>
454
+ struct BytesProducer < ' ch > {
455
+ chars : & ' ch str ,
454
456
}
455
457
458
+ impl < ' ch > ParallelIterator for Bytes < ' ch > {
459
+ type Item = u8 ;
460
+
461
+ fn drive_unindexed < C > ( self , consumer : C ) -> C :: Result
462
+ where C : UnindexedConsumer < Self :: Item >
463
+ {
464
+ bridge_unindexed ( BytesProducer { chars : self . chars } , consumer)
465
+ }
466
+ }
467
+
468
+ impl < ' ch > UnindexedProducer for BytesProducer < ' ch > {
469
+ type Item = u8 ;
470
+
471
+ fn split ( mut self ) -> ( Self , Option < Self > ) {
472
+ let index = find_char_midpoint ( self . chars ) ;
473
+ if index > 0 {
474
+ let ( left, right) = self . chars . split_at ( index) ;
475
+ self . chars = left;
476
+ ( self , Some ( BytesProducer { chars : right } ) )
477
+ } else {
478
+ ( self , None )
479
+ }
480
+ }
481
+
482
+ fn fold_with < F > ( self , folder : F ) -> F
483
+ where F : Folder < Self :: Item >
484
+ {
485
+ folder. consume_iter ( self . chars . bytes ( ) )
486
+ }
487
+ }
456
488
457
489
// /////////////////////////////////////////////////////////////////////////
458
490
0 commit comments