Skip to content

Commit d070650

Browse files
Implement vec! macro (#40)
A first version
1 parent 84b82cf commit d070650

File tree

2 files changed

+143
-1
lines changed

2 files changed

+143
-1
lines changed

src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
#![deny(missing_docs)]
33
#![doc = include_str!("../README.md")]
44

5+
#[macro_use]
6+
mod macros;
7+
58
use core::mem::MaybeUninit;
69
use core::{cmp, error, fmt, slice};
710

@@ -60,7 +63,7 @@ impl<T, const CAPACITY: usize> Vec<T, CAPACITY> {
6063
pub const fn new() -> Self {
6164
assert!(CAPACITY > 0, "CAPACITY must be greater than 0");
6265

63-
// SAFETY: The elements in the array are not accessed before beign initialized.
66+
// SAFETY: The elements in the array are not accessed before being initialized.
6467
let data = unsafe { MaybeUninit::<[MaybeUninit<T>; CAPACITY]>::uninit().assume_init() };
6568
Self { data, length: 0 }
6669
}

src/macros.rs

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#[macro_export]
2+
/// A macro for creating a static vector (`Vec`) with various initialization patterns.
3+
///
4+
/// # Usage
5+
///
6+
/// - `vec![Type; CAPACITY]`
7+
/// - Creates an empty static vector of the specified type and capacity.
8+
/// - Example: `vec![u32; 8]`
9+
///
10+
/// - `vec![value1, value2, ..., valueN]`
11+
/// - Creates a static vector with the given values, inferring the type and capacity from the values.
12+
/// - Example: `vec![1, 2, 3]`
13+
///
14+
/// - `vec![CAPACITY; value1, value2, ..., valueN]`
15+
/// - Creates a static vector with the specified capacity and initializes it with the given values.
16+
/// - Example: `vec![8; 1, 2, 3]`
17+
///
18+
/// - `vec![Type; CAPACITY; Length]`
19+
/// - Creates a static vector of the specified type and capacity, and sets its length to `Length`.
20+
/// - Example: `vec![u32; 8; 4]`
21+
///
22+
/// # Panics
23+
///
24+
/// Panics if the specified capacity is zero, or the number of provided values exceeds the capacity, or the requested length is greater than the capacity.
25+
///
26+
/// # Examples
27+
///
28+
/// ```rust
29+
/// use static_vector::vec;
30+
/// let vec = vec![u8; 4]; // Empty vector with capacity 4
31+
/// let vec = vec![1, 2, 3]; // Vector with 3 elements
32+
/// let vec = vec![4; 1, 2]; // Vector with capacity 4, initialized with 2 elements
33+
/// let vec = vec![u16; 8; 5]; // Vector with capacity 8, length set to 5, initialized with zeros
34+
/// ```
35+
macro_rules! vec {
36+
($type:ty; $capacity:literal) => {
37+
$crate::Vec::<$type, $capacity>::new()
38+
};
39+
40+
($($value:expr),+ $(,)?) => {
41+
{
42+
let mut vec = $crate::Vec::<_, { [$($value),+].len() }>::new();
43+
vec.extend_from_slice(&[$($value),+]).expect("length matches capacity");
44+
vec
45+
}
46+
};
47+
48+
($capacity:literal; $($value:expr),+ $(,)?) => {
49+
{
50+
let mut vec = $crate::Vec::<_, $capacity>::new();
51+
vec.extend_from_slice(&[$($value),+]).expect("length is less than or equal to capacity");
52+
vec
53+
}
54+
};
55+
56+
($type:ty; $capacity:literal; $length:literal) => {
57+
{
58+
let mut vec = $crate::Vec::<$type, $capacity>::new();
59+
vec.set_len($length).expect("length is less than or equal to capacity");
60+
vec
61+
}
62+
};
63+
}
64+
65+
#[cfg(test)]
66+
mod tests {
67+
#[test]
68+
fn vec_with_type_and_capacity() {
69+
let vec = vec![i32; 10];
70+
assert_eq!(vec.capacity(), 10);
71+
assert!(vec.is_empty());
72+
}
73+
74+
#[test]
75+
#[should_panic(expected = "CAPACITY must be greater than 0")]
76+
fn vec_with_type_and_capacity_zero() {
77+
let _ = vec![i32; 0];
78+
}
79+
80+
#[test]
81+
fn vec_with_one_element() {
82+
let vec = vec![999];
83+
assert_eq!(vec.capacity(), 1);
84+
assert_eq!(vec.len(), 1);
85+
assert_eq!(vec.as_slice(), &[999]);
86+
}
87+
88+
#[test]
89+
fn vec_with_elements() {
90+
let vec = vec![1, 2, 3];
91+
assert_eq!(vec.capacity(), 3);
92+
assert_eq!(vec.len(), 3);
93+
assert_eq!(vec.as_slice(), &[1, 2, 3]);
94+
}
95+
96+
#[test]
97+
fn vec_with_capacity_and_elements() {
98+
let vec = vec![10; 1, 2, 3];
99+
assert_eq!(vec.capacity(), 10);
100+
assert_eq!(vec.len(), 3);
101+
assert_eq!(vec.as_slice(), &[1, 2, 3]);
102+
}
103+
104+
#[test]
105+
#[should_panic(expected = "length is less than or equal to capacity: CapacityError")]
106+
fn vec_with_more_elements_than_capacity() {
107+
let _ = vec![2; 1, 2, 3];
108+
}
109+
110+
#[test]
111+
fn vec_with_capacity_and_length() {
112+
let vec = vec![i32; 10; 3];
113+
assert_eq!(vec.capacity(), 10);
114+
assert_eq!(vec.len(), 3);
115+
assert_eq!(vec.as_slice(), &[0, 0, 0]);
116+
}
117+
118+
#[test]
119+
fn vec_with_capacity_and_length_zero() {
120+
let vec = vec![i32; 10; 0];
121+
assert_eq!(vec.capacity(), 10);
122+
assert!(vec.is_empty());
123+
assert_eq!(vec.as_slice(), &[]);
124+
}
125+
126+
#[test]
127+
fn vec_with_capacity_and_length_equal_to_capacity() {
128+
let vec = vec![i32; 3; 3];
129+
assert_eq!(vec.capacity(), 3);
130+
assert_eq!(vec.len(), 3);
131+
assert_eq!(vec.as_slice(), &[0, 0, 0]);
132+
}
133+
134+
#[test]
135+
#[should_panic(expected = "length is less than or equal to capacity: CapacityError")]
136+
fn vec_with_capacity_and_length_greater_than_capacity() {
137+
let _ = vec![i32; 10; 30];
138+
}
139+
}

0 commit comments

Comments
 (0)