Skip to content

Commit

Permalink
Add vec macros
Browse files Browse the repository at this point in the history
  • Loading branch information
YuhanLiin committed Jan 10, 2024
1 parent cbd406e commit 5e64217
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Added `String::from_utf16`.
- Added `is_full`, `recent_index`, `oldest`, and `oldest_index` to `HistoryBuffer`
- Added infallible conversions from arrays to `Vec`.
- Add `vec` and `vec_with_cap` macros, as well as `Vec::from_array_same_cap`.

### Changed

Expand Down
67 changes: 67 additions & 0 deletions src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ impl<T, const N: usize> Vec<T, N> {
}
}

/// Constructs a new vector with fixed capacity of `N`, initializing it with the provided
/// array.
///
/// The length of the provided array must be `N`.
pub fn from_array_same_cap(src: [T; N]) -> Self {
Self::from_array::<N>(src)
}

/// Clones a vec into a new vec
pub(crate) fn clone(&self) -> Self
where
Expand Down Expand Up @@ -1227,6 +1235,65 @@ where
}
}

/// Shorthand for defining vectors using array syntax.
///
/// Create `Vec` with a list of elements
/// ```
/// let v = heapless::vec![1, 2, 3];
/// assert_eq!(v.as_slice(), &[1, 2, 3]);
/// assert_eq!(v.capacity(), 3);
/// ```
///
/// Create `Vec` with a repeated element and length
/// ```
/// let v = heapless::vec!['a'; 3];
/// assert_eq!(v.as_slice(), &['a', 'a', 'a']);
/// assert_eq!(v.capacity(), 3);
/// ```
///
/// Unlike the `std` version of this macro, the repeat element must be `Copy` or a constant, like
/// repeat elements in array expressions.
#[macro_export]
macro_rules! vec {
($($elem:expr),+) => {
heapless::Vec::from_array_same_cap([$($elem),+])
};

($elem:expr ; $len:expr) => {
heapless::Vec::from_array_same_cap([$elem; $len])
};
}

/// Shorthand for defining vectors using array syntax with an additional capacity argument.
///
/// Create `Vec` with a list of elements and capacity
/// ```
/// let v = heapless::vec_with_cap!([1, 2, 3]; 5);
/// assert_eq!(v.as_slice(), &[1, 2, 3]);
/// assert_eq!(v.capacity(), 5);
/// ```
///
/// Create `Vec` with a repeated element, length, and capacity
/// ```
/// let v = heapless::vec_with_cap!(['a'; 3]; 6);
/// assert_eq!(v.as_slice(), &['a', 'a', 'a']);
/// assert_eq!(v.capacity(), 6);
/// ```
///
/// The capacity must be greater than or equal to the length of the `Vec`, otherwise there will be
/// a compile error. Also, the repeat element must be `Copy` or a constant, like repeat elements in
/// array expressions.
#[macro_export]
macro_rules! vec_with_cap {
([$($elem:expr),+] ; $cap:expr) => {
heapless::Vec::<_, $cap>::from_array([$($elem),+])
};

([$elem:expr ; $len:expr]; $cap:expr) => {
heapless::Vec::<_, $cap>::from_array([$elem; $len])
};
}

#[cfg(test)]
mod tests {
use crate::Vec;
Expand Down

0 comments on commit 5e64217

Please sign in to comment.