devela/data/dst/
buffer.rs
1use crate::{Array, ConstDefault, Deref, DerefMut, MaybeUninit, MemPod};
4
5#[doc = crate::TAG_DATA_STRUCTURE!()]
6pub unsafe trait DstBuf {
17 type Inner: MemPod;
19
20 fn as_ref(&self) -> &[MaybeUninit<Self::Inner>];
22
23 fn as_mut(&mut self) -> &mut [MaybeUninit<Self::Inner>];
25
26 fn extend(&mut self, len: usize) -> Result<(), ()>;
28
29 fn round_to_words(bytes: usize) -> usize {
31 super::round_to_words::<Self::Inner>(bytes)
32 }
33}
34
35#[rustfmt::skip]
37unsafe impl<T, U> DstBuf for &mut T where U: MemPod, T: DstBuf<Inner = U> {
38 type Inner = T::Inner;
39
40 fn as_ref(&self) -> &[MaybeUninit<Self::Inner>] {
41 (**self).as_ref()
42 }
43 fn as_mut(&mut self) -> &mut [MaybeUninit<Self::Inner>] {
44 (**self).as_mut()
45 }
46 fn extend(&mut self, len: usize) -> Result<(), ()> {
47 (**self).extend(len)
48 }
49}
50
51unsafe impl<T: MemPod, const CAP: usize> DstBuf for [MaybeUninit<T>; CAP] {
53 type Inner = T;
54
55 fn as_ref(&self) -> &[MaybeUninit<Self::Inner>] {
56 self
57 }
58 fn as_mut(&mut self) -> &mut [MaybeUninit<Self::Inner>] {
59 self
60 }
61 fn extend(&mut self, len: usize) -> Result<(), ()> {
62 if len > CAP {
63 Err(())
64 } else {
65 Ok(())
66 }
67 }
68}
69
70#[cfg(feature = "alloc")]
84#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "alloc")))]
85unsafe impl<T: MemPod> DstBuf for crate::Vec<MaybeUninit<T>> {
86 type Inner = T;
87 fn as_ref(&self) -> &[MaybeUninit<Self::Inner>] {
88 self
89 }
90 fn as_mut(&mut self) -> &mut [MaybeUninit<Self::Inner>] {
91 self
92 }
93 fn extend(&mut self, len: usize) -> Result<(), ()> {
94 if len > self.len() {
95 self.resize(len, MaybeUninit::uninit());
96 let cap = self.capacity();
97 self.resize(cap, MaybeUninit::uninit());
98 }
99 Ok(())
100 }
101}
102
103#[doc = crate::TAG_DATA_STRUCTURE!()]
104pub struct DstArray<T, const CAP: usize> {
106 inner: Array<MaybeUninit<T>, CAP>,
107}
108impl<T, const CAP: usize> Deref for DstArray<T, CAP> {
109 type Target = Array<MaybeUninit<T>, CAP>;
110
111 fn deref(&self) -> &Self::Target {
112 &self.inner
113 }
114}
115impl<T, const CAP: usize> DerefMut for DstArray<T, CAP> {
116 fn deref_mut(&mut self) -> &mut Self::Target {
117 &mut self.inner
118 }
119}
120impl<T: MemPod, const CAP: usize> Default for DstArray<T, CAP> {
121 fn default() -> Self {
122 Self { inner: Array::new([MaybeUninit::uninit(); CAP]) }
123 }
124}
125impl<T: MemPod, const CAP: usize> ConstDefault for DstArray<T, CAP> {
126 const DEFAULT: Self = Self {
127 inner: Array::new_bare([MaybeUninit::uninit(); CAP]),
128 };
129}
130#[rustfmt::skip]
131unsafe impl<T: MemPod, const CAP: usize> DstBuf for DstArray<T, CAP> {
132 type Inner = T;
133 fn as_ref(&self) -> &[MaybeUninit<Self::Inner>] {
134 &self.inner
135 }
136 fn as_mut(&mut self) -> &mut [MaybeUninit<Self::Inner>] {
137 &mut self.inner
138 }
139 fn extend(&mut self, len: usize) -> Result<(), ()> {
140 if len > CAP { Err(()) } else { Ok(()) }
141 }
142}
143
144pub type DstArrayUsize<const CAP: usize> = DstArray<usize, CAP>;
147
148#[cfg(feature = "alloc")]
151#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "alloc")))]
152pub type DstVecUsize = crate::Vec<MaybeUninit<usize>>;
153
154