1use {
2 crate::{
3 PrimitiveTy,
4 wincode_extra::{LenMap, Map},
5 },
6 alloc::{borrow::Cow, vec::Vec},
7 core::{marker::PhantomData, mem::MaybeUninit},
8 wincode::{
9 ReadError, ReadResult, SchemaRead, SchemaReadContext,
10 config::{Config, ConfigCore, DefaultConfig},
11 context::Len,
12 io::Reader,
13 len::SeqLen,
14 },
15};
16
17#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
19pub enum PrimitiveValue {
20 U8(u8),
22 U16(u16),
24 U32(u32),
26 U64(u64),
28 I8(i8),
30 I16(i16),
32 I32(i32),
34 I64(i64),
36 F32(f32),
38 F64(f64),
40 Bool(bool),
42}
43
44impl PrimitiveValue {
45 #[inline]
50 pub const fn size(self) -> usize {
51 match self {
52 PrimitiveValue::U8(_) => 1,
53 PrimitiveValue::U16(_) => 2,
54 PrimitiveValue::U32(_) => 4,
55 PrimitiveValue::U64(_) => 8,
56 PrimitiveValue::I8(_) => 1,
57 PrimitiveValue::I16(_) => 2,
58 PrimitiveValue::I32(_) => 4,
59 PrimitiveValue::I64(_) => 8,
60 PrimitiveValue::F32(_) => 4,
61 PrimitiveValue::F64(_) => 8,
62 PrimitiveValue::Bool(_) => 1,
63 }
64 }
65}
66
67#[derive(Debug, Clone, PartialEq)]
74pub enum Value<'a> {
75 U8(u8),
77 U16(u16),
79 U32(u32),
81 U64(u64),
83 I8(i8),
85 I16(i16),
87 I32(i32),
89 I64(i64),
91 F32(f32),
93 F64(f64),
95 Bool(bool),
97 String(Cow<'a, str>),
99 Bytes(Cow<'a, [u8]>),
101 Vec(LazyVec<'a>),
103}
104
105impl Value<'_> {
106 #[inline]
114 pub fn size(&self) -> usize {
115 match self {
116 Value::U8(_) => 1,
117 Value::U16(_) => 2,
118 Value::U32(_) => 4,
119 Value::U64(_) => 8,
120 Value::I8(_) => 1,
121 Value::I16(_) => 2,
122 Value::I32(_) => 4,
123 Value::I64(_) => 8,
124 Value::F32(_) => 4,
125 Value::F64(_) => 8,
126 Value::Bool(_) => 1,
127 Value::String(str) => str.len(),
128 Value::Bytes(bytes) => bytes.len(),
129 Value::Vec(vec) => vec.payload.len(),
130 }
131 }
132}
133
134#[derive(Debug, Clone, PartialEq)]
142pub struct LazyVec<'a> {
143 ty: PrimitiveTy,
144 payload: Cow<'a, [u8]>,
145 len: usize,
149}
150
151pub mod lazy_vec {
153 use {super::*, crate::DynPrimitiveTy};
154
155 #[derive(Debug, Clone, Copy)]
159 pub struct Iter<'a, As> {
160 payload: &'a [u8],
161 len: usize,
162 index: usize,
163 _marker: PhantomData<As>,
164 }
165
166 #[derive(Debug, Clone)]
170 pub struct IntoIter<'a, As> {
171 payload: Cow<'a, [u8]>,
172 len: usize,
173 index: usize,
174 _marker: PhantomData<As>,
175 }
176
177 impl<'a> LazyVec<'a> {
178 pub(crate) unsafe fn new_unchecked(
182 ty: PrimitiveTy,
183 len: usize,
184 payload: Cow<'a, [u8]>,
185 ) -> Self {
186 Self { ty, len, payload }
187 }
188
189 #[inline]
247 pub fn try_iter_as<'iter, As>(&'iter self) -> ReadResult<Iter<'iter, As>>
248 where
249 As: DynPrimitiveTy,
250 {
251 validate_element_type::<As>(self.ty)?;
252
253 Ok(Iter {
254 len: self.len,
255 payload: self.payload.as_ref(),
256 index: 0,
257 _marker: PhantomData,
258 })
259 }
260
261 #[inline]
273 pub fn try_into_iter_as<As>(self) -> ReadResult<IntoIter<'a, As>>
274 where
275 As: DynPrimitiveTy,
276 {
277 validate_element_type::<As>(self.ty)?;
278
279 Ok(IntoIter {
280 len: self.len,
281 payload: self.payload,
282 index: 0,
283 _marker: PhantomData,
284 })
285 }
286
287 #[inline]
294 pub fn into_dyn_vec(self) -> ReadResult<Vec<PrimitiveValue>> {
295 <<DefaultConfig as Config>::LengthEncoding as SeqLen<DefaultConfig>>::prealloc_check::<
296 PrimitiveValue,
297 >(self.len)?;
298 <Array<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::get_with_context(
299 (Len(self.len), self.ty),
300 &self.payload[..],
301 )
302 }
303
304 #[inline]
306 pub const fn len(&self) -> usize {
307 self.len
308 }
309
310 #[inline]
312 pub fn is_empty(&self) -> bool {
313 self.payload.is_empty()
314 }
315
316 #[cfg(all(test, feature = "std"))]
317 pub(crate) fn has_borrowed_payload(&self) -> bool {
318 matches!(self.payload, Cow::Borrowed(_))
319 }
320
321 #[inline]
323 pub const fn ty(&self) -> PrimitiveTy {
324 self.ty
325 }
326 }
327
328 #[inline]
329 fn validate_element_type<As: DynPrimitiveTy>(ty: PrimitiveTy) -> ReadResult<()> {
330 #[cold]
331 const fn ty_mismatch() -> ReadError {
332 ReadError::Custom("lazy vector element type mismatch")
333 }
334
335 if As::TYPE != ty || ty.size() != size_of::<As>() {
336 return Err(ty_mismatch());
337 }
338
339 Ok(())
340 }
341
342 macro_rules! impl_iterator {
343 ($iter:ident) => {
344 impl<As> Iterator for $iter<'_, As>
345 where
346 As: for<'de> SchemaRead<'de, DefaultConfig, Dst = As>,
347 {
348 type Item = ReadResult<As>;
349
350 #[inline]
351 fn next(&mut self) -> Option<Self::Item> {
352 if self.index >= self.len {
353 return None;
354 }
355 let start = self.index * size_of::<As>();
356 let remaining = unsafe { self.payload.as_ref().get_unchecked(start..) };
361 let item = As::get(remaining);
362 self.index += 1;
363 Some(item)
364 }
365
366 #[inline]
367 fn size_hint(&self) -> (usize, Option<usize>) {
368 let remaining = self.len - self.index;
369 (remaining, Some(remaining))
370 }
371 }
372
373 impl<As> ExactSizeIterator for $iter<'_, As>
374 where
375 As: for<'de> SchemaRead<'de, DefaultConfig, Dst = As>,
376 {
377 #[inline]
378 fn len(&self) -> usize {
379 self.len - self.index
380 }
381 }
382
383 impl<As> core::iter::FusedIterator for $iter<'_, As> where
384 As: for<'de> SchemaRead<'de, DefaultConfig, Dst = As>
385 {
386 }
387 };
388 }
389
390 impl_iterator!(Iter);
391 impl_iterator!(IntoIter);
392}
393
394unsafe impl<'de, C: ConfigCore> SchemaReadContext<'de, C, PrimitiveTy> for PrimitiveValue {
395 type Dst = Self;
396
397 #[inline]
398 fn read_with_context(
399 ctx: PrimitiveTy,
400 reader: impl Reader<'de>,
401 dst: &mut MaybeUninit<Self::Dst>,
402 ) -> ReadResult<()> {
403 let val = match ctx {
404 PrimitiveTy::U8 => <u8 as SchemaRead<C>>::get(reader).map(PrimitiveValue::U8)?,
405 PrimitiveTy::U16 => <u16 as SchemaRead<C>>::get(reader).map(PrimitiveValue::U16)?,
406 PrimitiveTy::U32 => <u32 as SchemaRead<C>>::get(reader).map(PrimitiveValue::U32)?,
407 PrimitiveTy::U64 => <u64 as SchemaRead<C>>::get(reader).map(PrimitiveValue::U64)?,
408 PrimitiveTy::I8 => <i8 as SchemaRead<C>>::get(reader).map(PrimitiveValue::I8)?,
409 PrimitiveTy::I16 => <i16 as SchemaRead<C>>::get(reader).map(PrimitiveValue::I16)?,
410 PrimitiveTy::I32 => <i32 as SchemaRead<C>>::get(reader).map(PrimitiveValue::I32)?,
411 PrimitiveTy::I64 => <i64 as SchemaRead<C>>::get(reader).map(PrimitiveValue::I64)?,
412 PrimitiveTy::F32 => <f32 as SchemaRead<C>>::get(reader).map(PrimitiveValue::F32)?,
413 PrimitiveTy::F64 => <f64 as SchemaRead<C>>::get(reader).map(PrimitiveValue::F64)?,
414 PrimitiveTy::Bool => <bool as SchemaRead<C>>::get(reader).map(PrimitiveValue::Bool)?,
415 };
416
417 dst.write(val);
418
419 Ok(())
420 }
421}
422
423unsafe impl<'de> SchemaReadContext<'de, DefaultConfig, PrimitiveTy> for usize {
424 type Dst = Self;
425
426 #[inline]
427 fn read_with_context(
428 ctx: PrimitiveTy,
429 reader: impl Reader<'de>,
430 dst: &mut MaybeUninit<Self::Dst>,
431 ) -> ReadResult<()> {
432 #[inline]
433 fn try_cast_to_usize(val: impl TryInto<usize>) -> ReadResult<usize> {
434 #[cold]
435 fn err() -> ReadError {
436 ReadError::Custom("cannot cast to usize")
437 }
438 val.try_into().map_err(|_| err())
439 }
440
441 let val = match ctx {
442 PrimitiveTy::U8 => <u8 as SchemaRead<DefaultConfig>>::get(reader).map(usize::from)?,
443 PrimitiveTy::U16 => <u16 as SchemaRead<DefaultConfig>>::get(reader).map(usize::from)?,
444 PrimitiveTy::U32 => {
445 <u32 as SchemaRead<DefaultConfig>>::get(reader).and_then(try_cast_to_usize)?
446 }
447 PrimitiveTy::U64 => {
448 <u64 as SchemaRead<DefaultConfig>>::get(reader).and_then(try_cast_to_usize)?
449 }
450 PrimitiveTy::I8 => {
451 <i8 as SchemaRead<DefaultConfig>>::get(reader).and_then(try_cast_to_usize)?
452 }
453 PrimitiveTy::I16 => {
454 <i16 as SchemaRead<DefaultConfig>>::get(reader).and_then(try_cast_to_usize)?
455 }
456 PrimitiveTy::I32 => {
457 <i32 as SchemaRead<DefaultConfig>>::get(reader).and_then(try_cast_to_usize)?
458 }
459 PrimitiveTy::I64 => {
460 <i64 as SchemaRead<DefaultConfig>>::get(reader).and_then(try_cast_to_usize)?
461 }
462 _ => return Err(ReadError::Custom("cannot cast to usize")),
463 };
464
465 dst.write(val);
466
467 Ok(())
468 }
469}
470unsafe impl<'de, C: ConfigCore> SchemaReadContext<'de, C, PrimitiveTy> for Value<'de> {
471 type Dst = Self;
472
473 #[inline]
474 fn read_with_context(
475 ctx: PrimitiveTy,
476 reader: impl Reader<'de>,
477 dst: &mut MaybeUninit<Self::Dst>,
478 ) -> ReadResult<()> {
479 let val = match ctx {
480 PrimitiveTy::U8 => <u8 as SchemaRead<C>>::get(reader).map(Value::U8)?,
481 PrimitiveTy::U16 => <u16 as SchemaRead<C>>::get(reader).map(Value::U16)?,
482 PrimitiveTy::U32 => <u32 as SchemaRead<C>>::get(reader).map(Value::U32)?,
483 PrimitiveTy::U64 => <u64 as SchemaRead<C>>::get(reader).map(Value::U64)?,
484 PrimitiveTy::I8 => <i8 as SchemaRead<C>>::get(reader).map(Value::I8)?,
485 PrimitiveTy::I16 => <i16 as SchemaRead<C>>::get(reader).map(Value::I16)?,
486 PrimitiveTy::I32 => <i32 as SchemaRead<C>>::get(reader).map(Value::I32)?,
487 PrimitiveTy::I64 => <i64 as SchemaRead<C>>::get(reader).map(Value::I64)?,
488 PrimitiveTy::F32 => <f32 as SchemaRead<C>>::get(reader).map(Value::F32)?,
489 PrimitiveTy::F64 => <f64 as SchemaRead<C>>::get(reader).map(Value::F64)?,
490 PrimitiveTy::Bool => <bool as SchemaRead<C>>::get(reader).map(Value::Bool)?,
491 };
492
493 dst.write(val);
494
495 Ok(())
496 }
497}
498
499unsafe impl<'de> SchemaReadContext<'de, DefaultConfig, PrimitiveTy> for Vec<PrimitiveValue> {
500 type Dst = Self;
501
502 #[inline]
503 fn read_with_context(
504 ctx: PrimitiveTy,
505 reader: impl Reader<'de>,
506 dst: &mut MaybeUninit<Self::Dst>,
507 ) -> ReadResult<()> {
508 match ctx {
509 PrimitiveTy::U8 => {
510 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
511 Map::new(PrimitiveValue::U8),
512 reader,
513 dst,
514 )
515 }
516 PrimitiveTy::U16 => {
517 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
518 Map::new(PrimitiveValue::U16),
519 reader,
520 dst,
521 )
522 }
523 PrimitiveTy::U32 => {
524 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
525 Map::new(PrimitiveValue::U32),
526 reader,
527 dst,
528 )
529 }
530 PrimitiveTy::U64 => {
531 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
532 Map::new(PrimitiveValue::U64),
533 reader,
534 dst,
535 )
536 }
537 PrimitiveTy::I8 => {
538 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
539 Map::new(PrimitiveValue::I8),
540 reader,
541 dst,
542 )
543 }
544 PrimitiveTy::I16 => {
545 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
546 Map::new(PrimitiveValue::I16),
547 reader,
548 dst,
549 )
550 }
551 PrimitiveTy::I32 => {
552 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
553 Map::new(PrimitiveValue::I32),
554 reader,
555 dst,
556 )
557 }
558 PrimitiveTy::I64 => {
559 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
560 Map::new(PrimitiveValue::I64),
561 reader,
562 dst,
563 )
564 }
565 PrimitiveTy::F32 => {
566 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
567 Map::new(PrimitiveValue::F32),
568 reader,
569 dst,
570 )
571 }
572 PrimitiveTy::F64 => {
573 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
574 Map::new(PrimitiveValue::F64),
575 reader,
576 dst,
577 )
578 }
579 PrimitiveTy::Bool => {
580 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
581 Map::new(PrimitiveValue::Bool),
582 reader,
583 dst,
584 )
585 }
586 }
587 }
588}
589
590pub(crate) struct Array<T> {
591 _marker: core::marker::PhantomData<T>,
592}
593
594unsafe impl<'de, C: ConfigCore> SchemaReadContext<'de, C, (Len, PrimitiveTy)>
595 for Array<PrimitiveValue>
596{
597 type Dst = Vec<PrimitiveValue>;
598
599 #[inline]
600 fn read_with_context(
601 ctx: (Len, PrimitiveTy),
602 reader: impl Reader<'de>,
603 dst: &mut MaybeUninit<Self::Dst>,
604 ) -> ReadResult<()> {
605 let (Len(len), ty) = ctx;
606
607 match ty {
608 PrimitiveTy::U8 => {
609 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
610 LenMap::new(len, PrimitiveValue::U8),
611 reader,
612 dst,
613 )
614 }
615 PrimitiveTy::U16 => {
616 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
617 LenMap::new(len, PrimitiveValue::U16),
618 reader,
619 dst,
620 )
621 }
622 PrimitiveTy::U32 => {
623 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
624 LenMap::new(len, PrimitiveValue::U32),
625 reader,
626 dst,
627 )
628 }
629 PrimitiveTy::U64 => {
630 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
631 LenMap::new(len, PrimitiveValue::U64),
632 reader,
633 dst,
634 )
635 }
636 PrimitiveTy::I8 => {
637 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
638 LenMap::new(len, PrimitiveValue::I8),
639 reader,
640 dst,
641 )
642 }
643 PrimitiveTy::I16 => {
644 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
645 LenMap::new(len, PrimitiveValue::I16),
646 reader,
647 dst,
648 )
649 }
650 PrimitiveTy::I32 => {
651 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
652 LenMap::new(len, PrimitiveValue::I32),
653 reader,
654 dst,
655 )
656 }
657 PrimitiveTy::I64 => {
658 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
659 LenMap::new(len, PrimitiveValue::I64),
660 reader,
661 dst,
662 )
663 }
664 PrimitiveTy::F32 => {
665 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
666 LenMap::new(len, PrimitiveValue::F32),
667 reader,
668 dst,
669 )
670 }
671 PrimitiveTy::F64 => {
672 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
673 LenMap::new(len, PrimitiveValue::F64),
674 reader,
675 dst,
676 )
677 }
678 PrimitiveTy::Bool => {
679 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
680 LenMap::new(len, PrimitiveValue::Bool),
681 reader,
682 dst,
683 )
684 }
685 }
686 }
687}