nonempty_containers/
macros.rs

1/// Creates a [NonEmptyVec] containing the arguments.
2///
3/// This macro is very similar in goal to the standard library's `vec!` macro:
4///
5/// - Create a [NonEmptyVec] containing a given list of elements:
6///
7/// ```
8/// # use nonempty_containers::ne;
9/// # 
10/// let ne = ne![1, 2, 3];
11/// assert_eq!(ne[0], 1);
12/// assert_eq!(ne[1], 2);
13/// assert_eq!(ne[2], 3);
14/// ```
15///
16/// - Create a [NonEmptyVec] from a given element and size:
17///
18/// ```
19/// # use nonempty_containers::{ne, NonEmptyVec};
20/// # 
21/// let ne = ne![1; 3];
22/// assert_eq!(ne, NonEmptyVec::from_vec(vec![1, 1, 1]).unwrap());
23/// ```
24///
25/// Note that unlike [Vec]s, it is not possible to create an empty [NonEmptyVec] using this macro!
26#[macro_export]
27macro_rules! ne {
28    ($elem:expr; $n:expr) => (
29        $crate::nonemptyvec::NonEmptyVec::__from_vec_unsafe(vec![$elem; $n])
30    );
31    ($single:expr) => (
32        $crate::nonemptyvec::NonEmptyVec::singleton($single)
33    );
34    ($head:expr, $($tail:expr),+ $(,)?) => (
35        $crate::nonemptyvec::NonEmptyVec::new($head, vec![$($tail),+])
36    );
37}