1use ic_wire::Limits;
4use serde::{Serialize, de::DeserializeOwned};
5
6pub use ic_wire::Error;
7
8pub const MAX_NODES: usize = 1_048_576;
10pub const MAX_DEPTH: usize = 256;
13pub const MAX_HEAP_BYTES: usize = 256 * 1024 * 1024;
15pub const MAX_REPLY_BYTES: usize = 3 * 1024 * 1024;
17pub const MAX_REQUEST_BYTES: usize = 2 * 1024 * 1024;
19
20#[must_use]
22pub const fn limits(bytes: usize) -> Limits {
23 Limits {
24 bytes,
25 allocation: MAX_HEAP_BYTES.saturating_add(bytes),
26 nodes: MAX_NODES,
27 depth: MAX_DEPTH,
28 }
29}
30
31#[must_use]
36pub const fn sizing_limits() -> Limits {
37 limits(usize::MAX - MAX_HEAP_BYTES)
38}
39
40pub fn encode_one<T: Serialize>(value: T) -> Result<Vec<u8>, Error> {
42 ic_wire::native::encode(&value, limits(MAX_REPLY_BYTES))
43}
44
45pub fn decode_one<T: Serialize + DeserializeOwned>(bytes: &[u8]) -> Result<T, Error> {
47 ic_wire::native::decode(bytes, limits(MAX_REPLY_BYTES))
48}
49
50pub fn encoded_len<T: Serialize + ?Sized>(value: &T) -> Result<usize, Error> {
52 ic_wire::native::encoded_len(value, sizing_limits())
53}
54
55#[cfg(test)]
56mod tests;