@@ -11,13 +11,12 @@ mod macros;
11
11
mod chunks;
12
12
pub mod iter;
13
13
mod lanes;
14
+ mod trusted;
14
15
mod windows;
15
16
16
17
use std:: iter:: FromIterator ;
17
18
use 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 } ;
21
20
22
21
use crate :: imp_prelude:: * ;
23
22
use crate :: Ix1 ;
@@ -27,6 +26,7 @@ use super::{NdProducer, RemoveAxis};
27
26
pub use self :: chunks:: { ExactChunks , ExactChunksIter , ExactChunksIterMut , ExactChunksMut } ;
28
27
pub use self :: lanes:: { Lanes , LanesMut } ;
29
28
pub use self :: windows:: Windows ;
29
+ pub ( crate ) use self :: trusted:: { to_vec, to_vec_mapped} ;
30
30
31
31
use crate :: dimension;
32
32
@@ -1504,65 +1504,6 @@ send_sync_read_write!(AxisIterMut);
1504
1504
send_sync_read_write ! ( AxisChunksIterMut ) ;
1505
1505
send_sync_read_write ! ( ElementsBaseMut ) ;
1506
1506
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
-
1566
1507
#[ cfg( test) ]
1567
1508
#[ cfg( feature = "std" ) ]
1568
1509
mod tests {
0 commit comments