@@ -12,13 +12,12 @@ mod chunks;
12
12
mod into_iter;
13
13
pub mod iter;
14
14
mod lanes;
15
+ mod trusted;
15
16
mod windows;
16
17
17
18
use std:: iter:: FromIterator ;
18
19
use std:: marker:: PhantomData ;
19
- use std:: ptr;
20
- use std:: slice:: { self , Iter as SliceIter , IterMut as SliceIterMut } ;
21
- use alloc:: vec:: Vec ;
20
+ use std:: slice:: { Iter as SliceIter , IterMut as SliceIterMut } ;
22
21
23
22
use crate :: imp_prelude:: * ;
24
23
use crate :: Ix1 ;
@@ -29,6 +28,7 @@ pub use self::chunks::{ExactChunks, ExactChunksIter, ExactChunksIterMut, ExactCh
29
28
pub use self :: lanes:: { Lanes , LanesMut } ;
30
29
pub use self :: windows:: Windows ;
31
30
pub use self :: into_iter:: IntoIter ;
31
+ pub ( crate ) use self :: trusted:: { TrustedIterator , to_vec, to_vec_mapped} ;
32
32
33
33
use crate :: dimension;
34
34
@@ -1506,66 +1506,6 @@ send_sync_read_write!(AxisIterMut);
1506
1506
send_sync_read_write ! ( AxisChunksIterMut ) ;
1507
1507
send_sync_read_write ! ( ElementsBaseMut ) ;
1508
1508
1509
- /// (Trait used internally) An iterator that we trust
1510
- /// to deliver exactly as many items as it said it would.
1511
- ///
1512
- /// The iterator must produce exactly the number of elements it reported or
1513
- /// diverge before reaching the end.
1514
- pub ( crate ) unsafe trait TrustedIterator { }
1515
-
1516
- use crate :: indexes:: IndicesIterF ;
1517
- use crate :: iter:: IndicesIter ;
1518
- #[ cfg( feature = "std" ) ]
1519
- use crate :: { geomspace:: Geomspace , linspace:: Linspace , logspace:: Logspace } ;
1520
- #[ cfg( feature = "std" ) ]
1521
- unsafe impl < F > TrustedIterator for Linspace < F > { }
1522
- #[ cfg( feature = "std" ) ]
1523
- unsafe impl < F > TrustedIterator for Geomspace < F > { }
1524
- #[ cfg( feature = "std" ) ]
1525
- unsafe impl < F > TrustedIterator for Logspace < F > { }
1526
- unsafe impl < ' a , A , D > TrustedIterator for Iter < ' a , A , D > { }
1527
- unsafe impl < ' a , A , D > TrustedIterator for IterMut < ' a , A , D > { }
1528
- unsafe impl < I > TrustedIterator for std:: iter:: Cloned < I > where I : TrustedIterator { }
1529
- unsafe impl < I , F > TrustedIterator for std:: iter:: Map < I , F > where I : TrustedIterator { }
1530
- unsafe impl < ' a , A > TrustedIterator for slice:: Iter < ' a , A > { }
1531
- unsafe impl < ' a , A > TrustedIterator for slice:: IterMut < ' a , A > { }
1532
- unsafe impl TrustedIterator for :: std:: ops:: Range < usize > { }
1533
- // FIXME: These indices iter are dubious -- size needs to be checked up front.
1534
- unsafe impl < D > TrustedIterator for IndicesIter < D > where D : Dimension { }
1535
- unsafe impl < D > TrustedIterator for IndicesIterF < D > where D : Dimension { }
1536
- unsafe impl < A , D > TrustedIterator for IntoIter < A , D > where D : Dimension { }
1537
-
1538
- /// Like Iterator::collect, but only for trusted length iterators
1539
- pub ( crate ) fn to_vec < I > ( iter : I ) -> Vec < I :: Item >
1540
- where
1541
- I : TrustedIterator + ExactSizeIterator ,
1542
- {
1543
- to_vec_mapped ( iter, |x| x)
1544
- }
1545
-
1546
- /// Like Iterator::collect, but only for trusted length iterators
1547
- pub ( crate ) fn to_vec_mapped < I , F , B > ( iter : I , mut f : F ) -> Vec < B >
1548
- where
1549
- I : TrustedIterator + ExactSizeIterator ,
1550
- F : FnMut ( I :: Item ) -> B ,
1551
- {
1552
- // Use an `unsafe` block to do this efficiently.
1553
- // We know that iter will produce exactly .size() elements,
1554
- // and the loop can vectorize if it's clean (without branch to grow the vector).
1555
- let ( size, _) = iter. size_hint ( ) ;
1556
- let mut result = Vec :: with_capacity ( size) ;
1557
- let mut out_ptr = result. as_mut_ptr ( ) ;
1558
- let mut len = 0 ;
1559
- iter. fold ( ( ) , |( ) , elt| unsafe {
1560
- ptr:: write ( out_ptr, f ( elt) ) ;
1561
- len += 1 ;
1562
- result. set_len ( len) ;
1563
- out_ptr = out_ptr. offset ( 1 ) ;
1564
- } ) ;
1565
- debug_assert_eq ! ( size, result. len( ) ) ;
1566
- result
1567
- }
1568
-
1569
1509
#[ cfg( test) ]
1570
1510
#[ cfg( feature = "std" ) ]
1571
1511
mod tests {
0 commit comments