From 5bc90bd179441581ddf93b0e21a6c97600537750 Mon Sep 17 00:00:00 2001 From: Basti Ortiz <39114273+Some-Dood@users.noreply.github.com> Date: Sun, 14 Aug 2022 20:31:23 +0800 Subject: [PATCH 1/4] Refactor: tidy up imports and features --- Cargo.toml | 3 +-- src/lib.rs | 43 ++++++++++++++----------------------------- src/test.rs | 4 ++-- 3 files changed, 17 insertions(+), 33 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e8fd7e9..9f7a617 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,5 @@ readme = "README.md" categories = ["algorithms", "no-std"] [features] +default = ["alloc"] alloc = [] -std = [] -default = ["std"] diff --git a/src/lib.rs b/src/lib.rs index 396c8fb..160ac89 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -67,37 +67,22 @@ #![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")] +pub use alloc::{ + boxed::Box, + collections::{BTreeMap, BTreeSet}, + vec::Vec, +}; -#[cfg(any(feature = "std", feature = "alloc"))] -#[cfg(test)] +#[cfg(all(test, feature = "alloc"))] mod test; enum FoldStop { @@ -1000,7 +985,7 @@ impl DoubleEndedFallibleIterator for &m } } -#[cfg(any(feature = "std", feature = "alloc"))] +#[cfg(feature = "alloc")] impl FallibleIterator for Box { type Item = I::Item; type Error = I::Error; @@ -1021,7 +1006,7 @@ impl FallibleIterator for Box { } } -#[cfg(any(feature = "std", feature = "alloc"))] +#[cfg(feature = "alloc")] impl DoubleEndedFallibleIterator for Box { #[inline] fn next_back(&mut self) -> Result, I::Error> { diff --git a/src/test.rs b/src/test.rs index de4a8a6..682f755 100644 --- a/src/test.rs +++ b/src/test.rs @@ -1,5 +1,5 @@ -use core::iter; -use core::ops::Range; +use alloc::vec; +use core::{iter, ops::Range}; use super::{convert, FallibleIterator, Vec}; From 9e6d93186a628c188c33f16650d4df346081cd67 Mon Sep 17 00:00:00 2001 From: Basti Ortiz <39114273+Some-Dood@users.noreply.github.com> Date: Sun, 14 Aug 2022 20:47:30 +0800 Subject: [PATCH 2/4] Fix: address various Clippy lints --- src/lib.rs | 14 +++++--------- src/test.rs | 31 ++++++++++++++----------------- 2 files changed, 19 insertions(+), 26 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 160ac89..73acc59 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -76,11 +76,7 @@ use core::{ extern crate alloc; #[cfg(feature = "alloc")] -pub use alloc::{ - boxed::Box, - collections::{BTreeMap, BTreeSet}, - vec::Vec, -}; +use alloc::boxed::Box; #[cfg(all(test, feature = "alloc"))] mod test; @@ -229,7 +225,7 @@ pub trait FallibleIterator { Self: Sized, F: FnMut(Self::Item) -> Result, { - Map { it: self, f: f } + Map { it: self, f } } /// Calls a fallible closure on each element of an iterator. @@ -251,7 +247,7 @@ pub trait FallibleIterator { Self: Sized, F: FnMut(&Self::Item) -> Result, { - Filter { it: self, f: f } + Filter { it: self, f } } /// Returns an iterator which both filters and maps. The closure may fail; @@ -262,7 +258,7 @@ pub trait FallibleIterator { Self: Sized, F: FnMut(Self::Item) -> Result, Self::Error>, { - FilterMap { it: self, f: f } + FilterMap { it: self, f } } /// Returns an iterator which yields the current iteration count as well @@ -944,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. diff --git a/src/test.rs b/src/test.rs index 682f755..0c56007 100644 --- a/src/test.rs +++ b/src/test.rs @@ -1,7 +1,7 @@ -use alloc::vec; +use alloc::{vec, vec::Vec}; use core::{iter, ops::Range}; -use super::{convert, FallibleIterator, Vec}; +use super::{convert, FallibleIterator}; #[test] fn all() { @@ -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::(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) ); } @@ -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::(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) ); } @@ -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::)); + let mut it = convert(vec![1, 2, 3, 4].into_iter().map(Ok::)); 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) ); } @@ -335,7 +332,7 @@ fn skip() { let it = convert(vec![1, 2, 3, 4].into_iter().map(Ok::)); assert_eq!(it.clone().skip(0).collect::>(), Ok(vec![1, 2, 3, 4])); assert_eq!(it.clone().skip(2).collect::>(), Ok(vec![3, 4])); - assert_eq!(it.clone().skip(4).collect::>(), Ok(vec![])); + assert_eq!(it.skip(4).collect::>(), Ok(vec![])); } #[test] @@ -350,7 +347,7 @@ fn skip_while() { Ok(vec![3, 4, 1]) ); assert_eq!( - it.clone().skip_while(|x| Ok(*x < 5)).collect::>(), + it.skip_while(|x| Ok(*x < 5)).collect::>(), Ok(vec![]) ); } @@ -384,7 +381,7 @@ fn take_while() { Ok(vec![0, 1]) ); assert_eq!( - it.clone().take_while(|x| Ok(*x < 4)).collect::>(), + it.take_while(|x| Ok(*x < 4)).collect::>(), Ok(vec![0, 1, 2, 3, 0]) ); } @@ -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::)).inspect(|v| Ok(buf.push(*v))); + let it = convert(vec![0, 1, 2, 3].into_iter().map(Ok::)).inspect(|&v| { + buf.push(v); + Ok(()) + }); it.count().unwrap(); assert_eq!(buf, vec![0, 1, 2, 3]); } @@ -451,10 +451,7 @@ fn unzip() { #[test] fn cycle() { let it = convert(vec![0, 1, 2, 3].into_iter().map(Ok::)).cycle(); - assert_eq!( - it.take(6).clone().collect::>(), - Ok(vec![0, 1, 2, 3, 0, 1]) - ); + assert_eq!(it.take(6).collect::>(), Ok(vec![0, 1, 2, 3, 0, 1])); } #[test] From 3e2e0284e353e720ca04f3ae92232c54fb12f241 Mon Sep 17 00:00:00 2001 From: Basti Ortiz <39114273+Some-Dood@users.noreply.github.com> Date: Sun, 14 Aug 2022 21:36:51 +0800 Subject: [PATCH 3/4] Chore: bump MSRV to `1.36` --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index cf9d2e4..715f57c 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -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 From bfb2c687974b067db65eccf51c9b07f27ee8bc5f Mon Sep 17 00:00:00 2001 From: Basti Ortiz <39114273+Some-Dood@users.noreply.github.com> Date: Sun, 14 Aug 2022 21:38:58 +0800 Subject: [PATCH 4/4] Fix: remove `--features std` invocation --- .circleci/config.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 715f57c..5012bda 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -34,7 +34,6 @@ jobs: - run: rustc --version > ~/rust-version - *RESTORE_DEPS - run: cargo test - - run: cargo test --features std - *SAVE_DEPS nightly: docker: @@ -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: