Skip to content

Commit

Permalink
Adds the const_generics feature
Browse files Browse the repository at this point in the history
  • Loading branch information
c410-f3r committed Jun 16, 2019
1 parent 85440ce commit 776ce5c
Show file tree
Hide file tree
Showing 5 changed files with 394 additions and 217 deletions.
40 changes: 31 additions & 9 deletions src/into_iter.rs
Expand Up @@ -3,28 +3,39 @@ use crate::Array;
use crate::SmallVec;
use core::ptr;

macro_rules! create_with_parts {
(
<$($({$s_impl_ty_prefix:ident})? $s_impl_ty:ident$(: $s_impl_ty_bound:ident)?),*>,
<$s_decl_ty:ident$(, {$s_decl_const_ty:ident})?>,
$array_item:ty
) => {

/// An iterator that consumes a `SmallVec` and yields its items by value.
///
/// Returned from [`SmallVec::into_iter`][1].
///
/// [1]: struct.SmallVec.html#method.into_iter
pub struct IntoIter<A: Array> {
pub(crate) data: SmallVec<A>,
pub struct IntoIter<$($($s_impl_ty_prefix)? $s_impl_ty$(: $s_impl_ty_bound)?),*> {
pub(crate) data: SmallVec<$s_decl_ty$(, {$s_decl_const_ty})?>,
pub(crate) current: usize,
pub(crate) end: usize,
}

impl<A: Array> Drop for IntoIter<A> {
impl<$($($s_impl_ty_prefix)? $s_impl_ty$(: $s_impl_ty_bound)?),*> Drop
for IntoIter<$s_decl_ty$(, {$s_decl_const_ty})?>
{
fn drop(&mut self) {
for _ in self {}
}
}

impl<A: Array> Iterator for IntoIter<A> {
type Item = A::Item;
impl<$($($s_impl_ty_prefix)? $s_impl_ty$(: $s_impl_ty_bound)?),*> Iterator
for IntoIter<$s_decl_ty$(, {$s_decl_const_ty})?>
{
type Item = $array_item;

#[inline]
fn next(&mut self) -> Option<A::Item> {
fn next(&mut self) -> Option<$array_item> {
if self.current == self.end {
None
} else {
Expand All @@ -43,9 +54,11 @@ impl<A: Array> Iterator for IntoIter<A> {
}
}

impl<A: Array> DoubleEndedIterator for IntoIter<A> {
impl<$($($s_impl_ty_prefix)? $s_impl_ty$(: $s_impl_ty_bound)?),*> DoubleEndedIterator
for IntoIter<$s_decl_ty$(, {$s_decl_const_ty})?>
{
#[inline]
fn next_back(&mut self) -> Option<A::Item> {
fn next_back(&mut self) -> Option<$array_item> {
if self.current == self.end {
None
} else {
Expand All @@ -57,4 +70,13 @@ impl<A: Array> DoubleEndedIterator for IntoIter<A> {
}
}

impl<A: Array> ExactSizeIterator for IntoIter<A> {}
impl<$($($s_impl_ty_prefix)? $s_impl_ty$(: $s_impl_ty_bound)?),*> ExactSizeIterator
for IntoIter<$s_decl_ty$(, {$s_decl_const_ty})?> {}

}
}

#[cfg(feature = "const_generics")]
create_with_parts!(<T, {const} N: usize>, <T, {N}>, T);
#[cfg(not(feature = "const_generics"))]
create_with_parts!(<A: Array>, <A>, A::Item);

0 comments on commit 776ce5c

Please sign in to comment.