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]
248 pub fn try_iter_as<'iter, As>(&'iter self) -> ReadResult<Iter<'iter, As>>
249 where
250 As: DynPrimitiveTy,
251 {
252 validate_element_type::<As>(self.ty)?;
253
254 Ok(Iter {
255 len: self.len,
256 payload: self.payload.as_ref(),
257 index: 0,
258 _marker: PhantomData,
259 })
260 }
261
262 #[inline]
274 pub fn try_into_iter_as<As>(self) -> ReadResult<IntoIter<'a, As>>
275 where
276 As: DynPrimitiveTy,
277 {
278 validate_element_type::<As>(self.ty)?;
279
280 Ok(IntoIter {
281 len: self.len,
282 payload: self.payload,
283 index: 0,
284 _marker: PhantomData,
285 })
286 }
287
288 #[inline]
295 pub fn into_dyn_vec(self) -> ReadResult<Vec<PrimitiveValue>> {
296 <<DefaultConfig as Config>::LengthEncoding as SeqLen<DefaultConfig>>::prealloc_check::<
297 PrimitiveValue,
298 >(self.len)?;
299 <Array<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::get_with_context(
300 (Len(self.len), self.ty),
301 &self.payload[..],
302 )
303 }
304
305 #[inline]
307 pub const fn len(&self) -> usize {
308 self.len
309 }
310
311 #[inline]
313 pub fn is_empty(&self) -> bool {
314 self.payload.is_empty()
315 }
316
317 #[cfg(all(test, feature = "std"))]
318 pub(crate) fn has_borrowed_payload(&self) -> bool {
319 matches!(self.payload, Cow::Borrowed(_))
320 }
321
322 #[inline]
324 pub const fn ty(&self) -> PrimitiveTy {
325 self.ty
326 }
327 }
328
329 #[inline]
330 fn validate_element_type<As: DynPrimitiveTy>(ty: PrimitiveTy) -> ReadResult<()> {
331 #[cold]
332 const fn ty_mismatch() -> ReadError {
333 ReadError::Custom("lazy vector element type mismatch")
334 }
335
336 if As::TYPE != ty || ty.size() != size_of::<As>() {
337 return Err(ty_mismatch());
338 }
339
340 Ok(())
341 }
342
343 macro_rules! impl_iterator {
344 ($iter:ident) => {
345 impl<As> Iterator for $iter<'_, As>
346 where
347 As: for<'de> SchemaRead<'de, DefaultConfig, Dst = As>,
348 {
349 type Item = ReadResult<As>;
350
351 #[inline]
352 fn next(&mut self) -> Option<Self::Item> {
353 if self.index >= self.len {
354 return None;
355 }
356 let start = self.index * size_of::<As>();
357 let remaining = unsafe { self.payload.as_ref().get_unchecked(start..) };
362 let item = As::get(remaining);
363 self.index += 1;
364 Some(item)
365 }
366
367 #[inline]
368 fn size_hint(&self) -> (usize, Option<usize>) {
369 let remaining = self.len - self.index;
370 (remaining, Some(remaining))
371 }
372 }
373
374 impl<As> ExactSizeIterator for $iter<'_, As>
375 where
376 As: for<'de> SchemaRead<'de, DefaultConfig, Dst = As>,
377 {
378 #[inline]
379 fn len(&self) -> usize {
380 self.len - self.index
381 }
382 }
383
384 impl<As> core::iter::FusedIterator for $iter<'_, As> where
385 As: for<'de> SchemaRead<'de, DefaultConfig, Dst = As>
386 {
387 }
388 };
389 }
390
391 impl_iterator!(Iter);
392 impl_iterator!(IntoIter);
393}
394
395unsafe impl<'de, C: ConfigCore> SchemaReadContext<'de, C, PrimitiveTy> for PrimitiveValue {
396 type Dst = Self;
397
398 #[inline]
399 fn read_with_context(
400 ctx: PrimitiveTy,
401 reader: impl Reader<'de>,
402 dst: &mut MaybeUninit<Self::Dst>,
403 ) -> ReadResult<()> {
404 let val = match ctx {
405 PrimitiveTy::U8 => <u8 as SchemaRead<C>>::get(reader).map(PrimitiveValue::U8)?,
406 PrimitiveTy::U16 => <u16 as SchemaRead<C>>::get(reader).map(PrimitiveValue::U16)?,
407 PrimitiveTy::U32 => <u32 as SchemaRead<C>>::get(reader).map(PrimitiveValue::U32)?,
408 PrimitiveTy::U64 => <u64 as SchemaRead<C>>::get(reader).map(PrimitiveValue::U64)?,
409 PrimitiveTy::I8 => <i8 as SchemaRead<C>>::get(reader).map(PrimitiveValue::I8)?,
410 PrimitiveTy::I16 => <i16 as SchemaRead<C>>::get(reader).map(PrimitiveValue::I16)?,
411 PrimitiveTy::I32 => <i32 as SchemaRead<C>>::get(reader).map(PrimitiveValue::I32)?,
412 PrimitiveTy::I64 => <i64 as SchemaRead<C>>::get(reader).map(PrimitiveValue::I64)?,
413 PrimitiveTy::F32 => <f32 as SchemaRead<C>>::get(reader).map(PrimitiveValue::F32)?,
414 PrimitiveTy::F64 => <f64 as SchemaRead<C>>::get(reader).map(PrimitiveValue::F64)?,
415 PrimitiveTy::Bool => <bool as SchemaRead<C>>::get(reader).map(PrimitiveValue::Bool)?,
416 };
417
418 dst.write(val);
419
420 Ok(())
421 }
422}
423
424unsafe impl<'de> SchemaReadContext<'de, DefaultConfig, PrimitiveTy> for usize {
425 type Dst = Self;
426
427 #[inline]
428 fn read_with_context(
429 ctx: PrimitiveTy,
430 reader: impl Reader<'de>,
431 dst: &mut MaybeUninit<Self::Dst>,
432 ) -> ReadResult<()> {
433 #[inline]
434 fn try_cast_to_usize(val: impl TryInto<usize>) -> ReadResult<usize> {
435 #[cold]
436 fn err() -> ReadError {
437 ReadError::Custom("cannot cast to usize")
438 }
439 val.try_into().map_err(|_| err())
440 }
441
442 let val = match ctx {
443 PrimitiveTy::U8 => <u8 as SchemaRead<DefaultConfig>>::get(reader).map(usize::from)?,
444 PrimitiveTy::U16 => <u16 as SchemaRead<DefaultConfig>>::get(reader).map(usize::from)?,
445 PrimitiveTy::U32 => {
446 <u32 as SchemaRead<DefaultConfig>>::get(reader).and_then(try_cast_to_usize)?
447 }
448 PrimitiveTy::U64 => {
449 <u64 as SchemaRead<DefaultConfig>>::get(reader).and_then(try_cast_to_usize)?
450 }
451 PrimitiveTy::I8 => {
452 <i8 as SchemaRead<DefaultConfig>>::get(reader).and_then(try_cast_to_usize)?
453 }
454 PrimitiveTy::I16 => {
455 <i16 as SchemaRead<DefaultConfig>>::get(reader).and_then(try_cast_to_usize)?
456 }
457 PrimitiveTy::I32 => {
458 <i32 as SchemaRead<DefaultConfig>>::get(reader).and_then(try_cast_to_usize)?
459 }
460 PrimitiveTy::I64 => {
461 <i64 as SchemaRead<DefaultConfig>>::get(reader).and_then(try_cast_to_usize)?
462 }
463 _ => return Err(ReadError::Custom("cannot cast to usize")),
464 };
465
466 dst.write(val);
467
468 Ok(())
469 }
470}
471unsafe impl<'de, C: ConfigCore> SchemaReadContext<'de, C, PrimitiveTy> for Value<'de> {
472 type Dst = Self;
473
474 #[inline]
475 fn read_with_context(
476 ctx: PrimitiveTy,
477 reader: impl Reader<'de>,
478 dst: &mut MaybeUninit<Self::Dst>,
479 ) -> ReadResult<()> {
480 let val = match ctx {
481 PrimitiveTy::U8 => <u8 as SchemaRead<C>>::get(reader).map(Value::U8)?,
482 PrimitiveTy::U16 => <u16 as SchemaRead<C>>::get(reader).map(Value::U16)?,
483 PrimitiveTy::U32 => <u32 as SchemaRead<C>>::get(reader).map(Value::U32)?,
484 PrimitiveTy::U64 => <u64 as SchemaRead<C>>::get(reader).map(Value::U64)?,
485 PrimitiveTy::I8 => <i8 as SchemaRead<C>>::get(reader).map(Value::I8)?,
486 PrimitiveTy::I16 => <i16 as SchemaRead<C>>::get(reader).map(Value::I16)?,
487 PrimitiveTy::I32 => <i32 as SchemaRead<C>>::get(reader).map(Value::I32)?,
488 PrimitiveTy::I64 => <i64 as SchemaRead<C>>::get(reader).map(Value::I64)?,
489 PrimitiveTy::F32 => <f32 as SchemaRead<C>>::get(reader).map(Value::F32)?,
490 PrimitiveTy::F64 => <f64 as SchemaRead<C>>::get(reader).map(Value::F64)?,
491 PrimitiveTy::Bool => <bool as SchemaRead<C>>::get(reader).map(Value::Bool)?,
492 };
493
494 dst.write(val);
495
496 Ok(())
497 }
498}
499
500unsafe impl<'de> SchemaReadContext<'de, DefaultConfig, PrimitiveTy> for Vec<PrimitiveValue> {
501 type Dst = Self;
502
503 #[inline]
504 fn read_with_context(
505 ctx: PrimitiveTy,
506 reader: impl Reader<'de>,
507 dst: &mut MaybeUninit<Self::Dst>,
508 ) -> ReadResult<()> {
509 match ctx {
510 PrimitiveTy::U8 => {
511 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
512 Map::new(PrimitiveValue::U8),
513 reader,
514 dst,
515 )
516 }
517 PrimitiveTy::U16 => {
518 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
519 Map::new(PrimitiveValue::U16),
520 reader,
521 dst,
522 )
523 }
524 PrimitiveTy::U32 => {
525 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
526 Map::new(PrimitiveValue::U32),
527 reader,
528 dst,
529 )
530 }
531 PrimitiveTy::U64 => {
532 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
533 Map::new(PrimitiveValue::U64),
534 reader,
535 dst,
536 )
537 }
538 PrimitiveTy::I8 => {
539 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
540 Map::new(PrimitiveValue::I8),
541 reader,
542 dst,
543 )
544 }
545 PrimitiveTy::I16 => {
546 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
547 Map::new(PrimitiveValue::I16),
548 reader,
549 dst,
550 )
551 }
552 PrimitiveTy::I32 => {
553 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
554 Map::new(PrimitiveValue::I32),
555 reader,
556 dst,
557 )
558 }
559 PrimitiveTy::I64 => {
560 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
561 Map::new(PrimitiveValue::I64),
562 reader,
563 dst,
564 )
565 }
566 PrimitiveTy::F32 => {
567 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
568 Map::new(PrimitiveValue::F32),
569 reader,
570 dst,
571 )
572 }
573 PrimitiveTy::F64 => {
574 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
575 Map::new(PrimitiveValue::F64),
576 reader,
577 dst,
578 )
579 }
580 PrimitiveTy::Bool => {
581 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
582 Map::new(PrimitiveValue::Bool),
583 reader,
584 dst,
585 )
586 }
587 }
588 }
589}
590
591pub(crate) struct Array<T> {
592 _marker: core::marker::PhantomData<T>,
593}
594
595unsafe impl<'de, C: ConfigCore> SchemaReadContext<'de, C, (Len, PrimitiveTy)>
596 for Array<PrimitiveValue>
597{
598 type Dst = Vec<PrimitiveValue>;
599
600 #[inline]
601 fn read_with_context(
602 ctx: (Len, PrimitiveTy),
603 reader: impl Reader<'de>,
604 dst: &mut MaybeUninit<Self::Dst>,
605 ) -> ReadResult<()> {
606 let (Len(len), ty) = ctx;
607
608 match ty {
609 PrimitiveTy::U8 => {
610 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
611 LenMap::new(len, PrimitiveValue::U8),
612 reader,
613 dst,
614 )
615 }
616 PrimitiveTy::U16 => {
617 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
618 LenMap::new(len, PrimitiveValue::U16),
619 reader,
620 dst,
621 )
622 }
623 PrimitiveTy::U32 => {
624 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
625 LenMap::new(len, PrimitiveValue::U32),
626 reader,
627 dst,
628 )
629 }
630 PrimitiveTy::U64 => {
631 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
632 LenMap::new(len, PrimitiveValue::U64),
633 reader,
634 dst,
635 )
636 }
637 PrimitiveTy::I8 => {
638 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
639 LenMap::new(len, PrimitiveValue::I8),
640 reader,
641 dst,
642 )
643 }
644 PrimitiveTy::I16 => {
645 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
646 LenMap::new(len, PrimitiveValue::I16),
647 reader,
648 dst,
649 )
650 }
651 PrimitiveTy::I32 => {
652 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
653 LenMap::new(len, PrimitiveValue::I32),
654 reader,
655 dst,
656 )
657 }
658 PrimitiveTy::I64 => {
659 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
660 LenMap::new(len, PrimitiveValue::I64),
661 reader,
662 dst,
663 )
664 }
665 PrimitiveTy::F32 => {
666 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
667 LenMap::new(len, PrimitiveValue::F32),
668 reader,
669 dst,
670 )
671 }
672 PrimitiveTy::F64 => {
673 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
674 LenMap::new(len, PrimitiveValue::F64),
675 reader,
676 dst,
677 )
678 }
679 PrimitiveTy::Bool => {
680 <Vec<PrimitiveValue> as SchemaReadContext<DefaultConfig, _>>::read_with_context(
681 LenMap::new(len, PrimitiveValue::Bool),
682 reader,
683 dst,
684 )
685 }
686 }
687 }
688}