@@ -12,13 +12,12 @@ mod chunks;
1212mod into_iter;
1313pub mod iter;
1414mod lanes;
15+ mod trusted;
1516mod windows;
1617
1718use std:: iter:: FromIterator ;
1819use 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 } ;
2221
2322use crate :: imp_prelude:: * ;
2423use crate :: Ix1 ;
@@ -29,6 +28,7 @@ pub use self::chunks::{ExactChunks, ExactChunksIter, ExactChunksIterMut, ExactCh
2928pub use self :: lanes:: { Lanes , LanesMut } ;
3029pub use self :: windows:: Windows ;
3130pub use self :: into_iter:: IntoIter ;
31+ pub ( crate ) use self :: trusted:: { TrustedIterator , to_vec, to_vec_mapped} ;
3232
3333use crate :: dimension;
3434
@@ -1506,66 +1506,6 @@ send_sync_read_write!(AxisIterMut);
15061506send_sync_read_write ! ( AxisChunksIterMut ) ;
15071507send_sync_read_write ! ( ElementsBaseMut ) ;
15081508
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-
15691509#[ cfg( test) ]
15701510#[ cfg( feature = "std" ) ]
15711511mod tests {
0 commit comments