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