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 to ArrayVec a from_array_empty Constructor as const fn #141

Merged
merged 1 commit into from
Apr 4, 2021
Merged
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
36 changes: 35 additions & 1 deletion src/arrayvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,13 @@ macro_rules! array_vec {
///
/// let more_ints = ArrayVec::from_array_len([5, 6, 7, 8], 2);
/// assert_eq!(more_ints.len(), 2);
///
/// let no_ints: ArrayVec<[u8; 5]> = ArrayVec::from_array_empty([1, 2, 3, 4, 5]);
/// assert_eq!(no_ints.len(), 0);
/// ```
#[repr(C)]
#[derive(Clone, Copy)]
pub struct ArrayVec<A: Array> {
pub struct ArrayVec<A> {
len: u16,
pub(crate) data: A,
}
Expand Down Expand Up @@ -954,6 +957,37 @@ impl<A: Array> ArrayVec<A> {
}
}

impl<A> ArrayVec<A> {
/// Wraps up an array as a new empty `ArrayVec`.
///
/// If you want to simply use the full array, use `from` instead.
///
/// ## Examples
///
/// This method in particular allows to create values for statics:
///
/// ```rust
/// # use tinyvec::ArrayVec;
/// static DATA: ArrayVec<[u8; 5]> = ArrayVec::from_array_empty([0; 5]);
/// assert_eq!(DATA.len(), 0);
/// ```
///
/// But of course it is just an normal empty `ArrayVec`:
///
/// ```rust
/// # use tinyvec::ArrayVec;
/// let mut data = ArrayVec::from_array_empty([1, 2, 3, 4]);
/// assert_eq!(&data[..], &[]);
/// data.push(42);
/// assert_eq!(&data[..], &[42]);
/// ```
#[inline]
#[must_use]
pub const fn from_array_empty(data: A) -> Self {
Self { data, len: 0 }
}
}

#[cfg(feature = "grab_spare_slice")]
impl<A: Array> ArrayVec<A> {
/// Obtain the shared slice of the array _after_ the active memory.
Expand Down