Skip to content

Commit 281d17c

Browse files
committed
FIX: Move TrustedIterator related items to submodule
1 parent 5d701b6 commit 281d17c

File tree

2 files changed

+70
-62
lines changed

2 files changed

+70
-62
lines changed

src/iterators/mod.rs

Lines changed: 3 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ mod macros;
1111
mod chunks;
1212
pub mod iter;
1313
mod lanes;
14+
mod trusted;
1415
mod windows;
1516

1617
use std::iter::FromIterator;
1718
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};
2120

2221
use crate::imp_prelude::*;
2322
use crate::Ix1;
@@ -27,6 +26,7 @@ use super::{NdProducer, RemoveAxis};
2726
pub use self::chunks::{ExactChunks, ExactChunksIter, ExactChunksIterMut, ExactChunksMut};
2827
pub use self::lanes::{Lanes, LanesMut};
2928
pub use self::windows::Windows;
29+
pub(crate) use self::trusted::{to_vec, to_vec_mapped};
3030

3131
use crate::dimension;
3232

@@ -1504,65 +1504,6 @@ send_sync_read_write!(AxisIterMut);
15041504
send_sync_read_write!(AxisChunksIterMut);
15051505
send_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")]
15681509
mod tests {

src/iterators/trusted.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
use std::ptr;
3+
use std::slice;
4+
use alloc::vec::Vec;
5+
6+
use crate::Dimension;
7+
use super::{Iter, IterMut};
8+
9+
/// (Trait used internally) An iterator that we trust
10+
/// to deliver exactly as many items as it said it would.
11+
///
12+
/// The iterator must produce exactly the number of elements it reported or
13+
/// diverge before reaching the end.
14+
pub(crate) unsafe trait TrustedIterator {}
15+
16+
use crate::indexes::IndicesIterF;
17+
use crate::iter::IndicesIter;
18+
#[cfg(feature = "std")]
19+
use crate::{geomspace::Geomspace, linspace::Linspace, logspace::Logspace};
20+
#[cfg(feature = "std")]
21+
unsafe impl<F> TrustedIterator for Linspace<F> {}
22+
#[cfg(feature = "std")]
23+
unsafe impl<F> TrustedIterator for Geomspace<F> {}
24+
#[cfg(feature = "std")]
25+
unsafe impl<F> TrustedIterator for Logspace<F> {}
26+
unsafe impl<'a, A, D> TrustedIterator for Iter<'a, A, D> {}
27+
unsafe impl<'a, A, D> TrustedIterator for IterMut<'a, A, D> {}
28+
unsafe impl<I> TrustedIterator for std::iter::Cloned<I> where I: TrustedIterator {}
29+
unsafe impl<I, F> TrustedIterator for std::iter::Map<I, F> where I: TrustedIterator {}
30+
unsafe impl<'a, A> TrustedIterator for slice::Iter<'a, A> {}
31+
unsafe impl<'a, A> TrustedIterator for slice::IterMut<'a, A> {}
32+
unsafe impl TrustedIterator for ::std::ops::Range<usize> {}
33+
// FIXME: These indices iter are dubious -- size needs to be checked up front.
34+
unsafe impl<D> TrustedIterator for IndicesIter<D> where D: Dimension {}
35+
unsafe impl<D> TrustedIterator for IndicesIterF<D> where D: Dimension {}
36+
37+
/// Like Iterator::collect, but only for trusted length iterators
38+
pub(crate) fn to_vec<I>(iter: I) -> Vec<I::Item>
39+
where
40+
I: TrustedIterator + ExactSizeIterator,
41+
{
42+
to_vec_mapped(iter, |x| x)
43+
}
44+
45+
/// Like Iterator::collect, but only for trusted length iterators
46+
pub(crate) fn to_vec_mapped<I, F, B>(iter: I, mut f: F) -> Vec<B>
47+
where
48+
I: TrustedIterator + ExactSizeIterator,
49+
F: FnMut(I::Item) -> B,
50+
{
51+
// Use an `unsafe` block to do this efficiently.
52+
// We know that iter will produce exactly .size() elements,
53+
// and the loop can vectorize if it's clean (without branch to grow the vector).
54+
let (size, _) = iter.size_hint();
55+
let mut result = Vec::with_capacity(size);
56+
let mut out_ptr = result.as_mut_ptr();
57+
let mut len = 0;
58+
iter.fold((), |(), elt| unsafe {
59+
ptr::write(out_ptr, f(elt));
60+
len += 1;
61+
result.set_len(len);
62+
out_ptr = out_ptr.offset(1);
63+
});
64+
debug_assert_eq!(size, result.len());
65+
result
66+
}
67+

0 commit comments

Comments
 (0)