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