Skip to content

Commit acb5854

Browse files
committed
FIX: Move TrustedIterator related items to submodule
1 parent a334c2f commit acb5854

File tree

2 files changed

+71
-63
lines changed

2 files changed

+71
-63
lines changed

src/iterators/mod.rs

+3-63
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ mod chunks;
1212
mod into_iter;
1313
pub mod iter;
1414
mod lanes;
15+
mod trusted;
1516
mod windows;
1617

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

2322
use crate::imp_prelude::*;
2423
use crate::Ix1;
@@ -29,6 +28,7 @@ pub use self::chunks::{ExactChunks, ExactChunksIter, ExactChunksIterMut, ExactCh
2928
pub use self::lanes::{Lanes, LanesMut};
3029
pub use self::windows::Windows;
3130
pub use self::into_iter::IntoIter;
31+
pub(crate) use self::trusted::{TrustedIterator, to_vec, to_vec_mapped};
3232

3333
use crate::dimension;
3434

@@ -1506,66 +1506,6 @@ send_sync_read_write!(AxisIterMut);
15061506
send_sync_read_write!(AxisChunksIterMut);
15071507
send_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")]
15711511
mod tests {

src/iterators/trusted.rs

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
use std::ptr;
3+
use std::slice;
4+
use alloc::vec::Vec;
5+
6+
use crate::Dimension;
7+
use super::{Iter, IterMut, IntoIter};
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+
unsafe impl<A, D> TrustedIterator for IntoIter<A, D> where D: Dimension {}
37+
38+
/// Like Iterator::collect, but only for trusted length iterators
39+
pub(crate) fn to_vec<I>(iter: I) -> Vec<I::Item>
40+
where
41+
I: TrustedIterator + ExactSizeIterator,
42+
{
43+
to_vec_mapped(iter, |x| x)
44+
}
45+
46+
/// Like Iterator::collect, but only for trusted length iterators
47+
pub(crate) fn to_vec_mapped<I, F, B>(iter: I, mut f: F) -> Vec<B>
48+
where
49+
I: TrustedIterator + ExactSizeIterator,
50+
F: FnMut(I::Item) -> B,
51+
{
52+
// Use an `unsafe` block to do this efficiently.
53+
// We know that iter will produce exactly .size() elements,
54+
// and the loop can vectorize if it's clean (without branch to grow the vector).
55+
let (size, _) = iter.size_hint();
56+
let mut result = Vec::with_capacity(size);
57+
let mut out_ptr = result.as_mut_ptr();
58+
let mut len = 0;
59+
iter.fold((), |(), elt| unsafe {
60+
ptr::write(out_ptr, f(elt));
61+
len += 1;
62+
result.set_len(len);
63+
out_ptr = out_ptr.offset(1);
64+
});
65+
debug_assert_eq!(size, result.len());
66+
result
67+
}
68+

0 commit comments

Comments
 (0)