diff --git a/enum-iterator/Cargo.toml b/enum-iterator/Cargo.toml index 18446d1..7c1f1ed 100644 --- a/enum-iterator/Cargo.toml +++ b/enum-iterator/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "enum-iterator" -version = "1.5.0" +version = "2.0.0" authors = ["Stephane Raux "] edition = "2021" description = "Tools to iterate over all values of a type (e.g. all variants of an enumeration)" diff --git a/enum-iterator/src/lib.rs b/enum-iterator/src/lib.rs index bff3485..7082bcb 100644 --- a/enum-iterator/src/lib.rs +++ b/enum-iterator/src/lib.rs @@ -148,10 +148,10 @@ pub fn next(x: &T) -> Option { /// #[derive(Debug, PartialEq, Sequence)] /// enum Day { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } /// -/// assert_eq!(next_cycle(&Day::Sunday), Some(Day::Monday)); +/// assert_eq!(next_cycle(&Day::Sunday), Day::Monday); /// ``` -pub fn next_cycle(x: &T) -> Option { - next(x).or_else(first) +pub fn next_cycle(x: &T) -> T { + next(x).or_else(first).expect("Sequence::first returned None for inhabited type") } /// Returns the previous value of type `T` or `None` if this was the beginning. @@ -180,10 +180,10 @@ pub fn previous(x: &T) -> Option { /// #[derive(Debug, PartialEq, Sequence)] /// enum Day { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } /// -/// assert_eq!(previous_cycle(&Day::Monday), Some(Day::Sunday)); +/// assert_eq!(previous_cycle(&Day::Monday), Day::Sunday); /// ``` -pub fn previous_cycle(x: &T) -> Option { - previous(x).or_else(last) +pub fn previous_cycle(x: &T) -> T { + previous(x).or_else(last).expect("Sequence::last returned None for inhabited type") } /// Returns the first value of type `T`. @@ -284,6 +284,9 @@ impl FusedIterator for ReverseAll {} /// - `T::last().and_then(|x| x.next()).is_none()` /// - `T::first().is_none()` ⇔ `T::last().is_none()` /// - `std::iter::successors(T::first(), T::next)` must eventually yield `T::last()`. +/// - If `T` is inhabited, `T::first().is_some()`. +/// +/// If a manual implementation of `Sequence` violates any of these laws, the functions at the crate root may misbehave, including panicking. /// /// # Examples /// ## C-like enumeration