1#![doc = include_str!("../README.md")]
16#![cfg_attr(not(feature = "std"), no_std)]
17
18extern crate alloc;
19
20pub mod deserialize;
21pub mod serialize;
22#[cfg(test)]
23mod serialize_test;
24#[cfg(test)]
25pub mod tests;
26mod util;
27
28pub use deserialize::{Deserialize, DeserializeOwned};
29pub use risc0_zeroio_derive::{Deserialize, Serialize};
30pub use risc0_zkvm_platform::WORD_SIZE;
31pub use serialize::{serialize, Alloc, AllocBuf, Serialize};
32use util::as_words_padded;
33
34pub const PAD_WORDS: usize = 8;
35
36pub type Result<T> = core::result::Result<T, ZeroioError>;
37
38#[derive(Debug)]
39pub enum ZeroioError {
40 AllocationSizeMismatch,
41 FillOverrun,
42}
43
44impl core::fmt::Display for ZeroioError {
45 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
46 match self {
47 ZeroioError::AllocationSizeMismatch => {
48 write!(f, "Allocation mismatch during serialization")
49 }
50 ZeroioError::FillOverrun => write!(f, "Structure filled up more than it allocated"),
51 }
52 }
53}
54
55pub const fn align_bytes_to_words(bytes: usize) -> usize {
56 (bytes + WORD_SIZE - 1) / WORD_SIZE
57}
58
59pub(crate) fn pad_words(words: usize) -> usize {
60 ((words + PAD_WORDS - 1) / PAD_WORDS) * PAD_WORDS
61}
62
63pub fn to_vec<T: Serialize>(val: &T) -> Result<alloc::vec::Vec<u32>> {
64 serialize::serialize(val)
65}
66
67pub fn from_slice<'a, T: Deserialize<'a>>(words: &'a [u32]) -> Result<T::RefType> {
68 Ok(T::deserialize_from(words))
69}