Skip to content

Refactor: tidy up imports and features #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ version: 2
jobs:
stable:
docker:
- image: rust:1.33.0
- image: rust:1.36.0
environment:
RUSTFLAGS: -D warnings
working_directory: ~/build
Expand All @@ -34,7 +34,6 @@ jobs:
- run: rustc --version > ~/rust-version
- *RESTORE_DEPS
- run: cargo test
- run: cargo test --features std
- *SAVE_DEPS
nightly:
docker:
Expand All @@ -49,7 +48,7 @@ jobs:
- *SAVE_REGISTRY
- run: rustc --version > ~/rust-version
- *RESTORE_DEPS
- run: cargo test --features alloc
- run: cargo test
- *SAVE_DEPS

workflows:
Expand Down
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ readme = "README.md"
categories = ["algorithms", "no-std"]

[features]
default = ["alloc"]
alloc = []
std = []
default = ["std"]
47 changes: 14 additions & 33 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,37 +67,18 @@
#![warn(missing_docs)]
#![no_std]

use core::cmp::{self, Ordering};
use core::iter;
use core::{
cmp::{self, Ordering},
iter,
};

#[cfg(all(feature = "alloc", not(feature = "std")))]
#[cfg_attr(test, macro_use)]
#[cfg(feature = "alloc")]
extern crate alloc;

#[cfg(all(feature = "alloc", not(feature = "std")))]
mod imports {
pub use alloc::boxed::Box;
pub use alloc::collections::btree_map::BTreeMap;
pub use alloc::collections::btree_set::BTreeSet;
pub use alloc::vec::Vec;
}

#[cfg(feature = "std")]
#[cfg_attr(test, macro_use)]
extern crate std;

#[cfg(feature = "std")]
mod imports {
pub use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
pub use std::hash::{BuildHasher, Hash};
pub use std::prelude::v1::*;
}

#[cfg(any(feature = "std", feature = "alloc"))]
use crate::imports::*;
#[cfg(feature = "alloc")]
use alloc::boxed::Box;

#[cfg(any(feature = "std", feature = "alloc"))]
#[cfg(test)]
#[cfg(all(test, feature = "alloc"))]
mod test;

