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