testnumbat_codec/
top_de_input.rs1use crate::{
2 num_conv::bytes_to_number, transmute::vec_into_boxed_slice, DecodeError, NestedDecodeInput,
3 OwnedBytesNestedDecodeInput, TryStaticCast,
4};
5use alloc::{boxed::Box, vec::Vec};
6
7pub trait TopDecodeInput: Sized {
10 type NestedBuffer: NestedDecodeInput;
11
12 fn byte_len(&self) -> usize;
14
15 fn into_boxed_slice_u8(self) -> Box<[u8]>;
18
19 fn into_u64(self) -> u64 {
24 bytes_to_number(&*self.into_boxed_slice_u8(), false)
25 }
26
27 fn into_i64(self) -> i64 {
32 bytes_to_number(&*self.into_boxed_slice_u8(), true) as i64
33 }
34
35 #[inline]
36 fn into_specialized<T, F>(self, else_deser: F) -> Result<T, DecodeError>
37 where
38 T: TryStaticCast,
39 F: FnOnce(Self) -> Result<T, DecodeError>,
40 {
41 else_deser(self)
42 }
43
44 #[inline]
46 fn into_specialized_or_exit<T, F, ExitCtx>(
47 self,
48 c: ExitCtx,
49 exit: fn(ExitCtx, DecodeError) -> !,
50 else_deser: F,
51 ) -> T
52 where
53 T: TryStaticCast,
54 ExitCtx: Clone,
55 F: FnOnce(Self, ExitCtx, fn(ExitCtx, DecodeError) -> !) -> T,
56 {
57 else_deser(self, c, exit)
58 }
59
60 fn into_nested_buffer(self) -> Self::NestedBuffer;
61}
62
63impl TopDecodeInput for Box<[u8]> {
64 type NestedBuffer = OwnedBytesNestedDecodeInput;
65
66 fn byte_len(&self) -> usize {
67 self.len()
68 }
69
70 fn into_boxed_slice_u8(self) -> Box<[u8]> {
71 self
72 }
73
74 fn into_nested_buffer(self) -> Self::NestedBuffer {
75 OwnedBytesNestedDecodeInput::new(self)
76 }
77}
78
79impl TopDecodeInput for Vec<u8> {
80 type NestedBuffer = OwnedBytesNestedDecodeInput;
81
82 fn byte_len(&self) -> usize {
83 self.len()
84 }
85
86 fn into_boxed_slice_u8(self) -> Box<[u8]> {
87 vec_into_boxed_slice(self)
88 }
89
90 fn into_nested_buffer(self) -> Self::NestedBuffer {
91 OwnedBytesNestedDecodeInput::new(self.into_boxed_slice())
92 }
93}
94
95impl<'a> TopDecodeInput for &'a [u8] {
96 type NestedBuffer = &'a [u8];
97
98 fn byte_len(&self) -> usize {
99 self.len()
100 }
101
102 fn into_boxed_slice_u8(self) -> Box<[u8]> {
103 Box::from(self)
104 }
105
106 fn into_nested_buffer(self) -> Self::NestedBuffer {
107 self
108 }
109}