1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
//! APIs for environments where allocations cannot be made.
//!
//! These APIs require user-provided writers and allocators, and do not support
//! shared pointers.
#[cfg(feature = "bytecheck")]
mod checked;
use rancor::Strategy;
#[cfg(feature = "bytecheck")]
pub use self::checked::*;
use crate::{
access_unchecked,
api::{deserialize_using, serialize_using},
ser::{Allocator, Serializer, Writer},
Archive, Deserialize, Serialize,
};
/// A general-purpose serializer suitable for environments where allocations
/// cannot be made.
///
/// This is part of the [low-level API](crate::api::low).
pub type LowSerializer<W, A, E> = Strategy<Serializer<W, A, ()>, E>;
/// A general-purpose deserializer suitable for environments where allocations
/// cannot be made.
///
/// This is part of the [low-level API](crate::api::low).
pub type LowDeserializer<E> = Strategy<(), E>;
/// Serialize a value using the given allocator and write the bytes to the given
/// writer.
///
/// This is part of the [low-level API](crate::api::low).
///
/// # Example
///
/// ```
/// use core::mem::MaybeUninit;
///
/// use rkyv::{
/// access_unchecked,
/// api::low::to_bytes_in_with_alloc,
/// rancor::Failure,
/// ser::{allocator::SubAllocator, writer::Buffer},
/// util::Align,
/// with::InlineAsBox,
/// Archive, Serialize,
/// };
///
/// let mut output = Align([MaybeUninit::<u8>::uninit(); 256]);
/// let mut alloc = [MaybeUninit::<u8>::uninit(); 256];
///
/// #[derive(Archive, Serialize)]
/// struct Example<'a> {
/// #[rkyv(with = InlineAsBox)]
/// inner: &'a i32,
/// }
///
/// let forty_two = 42;
/// let value = Example { inner: &forty_two };
///
/// let bytes = to_bytes_in_with_alloc::<_, _, Failure>(
/// &value,
/// Buffer::from(&mut *output),
/// SubAllocator::new(&mut alloc),
/// )
/// .unwrap();
///
/// let archived = unsafe { access_unchecked::<ArchivedExample<'_>>(&*bytes) };
/// assert_eq!(*archived.inner, 42);
/// ```
pub fn to_bytes_in_with_alloc<W, A, E>(
value: &impl Serialize<LowSerializer<W, A, E>>,
writer: W,
alloc: A,
) -> Result<W, E>
where
W: Writer<E>,
A: Allocator<E>,
E: rancor::Source,
{
let mut serializer = Serializer::new(writer, alloc, ());
serialize_using(value, &mut serializer)?;
Ok(serializer.into_writer())
}
/// Deserialize a value from the given bytes.
///
/// This function does not check that the data is valid. Use [`from_bytes`] to
/// validate the data instead.
///
/// This is part of the [low-level API](crate::api::low).
///
/// # Safety
///
/// The byte slice must represent a valid archived type when accessed at the
/// default root position. See the [module docs](crate::api) for more
/// information.
///
/// # Example
///
/// ```
/// use core::mem::MaybeUninit;
///
/// use rkyv::{
/// access_unchecked,
/// api::low::{from_bytes_unchecked, to_bytes_in_with_alloc},
/// rancor::Failure,
/// ser::{allocator::SubAllocator, writer::Buffer},
/// util::Align,
/// with::InlineAsBox,
/// Archive, Deserialize, Serialize,
/// };
///
/// let mut output = Align([MaybeUninit::<u8>::uninit(); 256]);
/// let mut alloc = [MaybeUninit::<u8>::uninit(); 256];
///
/// #[derive(Archive, Serialize, Deserialize, Debug, PartialEq)]
/// struct Example {
/// inner: i32,
/// }
///
/// let value = Example { inner: 42 };
///
/// let bytes = to_bytes_in_with_alloc::<_, _, Failure>(
/// &value,
/// Buffer::from(&mut *output),
/// SubAllocator::new(&mut alloc),
/// )
/// .unwrap();
///
/// let deserialized =
/// unsafe { from_bytes_unchecked::<Example, Failure>(&*bytes).unwrap() };
/// assert_eq!(value, deserialized);
/// ```
pub unsafe fn from_bytes_unchecked<T, E>(bytes: &[u8]) -> Result<T, E>
where
T: Archive,
T::Archived: Deserialize<T, LowDeserializer<E>>,
{
// SAFETY: The caller has guaranteed that a valid `T` is located at the root
// position in the byte slice.
let archived = unsafe { access_unchecked::<T::Archived>(bytes) };
deserialize(archived)
}
/// Deserialize a value from the given archived value.
///
/// This is part of the [low-level API](crate::api::low).
///
/// # Example
///
/// ```
/// use core::mem::MaybeUninit;
///
/// use rkyv::{
/// access_unchecked,
/// api::low::{deserialize, to_bytes_in_with_alloc},
/// rancor::Failure,
/// ser::{allocator::SubAllocator, writer::Buffer},
/// util::Align,
/// with::InlineAsBox,
/// Archive, Deserialize, Serialize,
/// };
///
/// let mut output = Align([MaybeUninit::<u8>::uninit(); 256]);
/// let mut alloc = [MaybeUninit::<u8>::uninit(); 256];
///
/// #[derive(Archive, Serialize, Deserialize, Debug, PartialEq)]
/// struct Example {
/// inner: i32,
/// }
///
/// let value = Example { inner: 42 };
///
/// let bytes = to_bytes_in_with_alloc::<_, _, Failure>(
/// &value,
/// Buffer::from(&mut *output),
/// SubAllocator::new(&mut alloc),
/// )
/// .unwrap();
///
/// let archived = unsafe { access_unchecked::<ArchivedExample>(&*bytes) };
/// let deserialized = deserialize::<Example, Failure>(archived).unwrap();
/// assert_eq!(value, deserialized);
/// ```
pub fn deserialize<T, E>(
value: &impl Deserialize<T, LowDeserializer<E>>,
) -> Result<T, E> {
deserialize_using(value, &mut ())
}