enum FoldStop<T, E> {
Expand Down Expand Up @@ -244,7 +225,7 @@ pub trait FallibleIterator {
Self: Sized,
F: FnMut(Self::Item) -> Result<B, Self::Error>,
{
Map { it: self, f: f }
Map { it: self, f }
}

/// Calls a fallible closure on each element of an iterator.
Expand All @@ -266,7 +247,7 @@ pub trait FallibleIterator {
Self: Sized,
F: FnMut(&Self::Item) -> Result<bool, Self::Error>,
{
Filter { it: self, f: f }
Filter { it: self, f }
}

/// Returns an iterator which both filters and maps. The closure may fail;
Expand All @@ -277,7 +258,7 @@ pub trait FallibleIterator {
Self: Sized,
F: FnMut(Self::Item) -> Result<Option<B>, Self::Error>,
{
FilterMap { it: self, f: f }
FilterMap { it: self, f }
}

/// Returns an iterator which yields the current iteration count as well
Expand Down Expand Up @@ -959,7 +940,7 @@ pub trait FallibleIterator {
F: FnMut(Self::Error) -> B,
Self: Sized,
{
MapErr { it: self, f: f }
MapErr { it: self, f }
}

/// Returns an iterator which unwraps all of its elements.
Expand Down Expand Up @@ -1000,7 +981,7 @@ impl<I: DoubleEndedFallibleIterator + ?Sized> DoubleEndedFallibleIterator for &m
}
}

#[cfg(any(feature = "std", feature = "alloc"))]
#[cfg(feature = "alloc")]
impl<I: FallibleIterator + ?Sized> FallibleIterator for Box<I> {
type Item = I::Item;
type Error = I::Error;
Expand All @@ -1021,7 +1002,7 @@ impl<I: FallibleIterator + ?Sized> FallibleIterator for Box<I> {
}
}

#[cfg(any(feature = "std", feature = "alloc"))]
#[cfg(feature = "alloc")]
impl<I: DoubleEndedFallibleIterator + ?Sized> DoubleEndedFallibleIterator for Box<I> {
#[inline]
fn next_back(&mut self) -> Result<Option<I::Item>, I::Error> {
Expand Down
33 changes: 15 additions & 18 deletions src/test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::iter;
use core::ops::Range;
use alloc::{vec, vec::Vec};
use core::{iter, ops::Range};

use super::{convert, FallibleIterator, Vec};
use super::{convert, FallibleIterator};

#[test]
fn all() {
Expand Down Expand Up @@ -241,8 +241,7 @@ fn max_by_key() {
// Exercise failure both on the first item, and later.
assert_eq!(it.clone().max_by_key(|&i| Err::<i32, _>(i)), Err(0));
assert_eq!(
it.clone()
.max_by_key(|&i| if i > 0 { Err(i) } else { Ok(-i) }),
it.max_by_key(|&i| if i > 0 { Err(i) } else { Ok(-i) }),
Err(3)
);
}
Expand All @@ -266,8 +265,7 @@ fn min_by_key() {
// Exercise failure both on the first item, and later.
assert_eq!(it.clone().min_by_key(|&i| Err::<i32, _>(i)), Err(0));
assert_eq!(
it.clone()
.min_by_key(|&i| if i > 0 { Err(i) } else { Ok(-i) }),
it.min_by_key(|&i| if i > 0 { Err(i) } else { Ok(-i) }),
Err(3)
);
}
Expand Down Expand Up @@ -304,15 +302,14 @@ fn position() {
assert_eq!(it.position(|n| Ok(n == 3)).unwrap(), Some(0));
assert_eq!(it.position(|n| Ok(n == 5)).unwrap(), None);

let it = convert(vec![1, 2, 3, 4].into_iter().map(Ok::<i32, i32>));
let mut it = convert(vec![1, 2, 3, 4].into_iter().map(Ok::<i32, i32>));
assert_eq!(
it.clone()
.position(|n| if n == 3 { Err(42) } else { Ok(n == 2) }),
Ok(Some(1))
);
assert_eq!(
it.clone()
.position(|n| if n == 3 { Err(42) } else { Ok(n == 4) }),
it.position(|n| if n == 3 { Err(42) } else { Ok(n == 4) }),
Err(42)
);
}
Expand All @@ -335,7 +332,7 @@ fn skip() {
let it = convert(vec![1, 2, 3, 4].into_iter().map(Ok::<i32, ()>));
assert_eq!(it.clone().skip(0).collect::<Vec<_>>(), Ok(vec![1, 2, 3, 4]));
assert_eq!(it.clone().skip(2).collect::<Vec<_>>(), Ok(vec![3, 4]));
assert_eq!(it.clone().skip(4).collect::<Vec<_>>(), Ok(vec![]));
assert_eq!(it.skip(4).collect::<Vec<_>>(), Ok(vec![]));
}

#[test]
Expand All @@ -350,7 +347,7 @@ fn skip_while() {
Ok(vec![3, 4, 1])
);
assert_eq!(
it.clone().skip_while(|x| Ok(*x < 5)).collect::<Vec<_>>(),
it.skip_while(|x| Ok(*x < 5)).collect::<Vec<_>>(),
Ok(vec![])
);
}
Expand Down Expand Up @@ -384,7 +381,7 @@ fn take_while() {
Ok(vec![0, 1])
);
assert_eq!(
it.clone().take_while(|x| Ok(*x < 4)).collect::<Vec<_>>(),
it.take_while(|x| Ok(*x < 4)).collect::<Vec<_>>(),
Ok(vec![0, 1, 2, 3, 0])
);
}
Expand All @@ -411,7 +408,10 @@ fn flatten() {
#[test]
fn inspect() {
let mut buf = vec![];
let it = convert(vec![0, 1, 2, 3].into_iter().map(Ok::<i32, ()>)).inspect(|v| Ok(buf.push(*v)));
let it = convert(vec![0, 1, 2, 3].into_iter().map(Ok::<i32, ()>)).inspect(|&v| {
buf.push(v);
Ok(())
});
it.count().unwrap();
assert_eq!(buf, vec![0, 1, 2, 3]);
}
Expand Down Expand Up @@ -451,10 +451,7 @@ fn unzip() {
#[test]
fn cycle() {
let it = convert(vec![0, 1, 2, 3].into_iter().map(Ok::<i32, ()>)).cycle();
assert_eq!(
it.take(6).clone().collect::<Vec<_>>(),
Ok(vec![0, 1, 2, 3, 0, 1])
);
assert_eq!(it.take(6).collect::<Vec<_>>(), Ok(vec![0, 1, 2, 3, 0, 1]));
}

#[test]
Expand Down