dcrypt_api/traits/serialize.rs
1// File: crates/api/src/traits/serialize.rs
2
3//! Traits for byte serialization of cryptographic types.
4
5use crate::{Result, ZeroizingBytes};
6
7#[cfg(not(feature = "std"))]
8use alloc::vec::Vec;
9#[cfg(feature = "std")]
10use std::vec::Vec;
11
12/// A trait for public types that can be serialized to and from bytes.
13pub trait Serialize: Sized {
14 /// Creates an object from a byte slice.
15 fn from_bytes(bytes: &[u8]) -> Result<Self>;
16 /// Converts the object to a byte vector.
17 fn to_bytes(&self) -> Vec<u8>;
18}
19
20/// A trait for secret types that can be securely serialized.
21pub trait SerializeSecret: Sized {
22 /// Creates an object from a byte slice. Input should be zeroized after use.
23 fn from_bytes(bytes: &[u8]) -> Result<Self>;
24 /// Converts the object to exact-size bytes that are zeroized on drop.
25 fn to_bytes_zeroizing(&self) -> ZeroizingBytes;
26}