devela/data/collections/array/d1/methods/boxed.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
// devela::data::collections::array::d1::methods::boxed
//
//! 1-dimensional array *Boxed* methods.
//
// TOC
// - constructors
// - methods
use crate::{array_init, Array, Box, Boxed, Vec};
/// # *Boxed* constructors
impl<T, const CAP: usize> Array<T, CAP, Boxed> {
/// Returns a new `Array` from the given `boxed_array`.
pub fn new_boxed(boxed_array: Box<[T; CAP]>) -> Self {
Array { data: boxed_array }
}
/// Returns an array, allocated in the heap, filled with `element`, cloned.
///
/// # Example
/// ```
/// # use devela::{Array, Boxed};
/// let mut a = Array::<_, 1_000, Boxed>::with_cloned(0);
/// ```
pub fn with_cloned(element: T) -> Self
where
T: Clone,
{
let data = array_init!(clone_heap [T; CAP], "safe_data", "unsafe_array", element);
Self { data }
}
}
/// # *Boxed* methods
impl<T, const CAP: usize> Array<T, CAP, Boxed> {
/// Returns the inner [`Box`]ed primitive array.
#[must_use]
pub fn into_array(self) -> Box<[T; CAP]> {
self.data
}
/// Returns the inner [`Box`]ed primitive array as a slice.
#[must_use]
pub fn into_slice(self) -> Box<[T]> {
self.data
}
/// Returns the inner [`Box`]ed primitive array as a `Vec`.
#[must_use]
pub fn into_vec(self) -> Vec<T> {
self.into_slice().into_vec()
}
}