Skip to content

Commit

Permalink
Improve return type of NonEmpty::split
Browse files Browse the repository at this point in the history
Fixes #60[^issue-60].

As pointed out in #60, splitting a `NonEmpty` that contains a single
element can be ambiguous to a `NonEmpty` equivalent to `(x, [x])`. A
better representation for this return type is `(&T, &[T],
Option<&T>)`.

[^issue-60]: #60
Signed-off-by: Fintan Halpenny <[email protected]>
X-Clacks-Overhead: GNU Terry Pratchett
  • Loading branch information
FintanH committed Nov 14, 2024
1 parent 4b255e7 commit df0381c
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,17 +516,17 @@ impl<T> NonEmpty<T> {
///
/// // Guaranteed to have the last element and the elements
/// // preceding it.
/// assert_eq!(non_empty.split(), (&1, &[2, 3, 4][..], &5));
/// assert_eq!(non_empty.split(), (&1, &[2, 3, 4][..], Some(&5)));
///
/// let non_empty = NonEmpty::new(1);
///
/// // Guaranteed to have the last element.
/// assert_eq!(non_empty.split(), (&1, &[][..], &1));
/// assert_eq!(non_empty.split(), (&1, &[][..], None));
/// ```
pub fn split(&self) -> (&T, &[T], &T) {
pub fn split(&self) -> (&T, &[T], Option<&T>) {
match self.tail.split_last() {
None => (&self.head, &[], &self.head),
Some((last, middle)) => (&self.head, middle, last),
None => (&self.head, &[], None),
Some((last, middle)) => (&self.head, middle, Some(last)),
}
}

Expand Down

0 comments on commit df0381c

Please sign in to comment.