fidl_next_codec/
decoder.rs
1use core::mem::take;
8use core::ptr::NonNull;
9use core::slice;
10
11use crate::{Chunk, Decode, DecodeError, Owned, Slot, CHUNK_SIZE};
12
13pub trait InternalHandleDecoder {
15 #[doc(hidden)]
20 fn __internal_take_handles(&mut self, count: usize) -> Result<(), DecodeError>;
21
22 #[doc(hidden)]
27 fn __internal_handles_remaining(&self) -> usize;
28}
29
30pub unsafe trait Decoder: InternalHandleDecoder {
42 fn take_chunks_raw(&mut self, count: usize) -> Result<NonNull<Chunk>, DecodeError>;
46
47 fn finish(&mut self) -> Result<(), DecodeError>;
51}
52
53impl InternalHandleDecoder for &mut [Chunk] {
54 #[inline]
55 fn __internal_take_handles(&mut self, _: usize) -> Result<(), DecodeError> {
56 Err(DecodeError::InsufficientHandles)
57 }
58
59 #[inline]
60 fn __internal_handles_remaining(&self) -> usize {
61 0
62 }
63}
64
65unsafe impl Decoder for &mut [Chunk] {
66 #[inline]
67 fn take_chunks_raw(&mut self, count: usize) -> Result<NonNull<Chunk>, DecodeError> {
68 if count > self.len() {
69 return Err(DecodeError::InsufficientData);
70 }
71
72 let chunks = take(self);
73 let (prefix, suffix) = unsafe { chunks.split_at_mut_unchecked(count) };
74 *self = suffix;
75 unsafe { Ok(NonNull::new_unchecked(prefix.as_mut_ptr())) }
76 }
77
78 #[inline]
79 fn finish(&mut self) -> Result<(), DecodeError> {
80 if !self.is_empty() {
81 return Err(DecodeError::ExtraBytes { num_extra: self.len() * CHUNK_SIZE });
82 }
83
84 Ok(())
85 }
86}
87
88pub trait DecoderExt {
90 fn take_chunks<'buf>(
92 self: &mut &'buf mut Self,
93 count: usize,
94 ) -> Result<&'buf mut [Chunk], DecodeError>;
95
96 fn take_slot<'buf, T>(self: &mut &'buf mut Self) -> Result<Slot<'buf, T>, DecodeError>;
98
99 fn take_slice_slot<'buf, T>(
101 self: &mut &'buf mut Self,
102 len: usize,
103 ) -> Result<Slot<'buf, [T]>, DecodeError>;
104
105 fn decode_next<'buf, T: Decode<Self>>(
109 self: &mut &'buf mut Self,
110 ) -> Result<Owned<'buf, T>, DecodeError>;
111
112 fn decode_next_slice<'buf, T: Decode<Self>>(
116 self: &mut &'buf mut Self,
117 len: usize,
118 ) -> Result<Owned<'buf, [T]>, DecodeError>;
119
120 fn decode_last<'buf, T: Decode<Self>>(
125 self: &mut &'buf mut Self,
126 ) -> Result<Owned<'buf, T>, DecodeError>;
127
128 fn decode_last_slice<'buf, T: Decode<Self>>(
133 self: &mut &'buf mut Self,
134 len: usize,
135 ) -> Result<Owned<'buf, [T]>, DecodeError>;
136}
137
138impl<D: Decoder + ?Sized> DecoderExt for D {
139 fn take_chunks<'buf>(
140 self: &mut &'buf mut Self,
141 count: usize,
142 ) -> Result<&'buf mut [Chunk], DecodeError> {
143 self.take_chunks_raw(count).map(|p| unsafe { slice::from_raw_parts_mut(p.as_ptr(), count) })
144 }
145
146 fn take_slot<'buf, T>(self: &mut &'buf mut Self) -> Result<Slot<'buf, T>, DecodeError> {
147 assert!(
150 align_of::<T>() <= CHUNK_SIZE,
151 "attempted to take a slot for a type with an alignment higher \
152 than {}",
153 CHUNK_SIZE,
154 );
155
156 let count = size_of::<T>().div_ceil(CHUNK_SIZE);
157 let chunks = self.take_chunks(count)?;
158 unsafe { Ok(Slot::new_unchecked(chunks.as_mut_ptr().cast())) }
161 }
162
163 fn take_slice_slot<'buf, T>(
164 self: &mut &'buf mut Self,
165 len: usize,
166 ) -> Result<Slot<'buf, [T]>, DecodeError> {
167 assert!(
168 align_of::<T>() <= CHUNK_SIZE,
169 "attempted to take a slice slot for a type with an alignment \
170 higher than {}",
171 CHUNK_SIZE,
172 );
173
174 let count = (size_of::<T>() * len).div_ceil(CHUNK_SIZE);
175 let chunks = self.take_chunks(count)?;
176 unsafe { Ok(Slot::new_slice_unchecked(chunks.as_mut_ptr().cast(), len)) }
179 }
180
181 fn decode_next<'buf, T: Decode<Self>>(
182 self: &mut &'buf mut Self,
183 ) -> Result<Owned<'buf, T>, DecodeError> {
184 let mut slot = self.take_slot::<T>()?;
185 T::decode(slot.as_mut(), self)?;
186 unsafe { Ok(Owned::new_unchecked(slot.as_mut_ptr())) }
187 }
188
189 fn decode_next_slice<'buf, T: Decode<Self>>(
190 self: &mut &'buf mut Self,
191 len: usize,
192 ) -> Result<Owned<'buf, [T]>, DecodeError> {
193 let mut slot = self.take_slice_slot::<T>(len)?;
194 for i in 0..len {
195 T::decode(slot.index(i), self)?;
196 }
197 unsafe { Ok(Owned::new_unchecked(slot.as_mut_ptr())) }
198 }
199
200 fn decode_last<'buf, T: Decode<Self>>(
201 self: &mut &'buf mut Self,
202 ) -> Result<Owned<'buf, T>, DecodeError> {
203 let result = self.decode_next()?;
204 self.finish()?;
205 Ok(result)
206 }
207
208 fn decode_last_slice<'buf, T: Decode<Self>>(
209 self: &mut &'buf mut Self,
210 len: usize,
211 ) -> Result<Owned<'buf, [T]>, DecodeError> {
212 let result = self.decode_next_slice(len)?;
213 self.finish()?;
214 Ok(result)
215 }
216}