Skip to main content

icydb_schema/wire/
mod.rs

1//! Native RPC resource policy; IC Wire owns the codec and envelopes.
2
3use ic_wire::Limits;
4use serde::{Serialize, de::DeserializeOwned};
5
6pub use ic_wire::Error;
7
8/// Maximum admitted payload traversal nodes.
9pub const MAX_NODES: usize = 1_048_576;
10/// Maximum admitted Serde nesting, including endpoint record/container boundaries.
11/// Allows the 64-level schema contract plus enum, collection and reply wrappers.
12pub const MAX_DEPTH: usize = 256;
13/// Conservative allocation allowance for decoded native payloads.
14pub const MAX_HEAP_BYTES: usize = 256 * 1024 * 1024;
15/// Maximum public reply envelope bytes.
16pub const MAX_REPLY_BYTES: usize = 3 * 1024 * 1024;
17/// Maximum owned endpoint request envelope bytes.
18pub const MAX_REQUEST_BYTES: usize = 2 * 1024 * 1024;
19
20/// Admission allowances for payload or complete frame bytes.
21#[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/// Allocation-free sizing policy retaining the same traversal heap allowance.
32///
33/// A size may exceed delivery limits so precommit callers can return the typed
34/// oversized-reply error. No output buffer is allocated under this allowance.
35#[must_use]
36pub const fn sizing_limits() -> Limits {
37    limits(usize::MAX - MAX_HEAP_BYTES)
38}
39
40/// Encode a native payload through IC Wire's bounded codec.
41pub fn encode_one<T: Serialize>(value: T) -> Result<Vec<u8>, Error> {
42    ic_wire::native::encode(&value, limits(MAX_REPLY_BYTES))
43}
44
45/// Decode one complete canonical payload through IC Wire's bounded codec.
46pub fn decode_one<T: Serialize + DeserializeOwned>(bytes: &[u8]) -> Result<T, Error> {
47    ic_wire::native::decode(bytes, limits(MAX_REPLY_BYTES))
48}
49
50/// Measure native payload bytes without allocating output storage.
51pub 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;