avro_schema/write/
encode.rs

1//! Functions used to encode Avro physical types
2use crate::error::Error;
3
4/// Zigzag encoding of a signed integer.
5#[inline]
6pub fn zigzag_encode<W: std::io::Write>(n: i64, writer: &mut W) -> Result<(), Error> {
7    _zigzag_encode(((n << 1) ^ (n >> 63)) as u64, writer)
8}
9
10#[inline]
11fn _zigzag_encode<W: std::io::Write>(mut z: u64, writer: &mut W) -> Result<(), Error> {
12    loop {
13        if z <= 0x7F {
14            writer.write_all(&[(z & 0x7F) as u8])?;
15            break;
16        } else {
17            writer.write_all(&[(0x80 | (z & 0x7F)) as u8])?;
18            z >>= 7;
19        }
20    }
21    Ok(())
22}
23
24pub(crate) fn write_binary<W: std::io::Write>(bytes: &[u8], writer: &mut W) -> Result<(), Error> {
25    zigzag_encode(bytes.len() as i64, writer)?;
26    writer.write_all(bytes)?;
27    Ok(())
28}