Skip to content

Commit 8efd9c9

Browse files
committed
Make next_cycle and previous_cycle not return Options
1 parent 0a3fbd0 commit 8efd9c9

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

enum-iterator/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "enum-iterator"
3-
version = "1.5.0"
3+
version = "2.0.0"
44
authors = ["Stephane Raux <[email protected]>"]
55
edition = "2021"
66
description = "Tools to iterate over all values of a type (e.g. all variants of an enumeration)"

enum-iterator/src/lib.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,10 @@ pub fn next<T: Sequence>(x: &T) -> Option<T> {
148148
/// #[derive(Debug, PartialEq, Sequence)]
149149
/// enum Day { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }
150150
///
151-
/// assert_eq!(next_cycle(&Day::Sunday), Some(Day::Monday));
151+
/// assert_eq!(next_cycle(&Day::Sunday), Day::Monday);
152152
/// ```
153-
pub fn next_cycle<T: Sequence>(x: &T) -> Option<T> {
154-
next(x).or_else(first)
153+
pub fn next_cycle<T: Sequence>(x: &T) -> T {
154+
next(x).or_else(first).expect("Sequence::first returned None for inhabited type")
155155
}
156156

157157
/// Returns the previous value of type `T` or `None` if this was the beginning.
@@ -180,10 +180,10 @@ pub fn previous<T: Sequence>(x: &T) -> Option<T> {
180180
/// #[derive(Debug, PartialEq, Sequence)]
181181
/// enum Day { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }
182182
///
183-
/// assert_eq!(previous_cycle(&Day::Monday), Some(Day::Sunday));
183+
/// assert_eq!(previous_cycle(&Day::Monday), Day::Sunday);
184184
/// ```
185-
pub fn previous_cycle<T: Sequence>(x: &T) -> Option<T> {
186-
previous(x).or_else(last)
185+
pub fn previous_cycle<T: Sequence>(x: &T) -> T {
186+
previous(x).or_else(last).expect("Sequence::last returned None for inhabited type")
187187
}
188188

189189
/// Returns the first value of type `T`.
@@ -284,6 +284,9 @@ impl<T: Sequence> FusedIterator for ReverseAll<T> {}
284284
/// - `T::last().and_then(|x| x.next()).is_none()`
285285
/// - `T::first().is_none()` ⇔ `T::last().is_none()`
286286
/// - `std::iter::successors(T::first(), T::next)` must eventually yield `T::last()`.
287+
/// - If `T` is inhabited, `T::first().is_some()`.
288+
///
289+
/// If a manual implementation of `Sequence` violates any of these laws, the functions at the crate root may misbehave, including panicking.
287290
///
288291
/// # Examples
289292
/// ## C-like enumeration

0 commit comments

Comments
 (0)