usesuper::{ArrayLength, GenericArray}; use core::iter::FusedIterator; use core::mem::ManuallyDrop; use core::{cmp, fmt, mem, ptr};
/// An iterator that moves out of a `GenericArray` pubstruct GenericArrayIter<T, N: ArrayLength<T>> { // Invariants: index <= index_back <= N // Only values in array[index..index_back] are alive at any given time. // Values from array[..index] and array[index_back..] are already moved/dropped.
array: ManuallyDrop<GenericArray<T, N>>,
index: usize,
index_back: usize,
}
impl<T, N> GenericArrayIter<T, N> where
N: ArrayLength<T>,
{ /// Returns the remaining items of this iterator as a slice #[inline] pubfn as_slice(&self) -> &[T] {
&self.array.as_slice()[self.index..self.index_back]
}
/// Returns the remaining items of this iterator as a mutable slice #[inline] pubfn as_mut_slice(&mutself) -> &mut [T] {
&mutself.array.as_mut_slice()[self.index..self.index_back]
}
}
impl<T, N> IntoIterator for GenericArray<T, N> where
N: ArrayLength<T>,
{ type Item = T; type IntoIter = GenericArrayIter<T, N>;
// Based on work in rust-lang/rust#49000 impl<T: fmt::Debug, N> fmt::Debug for GenericArrayIter<T, N> where
N: ArrayLength<T>,
{ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("GenericArrayIter")
.field(&self.as_slice())
.finish()
}
}
impl<T, N> Drop for GenericArrayIter<T, N> where
N: ArrayLength<T>,
{ #[inline] fn drop(&mutself) { if mem::needs_drop::<T>() { // Drop values that are still alive. for p inself.as_mut_slice() { unsafe {
ptr::drop_in_place(p);
}
}
}
}
}
// Based on work in rust-lang/rust#49000 impl<T: Clone, N> Clone for GenericArrayIter<T, N> where
N: ArrayLength<T>,
{ fn clone(&self) -> Self { // This places all cloned elements at the start of the new array iterator, // not at their original indices.
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.