1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#![feature(try_from, type_ascription)]

use std::convert::TryFrom;

/// Non-empty list data type.
#[derive(Debug)]
pub struct NonEmpty<T>(T, Vec<T>);

impl<T: PartialEq> PartialEq for NonEmpty<T> {
/// Equality comparison.
fn eq(&self, other: &Self) -> bool {
self.0 == other.0 && self.1 == other.1
}
}

impl<T: Default> Default for NonEmpty<T> {
/// Default value.
fn default() -> Self {
NonEmpty(T::default(), Vec::new())
}
}

impl<T> Into<Vec<T>> for NonEmpty<T> {
/// Turns a non-empty list into a Vec.
fn into(mut self) -> Vec<T> {
let mut t = vec![self.0];
t.append(&mut self.1);
t
}
}

/// Error arisen when given an empty vector.
#[derive(Debug, PartialEq)]
pub struct EmptyVecError;

impl<T> TryFrom<Vec<T>> for NonEmpty<T> {
type Error = EmptyVecError;

/// Turns a vec into a non-empty list.
fn try_from(v: Vec<T>) -> Result<NonEmpty<T>, EmptyVecError> {
if v.is_empty() {
Err(EmptyVecError)
} else {
let mut it = v.into_iter();
if let Some(n) = it.next() {
Ok(NonEmpty(n, it.collect::<Vec<T>>()))
} else {
Err(EmptyVecError)
}
}
}
}

#[cfg(test)]
mod test {
use NonEmpty;
use EmptyVecError;
use std::convert::TryFrom;

#[test]
fn i() {
let i1 = NonEmpty(1, vec![2, 3]);
assert_eq!(i1.into(): Vec<i32>, vec![1, 2, 3]);
}

#[test]
fn f() {
let f1 = vec![1, 2, 3];
assert_eq!(NonEmpty::try_from(f1), Ok(NonEmpty(1, vec![2, 3])));

let f2: Vec<i32> = vec![];
assert_eq!(NonEmpty::try_from(f2), Err(EmptyVecError))
}
}