#[cfg(target_family = "wasm")]
use crate::host_bytes::HostBytes;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Payload {
Bytes(Vec<u8>),
#[cfg(target_family = "wasm")]
Host(HostBytes),
}
impl Payload {
#[must_use]
pub fn len(&self) -> usize {
match self {
Payload::Bytes(bytes) => bytes.len(),
#[cfg(target_family = "wasm")]
Payload::Host(bytes) => bytes.len(),
}
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
#[must_use]
pub fn into_vec(self) -> Vec<u8> {
match self {
Payload::Bytes(bytes) => bytes,
#[cfg(target_family = "wasm")]
Payload::Host(bytes) => bytes.to_vec(),
}
}
}
impl From<Vec<u8>> for Payload {
fn from(bytes: Vec<u8>) -> Self {
Payload::Bytes(bytes)
}
}
pub(crate) const PAYLOAD_MIN: usize = 4096;
#[derive(Debug, Default)]
pub struct Payloads(Vec<Option<Payload>>);
impl Payloads {
#[must_use]
pub fn new() -> Self {
Payloads::default()
}
#[must_use]
pub fn into_vec(self) -> Vec<Payload> {
self.0.into_iter().flatten().collect()
}
}
impl From<Vec<Payload>> for Payloads {
fn from(payloads: Vec<Payload>) -> Self {
Payloads(payloads.into_iter().map(Some).collect())
}
}
impl crate::Context for Payloads {
fn provide<'a>(&'a mut self, request: &mut crate::Request<'a>) {
request.give_mut(self);
}
}
fn write(bytes: &[u8], w: &mut crate::Writer) {
let index = if bytes.len() >= PAYLOAD_MIN {
w.context_mut::<Payloads>().map(|Payloads(payloads)| {
payloads.push(Some(Payload::Bytes(bytes.to_vec())));
payloads.len() - 1
})
} else {
None
};
if let Some(index) = index {
w.push(1);
crate::write_varint(w, index as u64);
} else {
w.push(0);
crate::write_varint(w, bytes.len() as u64);
w.extend_from_slice(bytes);
}
}
fn read_payload(r: &mut crate::Reader<'_>) -> Option<Payload> {
match r.byte()? {
0 => {
let len = r.length()?;
Some(Payload::Bytes(r.take(len)?.to_vec()))
}
1 => {
let index = r.varint()?;
let payload =
usize::try_from(index).ok().and_then(|index| r.context_mut::<Payloads>()?.0.get_mut(index)?.take());
payload.or_else(|| r.fail("a payload the message doesn't have"))
}
_ => r.fail_tag(),
}
}
fn read(r: &mut crate::Reader<'_>) -> Option<Vec<u8>> {
read_payload(r).map(Payload::into_vec)
}
pub mod bytes {
pub fn write(bytes: &[u8], w: &mut crate::Writer) {
super::write(bytes, w);
}
pub fn read(r: &mut crate::Reader<'_>) -> Option<Vec<u8>> {
super::read(r)
}
}
pub mod shared_bytes {
use std::sync::Arc;
pub fn write(bytes: &Arc<Vec<u8>>, w: &mut crate::Writer) {
super::write(bytes, w);
}
pub fn read(r: &mut crate::Reader<'_>) -> Option<Arc<Vec<u8>>> {
super::read(r).map(Arc::new)
}
pub mod option {
use std::sync::Arc;
pub fn write(bytes: &Option<Arc<Vec<u8>>>, w: &mut crate::Writer) {
match bytes {
None => w.push(0),
Some(bytes) => {
w.push(1);
super::write(bytes, w);
}
}
}
pub fn read(r: &mut crate::Reader<'_>) -> Option<Option<Arc<Vec<u8>>>> {
match r.byte()? {
0 => Some(None),
1 => super::read(r).map(Some),
_ => r.fail("invalid option"),
}
}
}
}
pub mod pod {
use super::Payload;
pub fn write<T: bytemuck::Pod>(values: &[T], w: &mut crate::Writer) {
super::write(bytemuck::cast_slice(values), w);
}
pub fn read<T: bytemuck::Pod>(r: &mut crate::Reader<'_>) -> Option<Vec<T>> {
Some(match read_payload::<T>(r)? {
Payload::Bytes(bytes) => bytemuck::pod_collect_to_vec(&bytes),
#[cfg(target_family = "wasm")]
Payload::Host(bytes) => bytes.to_pod_vec(),
})
}
pub fn read_payload<T: bytemuck::Pod>(r: &mut crate::Reader<'_>) -> Option<Payload> {
let bytes = super::read_payload(r)?;
if !bytes.len().is_multiple_of(size_of::<T>()) {
return r.fail("bytes that are not a whole number of values");
}
Some(bytes)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Debug, PartialEq, crate::Wire)]
struct Message {
#[wire(with = "bytes")]
small: Vec<u8>,
#[wire(with = "bytes")]
large: Vec<u8>,
#[wire(with = "pod")]
values: Vec<[u16; 2]>,
}
fn message() -> Message {
Message { small: vec![1, 2, 3], large: vec![7; PAYLOAD_MIN], values: vec![[1, 2]; PAYLOAD_MIN] }
}
#[test]
fn large_arrays_travel_beside_the_message() {
let mut payloads = Payloads::new();
let bytes = crate::to_vec_in(&message(), &mut payloads).unwrap();
let payloads = payloads.into_vec();
assert_eq!(payloads.len(), 2);
assert!(bytes.len() < 32);
let decoded: Message = crate::from_slice_in(&bytes, Payloads::from(payloads)).unwrap();
assert_eq!(decoded, message());
let inline = crate::to_vec(&message()).unwrap();
assert!(inline.len() > 5 * PAYLOAD_MIN);
assert_eq!(crate::from_slice::<Message>(&inline).unwrap(), message());
}
#[test]
fn missing_payloads_are_errors() {
let bytes = crate::to_vec_in(&message(), Payloads::new()).unwrap();
assert!(crate::from_slice_in::<Message>(&bytes, Payloads::new()).is_err());
}
}