1 | /// Creates a [`TiVec`] containing the arguments. |
2 | /// |
3 | /// `ti_vec!` allows `TiVec`s to be defined with the same syntax as array |
4 | /// expressions. There are two forms of this macro: |
5 | /// |
6 | /// - Create a [`TiVec`] containing a given list of elements: |
7 | /// |
8 | /// ``` |
9 | /// use derive_more::{From, Into}; |
10 | /// use typed_index_collections::{ti_vec, TiVec}; |
11 | /// |
12 | /// #[derive(From, Into, Debug)] |
13 | /// struct FooId(usize); |
14 | /// |
15 | /// let v: TiVec<FooId, usize> = ti_vec![1, 2, 3]; |
16 | /// assert_eq!(v[FooId(0)], 1); |
17 | /// assert_eq!(v[FooId(1)], 2); |
18 | /// assert_eq!(v[FooId(2)], 3); |
19 | /// ``` |
20 | /// |
21 | /// - Create a [`TiVec`] from a given element and size: |
22 | /// |
23 | /// ``` |
24 | /// use typed_index_collections::{ti_vec, TiVec}; |
25 | /// use derive_more::{From, Into}; |
26 | /// |
27 | /// #[derive(From, Into, Debug)] |
28 | /// struct FooId(usize); |
29 | /// |
30 | /// let v: TiVec<FooId, usize> = ti_vec![1; 3]; |
31 | /// assert_eq!(v.as_ref(), [1, 1, 1]); |
32 | /// ``` |
33 | /// |
34 | /// Note that unlike array expressions this syntax supports all elements |
35 | /// which implement [`Clone`] and the number of elements doesn't have to be |
36 | /// a constant. |
37 | /// |
38 | /// This will use `clone` to duplicate an expression, so one should be careful |
39 | /// using this with types having a nonstandard `Clone` implementation. For |
40 | /// example, `ti_vec![Rc::new(1); 5]` will create a vector of five references |
41 | /// to the same boxed integer value, not five references pointing to |
42 | /// independently boxed integers. |
43 | /// |
44 | /// Also, note that `ti_vec![expr; 0]` is allowed, and produces an empty vector. |
45 | /// This will still evaluate `expr`, however, and immediately drop the resulting |
46 | /// value, so be mindful of side effects. |
47 | /// |
48 | /// [`TiVec`]: crate::TiVec |
49 | #[macro_export ] |
50 | macro_rules! ti_vec { |
51 | () => ( |
52 | $crate::TiVec::new() |
53 | ); |
54 | ($elem:expr; $n:expr) => ( |
55 | $crate::TiVec::from($crate::macro_deps::vec![$elem; $n]) |
56 | ); |
57 | ($($x:expr),+ $(,)?) => ( |
58 | $crate::TiVec::from($crate::macro_deps::vec![$($x),+]) |
59 | ); |
60 | } |
61 | |