Merge pull request #211 from GrantM11235/vec_into_array

Add new `Vec::into_array` method
This commit is contained in:
Emil Fresk 2021-06-25 11:09:10 +02:00 committed by GitHub
commit 22e4c73383
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -107,6 +107,32 @@ impl<T, const N: usize> Vec<T, N> {
unsafe { slice::from_raw_parts(self.buffer.as_ptr() as *const T, self.len) }
}
/// Returns the contents of the vector as an array of length `M` if the length
/// of the vector is exactly `M`, otherwise returns `Err(self)`.
///
/// # Examples
///
/// ```
/// use heapless::Vec;
/// let buffer: Vec<u8, 42> = Vec::from_slice(&[1, 2, 3, 5, 8]).unwrap();
/// let array: [u8; 5] = buffer.into_array().unwrap();
/// assert_eq!(array, [1, 2, 3, 5, 8]);
/// ```
pub fn into_array<const M: usize>(self) -> Result<[T; M], Self> {
if self.len() == M {
// This is how the unstable `MaybeUninit::array_assume_init` method does it
let array = unsafe { (&self.buffer as *const _ as *const [T; M]).read() };
// We don't want `self`'s destructor to be called because that would drop all the
// items in the array
core::mem::forget(self);
Ok(array)
} else {
Err(self)
}
}
/// Extracts a mutable slice containing the entire vector.
///
/// Equivalent to `&s[..]`.