Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve return type of NonEmpty::split #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ impl<T> NonEmpty<T> {
/// Deconstruct a `NonEmpty` into its first, last, and
/// middle elements, in that order.
///
/// If there is only one element then first == last.
/// If there is only one element then last is `None`.
///
/// # Example Use
///
Expand All @@ -514,19 +514,20 @@ impl<T> NonEmpty<T> {
///
/// let mut non_empty = NonEmpty::from((1, vec![2, 3, 4, 5]));
///
/// // Guaranteed to have the last element and the elements
/// // preceding it.
/// assert_eq!(non_empty.split(), (&1, &[2, 3, 4][..], &5));
/// // When there are two or more elements, the last element is represented
/// // as a `Some`. Elements preceding it, except for the first, are returned
/// // in the middle.
/// 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));
/// // The last element is `None` when there's only one element.
/// 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 Expand Up @@ -1263,5 +1264,16 @@ mod tests {
);
Ok(())
}

#[test]
fn test_arbitrary_with_split() -> arbitrary::Result<()> {
let mut u = Unstructured::new(&[1, 2, 3, 4, 5, 6, 7, 8]);
let ne = NonEmpty::<i32>::arbitrary(&mut u)?;
let (head, middle, last) = ne.split();
let mut tail = middle.to_vec();
tail.extend(last);
assert_eq!(ne, NonEmpty { head: *head, tail });
Ok(())
}
}
}
Loading