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