pilota 0.13.1

Pilota is a thrift and protobuf implementation in pure rust with high performance and extensibility.
Documentation
// Re-export the bytes crate for use within derived code.
pub use bytes;

mod error;
mod message;
mod types;

pub mod descriptor_getter;
#[doc(hidden)]
pub mod encoding;
pub mod extension;

use bytes::{BufMut, Bytes};
pub use encoding::{DecodeContext, EncodeLengthContext};
use encoding::{decode_varint, encode_varint, encoded_len_varint};
pub use error::{DecodeError, EncodeError};
pub use linkedbytes::LinkedBytes;
pub use message::{EnumMessage, Message};
// pb custom options
pub use protobuf::{
    EnumOrUnknown, Message as PbMessage, UnknownValueRef, descriptor, reflect, well_known_types,
};

// See `encoding::DecodeContext` for more info.
// 100 is the default recursion limit in the C++ implementation.
#[cfg(not(feature = "no-recursion-limit"))]
const RECURSION_LIMIT: u32 = 100;

/// Encodes a length delimiter to the buffer.
///
/// See [Message.encode_length_delimited] for more info.
///
/// An error will be returned if the buffer does not have sufficient capacity to
/// encode the delimiter.
pub fn encode_length_delimiter(length: usize, buf: &mut LinkedBytes) -> Result<(), EncodeError> {
    let length = length as u64;
    let required = encoded_len_varint(length);
    let remaining = buf.remaining_mut();
    if required > remaining {
        return Err(EncodeError::new(required, remaining));
    }
    encode_varint(length, buf);
    Ok(())
}

/// Returns the encoded length of a length delimiter.
///
/// Applications may use this method to ensure sufficient buffer capacity before
/// calling `encode_length_delimiter`. The returned size will be between 1 and
/// 10, inclusive.
pub fn length_delimiter_len(length: usize) -> usize {
    encoded_len_varint(length as u64)
}

/// Decodes a length delimiter from the buffer.
///
/// This method allows the length delimiter to be decoded independently of the
/// message, when the message is encoded with [Message.encode_length_delimited].
///
/// An error may be returned in two cases:
///
///  * If the supplied buffer contains fewer than 10 bytes, then an error
///    indicates that more input is required to decode the full delimiter.
///  * If the supplied buffer contains more than 10 bytes, then the buffer
///    contains an invalid delimiter, and typically the buffer should be
///    considered corrupt.
pub fn decode_length_delimiter(mut buf: Bytes) -> Result<usize, DecodeError> {
    let length = decode_varint(&mut buf)?;
    if length > usize::MAX as u64 {
        return Err(DecodeError::new(
            "length delimiter exceeds maximum usize value",
        ));
    }
    Ok(length as usize)
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_length_delimiter_len() {
        let length = length_delimiter_len(10);
        assert_eq!(length, 1);
    }

    #[test]
    fn test_encode_and_decode_length_delimiter() {
        let mut buf = LinkedBytes::new();
        encode_length_delimiter(10, &mut buf).unwrap();
        let length = decode_length_delimiter(buf.bytes().clone().freeze()).unwrap();
        assert_eq!(length, 10);
        assert_eq!(buf.bytes().len(), 1);
    }
}

#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
// According to the benchmark, 1KB is the suitable threshold for zero-copy on
// Apple Silicon.
const ZERO_COPY_THRESHOLD: usize = 1024;

#[cfg(not(all(target_os = "macos", target_arch = "aarch64")))]
// While 4KB is better for other platforms (mainly amd64 linux).
const ZERO_COPY_THRESHOLD: usize = 4 * 1024;