Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vec macros #438

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

N can be constructed in the macro, so this isn't needed.

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
Loading