devela/data/list/array/d1/
definitions.rs

1// devela::data::list::array::d1::definitions
2//
3//! 1-dimensional array definitions
4//
5// RETHINK: usefullness
6// - not as much being generic over stack and heap… (impedes being as const as possible)
7// - maybe for methods on Option<T>
8
9use crate::{Bare, Storage};
10// #[cfg(feature = "dep_rkyv")] // DEP_DISABLED
11// use rkyv::{Archive, Deserialize, Serialize};
12
13#[doc = crate::TAG_DATA_STRUCTURE!()]
14/// A static 1-dimensional array backed by the core [array] primitive.
15///
16/// It is generic in respect to its
17/// elements (`T`),
18/// storage (`S`)
19/// and capacity (`CAP`).
20///
21/// See also the related trait: [`DataArray`][crate::DataArray].
22///
23/// ## Methods
24///
25/// - Constructors:
26///   [`new`][Self::new],
27///   [`new_boxed`][Self::new_boxed]*(`alloc`)*,
28///   [`new_bare`][Self::new_bare](*const*),
29///   [`with_cloned`][Self::with_cloned],
30///   [`with_copied`][Self::with_copied].
31/// - Deconstructors:
32///   [`as_slice`][Self::as_slice],
33///   [`as_bare_slice`][Self::as_bare_slice](*const*),
34///   [`as_mut_slice`][Self::as_mut_slice],
35///   [`into_array`][Self::into_array]*([`copy`][Self::into_array_copy])*,
36///   [`into_slice`][Self::into_slice]*(`alloc`)*,
37///   [`into_vec`][Self::into_vec]*(`alloc`)*.
38/// - Queries:
39///   [`capacity`][Self::capacity],
40///   [`contains`][Self::contains].
41///
42/// ---
43///
44/// - [Methods depending on `Option<T>`](#operations-depending-on-option-t).
45///   - Over single elements:
46///   [`take`][Self::take],
47///   [`replace`][Self::replace],
48///   [`unset`][Self::unset].
49///   - Over all elements:
50///   [`clear`][Self::clear],
51///   [`fill_none`][Self::fill_none]*(Clone)*.
52///   - Queries:
53///   [`count_none`][Self::count_none],
54///   [`count_some`][Self::count_some],
55///   [`is_empty`][Self::is_empty],
56///   [`is_full`][Self::is_full],
57///   [`first_none`][Self::first_none],
58///   [`first_none_mut`][Self::first_none_mut],
59///   [`first_none_ref`][Self::first_none_ref],
60///   [`first_some`][Self::first_some],
61///   [`first_some_mut`][Self::first_some_mut],
62///   [`first_some_ref`][Self::first_some_ref].
63///
64/// - [Methods depending on `Option<T: Copy>`](#operations-depending-on-option-t-copy).
65///   - Queries:
66///   [`is_bare_empty`][Self::is_bare_empty]*(const)*,
67///   [`is_bare_full`][Self::is_bare_full]*(const)*.
68#[must_use]
69// #[cfg_attr(feature = "dep_rkyv", derive(Archive, Serialize, Deserialize))]
70pub struct Array<T, const CAP: usize, S: Storage = Bare> {
71    pub(crate) data: S::Stored<[T; CAP]>,
72}
73
74// TODO: add manual serde impl
75
76// impl<T, const CAP: usize, S: Storage> Serialize for Array<T, CAP, S>
77// where
78//     T: Serialize,
79// {
80//     fn serialize<SER>(&self, serializer: SER) -> Result<SER::Ok, SER::Error>
81//     where
82//         SER: Serializer,
83//     {
84//         let mut state = serializer.serialize_struct("Array", 2)?;
85//         state.serialize_field("capacity", &CAP)?;
86//         // state.serialize_field("data", &self.iter().collect::<Vec<_>>())?;
87//         let mut seq = serializer.serialize_seq(Some(self.len()))?;
88//         for e in self.iter() {
89//             seq.serialize_element(e)?;
90//         }
91//         seq.end()?;
92//         state.end()
93//     }
94// }
95
96// impl<T, const CAP: usize, S: Storage> Serialize for Array<T, CAP, S>
97// where
98//     T: Serialize,
99// {
100//     fn serialize<SER>(&self, serializer: SER) -> Result<SER::Ok, SER::Error>
101//     where
102//         SER: Serializer,
103//     {
104//         let mut seq = serializer.serialize_seq(Some(self.len()))?;
105//         for e in self.iter() {
106//             seq.serialize_element(e)?;
107//         }
108//         seq.end()
109//     }
110// }
111
112// // Manual implementation of Serialize for Point
113// impl<T, const CAP: usize, S: Storage> Serialize for Array<T, CAP, S> {
114//     fn serialize<SE>(&self, serializer: SE) -> Result<SE::Ok, SE::Error>
115//     where
116//         S: Serializer,
117//     {
118//         // Serialize as a map with keys "x" and "y"
119//         let mut state = serializer.serialize_struct("Point", 2)?;
120//         state.serialize_field("x", &self.x)?;
121//         state.serialize_field("y", &self.y)?;
122//         state.end()
123//     }
124// }
125
126// use core::fmt;
127// use serde::de::{self, Visitor};
128// struct I32Visitor;
129//
130// impl<'de> Visitor<'de> for I32Visitor {
131//     type Value = i32;
132//
133//     fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
134//         formatter.write_str("an integer between -2^31 and 2^31")
135//     }
136//
137//     fn visit_i8<E>(self, value: i8) -> Result<Self::Value, E>
138//     where
139//         E: de::Error,
140//     {
141//         Ok(i32::from(value))
142//     }
143//
144//     fn visit_i32<E>(self, value: i32) -> Result<Self::Value, E>
145//     where
146//         E: de::Error,
147//     {
148//         Ok(value)
149//     }
150//
151//     fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E>
152//     where
153//         E: de::Error,
154//     {
155//         use std::i32;
156//         if value >= i64::from(i32::MIN) && value <= i64::from(i32::MAX) {
157//             Ok(value as i32)
158//         } else {
159//             Err(E::custom(format!("i32 out of range: {}", value)))
160//         }
161//     }
162// }