numbat_codec/
top_de_input.rs

1use crate::num_conv::bytes_to_number;
2use crate::transmute::vec_into_boxed_slice;
3use alloc::boxed::Box;
4use alloc::vec::Vec;
5
6/// Trait that abstracts away an underlying API for a top-level object deserializer.
7/// The underlying API can provide pre-parsed i64/u64 or pre-bundled boxed slices.
8pub trait TopDecodeInput: Sized {
9	/// Length of the underlying data, in bytes.
10	fn byte_len(&self) -> usize;
11
12	/// Provides the underlying data as an owned byte slice box.
13	/// Consumes the input object in the process.
14	fn into_boxed_slice_u8(self) -> Box<[u8]>;
15
16	/// Retrieves the underlying data as a pre-parsed u64.
17	/// Expected to panic if the conversion is not possible.
18	///
19	/// Consumes the input object in the process.
20	fn into_u64(self) -> u64 {
21		bytes_to_number(&*self.into_boxed_slice_u8(), false)
22	}
23
24	/// Retrieves the underlying data as a pre-parsed i64.
25	/// Expected to panic if the conversion is not possible.
26	///
27	/// Consumes the input object in the process.
28	fn into_i64(self) -> i64 {
29		bytes_to_number(&*self.into_boxed_slice_u8(), true) as i64
30	}
31
32	/// Unless you're developing numbat-wasm, please ignore.
33	///
34	/// Shortcut for sending a BigInt managed by the API to the API directly via its handle.
35	///
36	/// - AndesBigInt + finish API
37	/// - AndesBigInt + set storage
38	/// Not used for:
39	/// - RustBigInt
40	/// - async call
41	#[doc(hidden)]
42	#[inline]
43	fn try_get_big_int_handle(&self) -> (bool, i32) {
44		(false, -1)
45	}
46
47	/// Unless you're developing numbat-wasm, please ignore.
48	///
49	/// Shortcut for sending a BigUint managed by the API to the API directly via its handle.
50	///
51	/// Used for:
52	/// - AndesBigUint + finish API
53	/// - AndesBigUint + set storage
54	/// Not used for:
55	/// - RustBigUint
56	/// - async call
57	/// - anything else
58	///
59	#[doc(hidden)]
60	#[inline]
61	fn try_get_big_uint_handle(&self) -> (bool, i32) {
62		(false, -1)
63	}
64}
65
66impl TopDecodeInput for Box<[u8]> {
67	fn byte_len(&self) -> usize {
68		self.len()
69	}
70
71	fn into_boxed_slice_u8(self) -> Box<[u8]> {
72		self
73	}
74}
75
76impl TopDecodeInput for Vec<u8> {
77	fn byte_len(&self) -> usize {
78		self.len()
79	}
80
81	fn into_boxed_slice_u8(self) -> Box<[u8]> {
82		vec_into_boxed_slice(self)
83	}
84}
85
86impl<'a> TopDecodeInput for &'a [u8] {
87	fn byte_len(&self) -> usize {
88		self.len()
89	}
90
91	fn into_boxed_slice_u8(self) -> Box<[u8]> {
92		Box::from(self)
93	}
94}