float_pigment_consistent_bincode/config/
mod.rs1use crate::de::read::BincodeRead;
31use crate::error::Result;
32use crate::io::{Read, Write};
33use alloc::vec::Vec;
34use core::marker::PhantomData;
35
36pub(crate) use self::endian::BincodeByteOrder;
37pub(crate) use self::int::IntEncoding;
38pub(crate) use self::internal::*;
39pub(crate) use self::limit::SizeLimit;
40pub(crate) use self::trailing::TrailingBytes;
41
42pub use self::endian::{BigEndian, LittleEndian, NativeEndian};
43pub use self::int::{FixintEncoding, VarintEncoding};
44pub use self::limit::{Bounded, Infinite};
45pub use self::trailing::{AllowTrailing, RejectTrailing};
46
47mod endian;
48mod int;
49mod limit;
50mod trailing;
51
52#[derive(Copy, Clone)]
75pub struct DefaultOptions(Infinite);
76
77pub struct SizeDetail(Vec<u32>);
79
80impl DefaultOptions {
81 pub fn new() -> DefaultOptions {
89 DefaultOptions(Infinite)
90 }
91}
92
93impl Default for DefaultOptions {
94 fn default() -> Self {
95 Self::new()
96 }
97}
98
99impl InternalOptions for DefaultOptions {
100 type Limit = Infinite;
101 type Endian = LittleEndian;
102 type IntEncoding = VarintEncoding;
103 type Trailing = RejectTrailing;
104
105 #[inline(always)]
106 fn limit(&mut self) -> &mut Infinite {
107 &mut self.0
108 }
109}
110
111pub trait Options: InternalOptions + Sized {
130 fn with_no_limit(self) -> WithOtherLimit<Self, Infinite> {
133 WithOtherLimit::new(self, Infinite)
134 }
135
136 fn with_limit(self, limit: u64) -> WithOtherLimit<Self, Bounded> {
138 WithOtherLimit::new(self, Bounded(limit))
139 }
140
141 fn with_little_endian(self) -> WithOtherEndian<Self, LittleEndian> {
144 WithOtherEndian::new(self)
145 }
146
147 fn with_big_endian(self) -> WithOtherEndian<Self, BigEndian> {
149 WithOtherEndian::new(self)
150 }
151
152 fn with_native_endian(self) -> WithOtherEndian<Self, NativeEndian> {
154 WithOtherEndian::new(self)
155 }
156
157 fn with_varint_encoding(self) -> WithOtherIntEncoding<Self, VarintEncoding> {
159 WithOtherIntEncoding::new(self)
160 }
161
162 fn with_fixint_encoding(self) -> WithOtherIntEncoding<Self, FixintEncoding> {
164 WithOtherIntEncoding::new(self)
165 }
166
167 fn reject_trailing_bytes(self) -> WithOtherTrailing<Self, RejectTrailing> {
169 WithOtherTrailing::new(self)
170 }
171
172 fn allow_trailing_bytes(self) -> WithOtherTrailing<Self, AllowTrailing> {
174 WithOtherTrailing::new(self)
175 }
176
177 #[inline(always)]
179 fn serialize<S: ?Sized + serde::Serialize>(self, t: &S) -> Result<Vec<u8>> {
180 crate::internal::serialize(t, self)
181 }
182
183 #[inline(always)]
185 fn serialized_size<T: ?Sized + serde::Serialize>(self, t: &T) -> Result<(u64, SizeDetail)> {
186 crate::internal::serialized_size(t, self).map(|(s, sizes_list)| (s, SizeDetail(sizes_list)))
187 }
188
189 #[inline(always)]
194 fn serialize_into<W: Write, T: ?Sized + serde::Serialize>(
195 self,
196 w: W,
197 t: &T,
198 d: SizeDetail,
199 ) -> Result<()> {
200 crate::internal::serialize_into(w, t, self, d.0)
201 }
202
203 #[inline(always)]
205 fn deserialize<'a, T: serde::Deserialize<'a>>(self, bytes: &'a [u8]) -> Result<T> {
206 crate::internal::deserialize(bytes, self)
207 }
208
209 #[doc(hidden)]
211 #[inline(always)]
212 fn deserialize_in_place<'a, R, T>(self, reader: R, place: &mut T) -> Result<()>
213 where
214 R: BincodeRead<'a>,
215 T: serde::de::Deserialize<'a>,
216 {
217 crate::internal::deserialize_in_place(reader, self, place)
218 }
219
220 #[inline(always)]
222 fn deserialize_seed<'a, T: serde::de::DeserializeSeed<'a>>(
223 self,
224 seed: T,
225 bytes: &'a [u8],
226 ) -> Result<T::Value> {
227 crate::internal::deserialize_seed(seed, bytes, self)
228 }
229
230 #[inline(always)]
234 fn deserialize_from<R: Read, T: serde::de::DeserializeOwned>(self, reader: R) -> Result<T> {
235 crate::internal::deserialize_from(reader, self)
236 }
237
238 #[inline(always)]
242 fn deserialize_from_seed<'a, R: Read, T: serde::de::DeserializeSeed<'a>>(
243 self,
244 seed: T,
245 reader: R,
246 ) -> Result<T::Value> {
247 crate::internal::deserialize_from_seed(seed, reader, self)
248 }
249
250 #[inline(always)]
256 fn deserialize_from_custom<'a, R: BincodeRead<'a>, T: serde::de::DeserializeOwned>(
257 self,
258 reader: R,
259 ) -> Result<T> {
260 crate::internal::deserialize_from_custom(reader, self)
261 }
262
263 #[inline(always)]
269 fn deserialize_from_custom_seed<'a, R: BincodeRead<'a>, T: serde::de::DeserializeSeed<'a>>(
270 self,
271 seed: T,
272 reader: R,
273 ) -> Result<T::Value> {
274 crate::internal::deserialize_from_custom_seed(seed, reader, self)
275 }
276}
277
278impl<T: InternalOptions> Options for T {}
279
280#[derive(Clone, Copy)]
282pub struct WithOtherLimit<O: Options, L: SizeLimit> {
283 _options: O,
284 pub(crate) new_limit: L,
285}
286
287#[derive(Clone, Copy)]
289pub struct WithOtherEndian<O: Options, E: BincodeByteOrder> {
290 options: O,
291 _endian: PhantomData<E>,
292}
293
294#[derive(Clone, Copy)]
296pub struct WithOtherIntEncoding<O: Options, I: IntEncoding> {
297 options: O,
298 _length: PhantomData<I>,
299}
300
301#[derive(Clone, Copy)]
303pub struct WithOtherTrailing<O: Options, T: TrailingBytes> {
304 options: O,
305 _trailing: PhantomData<T>,
306}
307
308impl<O: Options, L: SizeLimit> WithOtherLimit<O, L> {
309 #[inline(always)]
310 pub(crate) fn new(options: O, limit: L) -> WithOtherLimit<O, L> {
311 WithOtherLimit {
312 _options: options,
313 new_limit: limit,
314 }
315 }
316}
317
318impl<O: Options, E: BincodeByteOrder> WithOtherEndian<O, E> {
319 #[inline(always)]
320 pub(crate) fn new(options: O) -> WithOtherEndian<O, E> {
321 WithOtherEndian {
322 options,
323 _endian: PhantomData,
324 }
325 }
326}
327
328impl<O: Options, I: IntEncoding> WithOtherIntEncoding<O, I> {
329 #[inline(always)]
330 pub(crate) fn new(options: O) -> WithOtherIntEncoding<O, I> {
331 WithOtherIntEncoding {
332 options,
333 _length: PhantomData,
334 }
335 }
336}
337
338impl<O: Options, T: TrailingBytes> WithOtherTrailing<O, T> {
339 #[inline(always)]
340 pub(crate) fn new(options: O) -> WithOtherTrailing<O, T> {
341 WithOtherTrailing {
342 options,
343 _trailing: PhantomData,
344 }
345 }
346}
347
348impl<O: Options, E: BincodeByteOrder + 'static> InternalOptions for WithOtherEndian<O, E> {
349 type Limit = O::Limit;
350 type Endian = E;
351 type IntEncoding = O::IntEncoding;
352 type Trailing = O::Trailing;
353 #[inline(always)]
354 fn limit(&mut self) -> &mut O::Limit {
355 self.options.limit()
356 }
357}
358
359impl<O: Options, L: SizeLimit + 'static> InternalOptions for WithOtherLimit<O, L> {
360 type Limit = L;
361 type Endian = O::Endian;
362 type IntEncoding = O::IntEncoding;
363 type Trailing = O::Trailing;
364 fn limit(&mut self) -> &mut L {
365 &mut self.new_limit
366 }
367}
368
369impl<O: Options, I: IntEncoding + 'static> InternalOptions for WithOtherIntEncoding<O, I> {
370 type Limit = O::Limit;
371 type Endian = O::Endian;
372 type IntEncoding = I;
373 type Trailing = O::Trailing;
374
375 fn limit(&mut self) -> &mut O::Limit {
376 self.options.limit()
377 }
378}
379
380impl<O: Options, T: TrailingBytes + 'static> InternalOptions for WithOtherTrailing<O, T> {
381 type Limit = O::Limit;
382 type Endian = O::Endian;
383 type IntEncoding = O::IntEncoding;
384 type Trailing = T;
385
386 fn limit(&mut self) -> &mut O::Limit {
387 self.options.limit()
388 }
389}
390
391mod internal {
392 use super::*;
393
394 pub trait InternalOptions {
395 type Limit: SizeLimit + 'static;
396 type Endian: BincodeByteOrder + 'static;
397 type IntEncoding: IntEncoding + 'static;
398 type Trailing: TrailingBytes + 'static;
399
400 fn limit(&mut self) -> &mut Self::Limit;
401 }
402
403 impl<'a, O: InternalOptions> InternalOptions for &'a mut O {
404 type Limit = O::Limit;
405 type Endian = O::Endian;
406 type IntEncoding = O::IntEncoding;
407 type Trailing = O::Trailing;
408
409 #[inline(always)]
410 fn limit(&mut self) -> &mut Self::Limit {
411 (*self).limit()
412 }
413 }
414}