numbat_codec/
top_ser_output.rs

1use crate::num_conv::top_encode_number_to_output;
2use alloc::vec::Vec;
3
4/// Specifies objects that can receive the result of a TopEncode computation.
5
6/// in principle from NestedEncode performed on nested items.
7///
8/// All methods consume the object, so they can only be called once.
9///
10/// The trait is used in 3 scenarios:
11/// - SC results
12/// - `#[storage_set(...)]`
13/// - Serialize async call.
14pub trait TopEncodeOutput: Sized {
15	fn set_slice_u8(self, bytes: &[u8]);
16
17	fn set_u64(self, value: u64) {
18		let mut buffer = Vec::<u8>::with_capacity(8);
19		top_encode_number_to_output(&mut buffer, value, false);
20		self.set_slice_u8(&buffer[..]);
21	}
22
23	fn set_i64(self, value: i64) {
24		let mut buffer = Vec::<u8>::with_capacity(8);
25		top_encode_number_to_output(&mut buffer, value as u64, true);
26		self.set_slice_u8(&buffer[..]);
27	}
28
29	/// The unit type `()` is serializable, but some TopEncodeOutput implementations might want to treat it differently.
30	/// For instance, SC function result units do not cause `finish` to be called, no empty result produced.
31	#[doc(hidden)]
32	#[inline]
33	fn set_unit(self) {
34		self.set_slice_u8(&[]);
35	}
36
37	/// Unless you're developing numbat-wasm, please ignore.
38	///
39	/// Shortcut for sending a BigInt managed by the API to the API directly via its handle.
40	///
41	/// - AndesBigInt + finish API
42	/// - AndesBigInt + set storage
43	/// Not used for:
44	/// - RustBigInt
45	/// - async call
46	///
47	/// Note: The byte representation is required as a lambda, so it is computed lazily.
48	/// It should not be computed whenever the handle is present.
49	#[doc(hidden)]
50	#[inline]
51	fn set_big_int_handle_or_bytes<F: FnOnce() -> Vec<u8>>(self, _handle: i32, else_bytes: F) {
52		self.set_slice_u8(else_bytes().as_slice());
53	}
54
55	/// Unless you're developing numbat-wasm, please ignore.
56	///
57	/// Shortcut for sending a BigUint managed by the API to the API directly via its handle.
58	///
59	/// Used for:
60	/// - AndesBigUint + finish API
61	/// - AndesBigUint + set storage
62	/// Not used for:
63	/// - RustBigUint
64	/// - async call
65	/// - anything else
66	///
67	/// Note: The byte representation is required as a lambda, so it is computed lazily.
68	/// It should not be computed whenever the handle is present.
69	#[doc(hidden)]
70	#[inline]
71	fn set_big_uint_handle_or_bytes<F: FnOnce() -> Vec<u8>>(self, _handle: i32, else_bytes: F) {
72		self.set_slice_u8(else_bytes().as_slice());
73	}
74}
75
76impl TopEncodeOutput for &mut Vec<u8> {
77	fn set_slice_u8(self, bytes: &[u8]) {
78		self.extend_from_slice(bytes);
79	}
80}