prost/
lib.rs

1#![doc(html_root_url = "https://docs.rs/prost/0.11")]
2#![cfg_attr(not(feature = "std"), no_std)]
3#![doc = include_str!("../README.md")]
4
5// Re-export the alloc crate for use within derived code.
6#[doc(hidden)]
7pub extern crate alloc;
8
9// Re-export the bytes crate for use within derived code.
10pub use bytes;
11
12mod error;
13mod message;
14mod types;
15
16#[doc(hidden)]
17pub mod encoding;
18
19pub use crate::error::{DecodeError, EncodeError};
20pub use crate::message::Message;
21
22use bytes::{Buf, BufMut};
23
24use crate::encoding::{decode_varint, encode_varint, encoded_len_varint};
25
26// See `encoding::DecodeContext` for more info.
27// 100 is the default recursion limit in the C++ implementation.
28#[cfg(not(feature = "no-recursion-limit"))]
29const RECURSION_LIMIT: u32 = 100;
30
31/// Encodes a length delimiter to the buffer.
32///
33/// See [Message.encode_length_delimited] for more info.
34///
35/// An error will be returned if the buffer does not have sufficient capacity to encode the
36/// delimiter.
37pub fn encode_length_delimiter<B>(length: usize, buf: &mut B) -> Result<(), EncodeError>
38where
39    B: BufMut,
40{
41    let length = length as u64;
42    let required = encoded_len_varint(length);
43    let remaining = buf.remaining_mut();
44    if required > remaining {
45        return Err(EncodeError::new(required, remaining));
46    }
47    encode_varint(length, buf);
48    Ok(())
49}
50
51/// Returns the encoded length of a length delimiter.
52///
53/// Applications may use this method to ensure sufficient buffer capacity before calling
54/// `encode_length_delimiter`. The returned size will be between 1 and 10, inclusive.
55pub fn length_delimiter_len(length: usize) -> usize {
56    encoded_len_varint(length as u64)
57}
58
59/// Decodes a length delimiter from the buffer.
60///
61/// This method allows the length delimiter to be decoded independently of the message, when the
62/// message is encoded with [Message.encode_length_delimited].
63///
64/// An error may be returned in two cases:
65///
66///  * If the supplied buffer contains fewer than 10 bytes, then an error indicates that more
67///    input is required to decode the full delimiter.
68///  * If the supplied buffer contains more than 10 bytes, then the buffer contains an invalid
69///    delimiter, and typically the buffer should be considered corrupt.
70pub fn decode_length_delimiter<B>(mut buf: B) -> Result<usize, DecodeError>
71where
72    B: Buf,
73{
74    let length = decode_varint(&mut buf)?;
75    if length > usize::max_value() as u64 {
76        return Err(DecodeError::new(
77            "length delimiter exceeds maximum usize value",
78        ));
79    }
80    Ok(length as usize)
81}
82
83// Re-export #[derive(Message, Enumeration, Oneof)].
84// Based on serde's equivalent re-export [1], but enabled by default.
85//
86// [1]: https://github.com/serde-rs/serde/blob/v1.0.89/serde/src/lib.rs#L245-L256
87#[cfg(feature = "prost-derive")]
88#[allow(unused_imports)]
89#[macro_use]
90extern crate prost_derive;
91#[cfg(feature = "prost-derive")]
92#[doc(hidden)]
93pub use prost_derive::*;