Skip to content

Commit 929b68a

Browse files
committed
Implement Sequence for Ordering and Poll
1 parent 19e86f7 commit 929b68a

File tree

1 file changed

+101
-2
lines changed

1 file changed

+101
-2
lines changed

enum-iterator/src/lib.rs

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
#![deny(warnings)]
6565
#![no_std]
6666

67-
use core::{iter::FusedIterator, ops::ControlFlow};
67+
use core::{cmp::Ordering, iter::FusedIterator, ops::ControlFlow, task::Poll};
6868

6969
pub use enum_iterator_derive::Sequence;
7070

@@ -538,6 +538,35 @@ impl Sequence for core::convert::Infallible {
538538
}
539539
}
540540

541+
impl Sequence for Ordering {
542+
const CARDINALITY: usize = 3;
543+
544+
fn next(&self) -> Option<Self> {
545+
int_to_ordering(*self as i8 + 1)
546+
}
547+
548+
fn previous(&self) -> Option<Self> {
549+
int_to_ordering(*self as i8 - 1)
550+
}
551+
552+
fn first() -> Option<Self> {
553+
Some(Ordering::Less)
554+
}
555+
556+
fn last() -> Option<Self> {
557+
Some(Ordering::Greater)
558+
}
559+
}
560+
561+
fn int_to_ordering(i: i8) -> Option<Ordering> {
562+
match i {
563+
-1 => Some(Ordering::Less),
564+
0 => Some(Ordering::Equal),
565+
1 => Some(Ordering::Greater),
566+
_ => None,
567+
}
568+
}
569+
541570
impl<T: Sequence> Sequence for Option<T> {
542571
const CARDINALITY: usize = T::CARDINALITY + 1;
543572

@@ -561,6 +590,32 @@ impl<T: Sequence> Sequence for Option<T> {
561590
}
562591
}
563592

593+
impl<T: Sequence> Sequence for Poll<T> {
594+
const CARDINALITY: usize = T::CARDINALITY + 1;
595+
596+
fn next(&self) -> Option<Self> {
597+
match self {
598+
Poll::Ready(x) => x.next().map(Poll::Ready).or(Some(Poll::Pending)),
599+
Poll::Pending => None,
600+
}
601+
}
602+
603+
fn previous(&self) -> Option<Self> {
604+
match self {
605+
Poll::Ready(x) => x.previous().map(Poll::Ready),
606+
Poll::Pending => T::last().map(Poll::Ready),
607+
}
608+
}
609+
610+
fn first() -> Option<Self> {
611+
T::first().map(Poll::Ready).or(Some(Poll::Pending))
612+
}
613+
614+
fn last() -> Option<Self> {
615+
Some(Poll::Pending)
616+
}
617+
}
618+
564619
impl<const N: usize, T: Sequence + Clone> Sequence for [T; N] {
565620
const CARDINALITY: usize = {
566621
let tc = T::CARDINALITY;
@@ -728,7 +783,7 @@ impl_sequence_for_tuples!(
728783
#[cfg(test)]
729784
mod tests {
730785
use crate::{all, cardinality, reverse_all, Sequence};
731-
use core::convert::Infallible;
786+
use core::{cmp::Ordering, convert::Infallible, task::Poll};
732787

733788
fn cardinality_equals_item_count<T: Sequence>() {
734789
assert_eq!(cardinality::<T>(), all::<T>().count());
@@ -879,6 +934,50 @@ mod tests {
879934
assert!(all::<Option<bool>>().eq([None, Some(false), Some(true)]));
880935
}
881936

937+
#[test]
938+
fn cardinality_equals_item_count_for_ordering() {
939+
cardinality_equals_item_count::<Ordering>();
940+
}
941+
942+
#[test]
943+
fn all_ordering_values_are_yielded() {
944+
assert!(all::<Ordering>().eq([Ordering::Less, Ordering::Equal, Ordering::Greater]));
945+
}
946+
947+
#[test]
948+
fn all_ordering_values_are_yielded_in_reverse() {
949+
assert!(reverse_all::<Ordering>().eq([Ordering::Greater, Ordering::Equal, Ordering::Less]));
950+
}
951+
952+
#[test]
953+
fn cardinality_equals_item_count_for_poll() {
954+
cardinality_equals_item_count::<Poll<u8>>();
955+
}
956+
957+
#[test]
958+
fn all_bool_poll_items_are_yielded() {
959+
assert!(all::<Poll<bool>>().eq([Poll::Ready(false), Poll::Ready(true), Poll::Pending]));
960+
}
961+
962+
#[test]
963+
fn all_bool_poll_items_are_yielded_in_reverse() {
964+
assert!(reverse_all::<Poll<bool>>().eq([
965+
Poll::Pending,
966+
Poll::Ready(true),
967+
Poll::Ready(false),
968+
]));
969+
}
970+
971+
#[test]
972+
fn all_infallible_poll_items_are_yielded() {
973+
assert!(all::<Poll<Infallible>>().eq([Poll::Pending]));
974+
}
975+
976+
#[test]
977+
fn all_infallible_poll_items_are_yielded_in_reverse() {
978+
assert!(reverse_all::<Poll<Infallible>>().eq([Poll::Pending]));
979+
}
980+
882981
#[test]
883982
fn tuple_fields_vary_from_right_to_left() {
884983
assert!(all::<(Option<bool>, bool)>().eq([

0 commit comments

Comments
 (0)