Skip to content

Commit

Permalink
Make next_cycle and previous_cycle not return Options
Browse files Browse the repository at this point in the history
  • Loading branch information
fenhl committed Feb 25, 2024
1 parent 0a3fbd0 commit 8efd9c9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion enum-iterator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "enum-iterator"
version = "1.5.0"
version = "2.0.0"
authors = ["Stephane Raux <stephaneyfx@gmail.com>"]
edition = "2021"
description = "Tools to iterate over all values of a type (e.g. all variants of an enumeration)"
Expand Down
15 changes: 9 additions & 6 deletions enum-iterator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,10 @@ pub fn next<T: Sequence>(x: &T) -> Option<T> {
/// #[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<T: Sequence>(x: &T) -> Option<T> {
next(x).or_else(first)
pub fn next_cycle<T: Sequence>(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.
Expand Down Expand Up @@ -180,10 +180,10 @@ pub fn previous<T: Sequence>(x: &T) -> Option<T> {
/// #[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<T: Sequence>(x: &T) -> Option<T> {
previous(x).or_else(last)
pub fn previous_cycle<T: Sequence>(x: &T) -> T {
previous(x).or_else(last).expect("Sequence::last returned None for inhabited type")
}

/// Returns the first value of type `T`.
Expand Down Expand Up @@ -284,6 +284,9 @@ impl<T: Sequence> FusedIterator for ReverseAll<T> {}
/// - `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
Expand Down

0 comments on commit 8efd9c9

Please sign in to comment.