Coverage Report

Created: 2026-08-31 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/prost/src/lib.rs
Line
Count
Source
1
#![doc(html_root_url = "https://docs.rs/prost/0.7.0")]
2
#![cfg_attr(not(feature = "std"), no_std)]
3
4
// Re-export the alloc crate for use within derived code.
5
#[doc(hidden)]
6
pub extern crate alloc;
7
8
// Re-export the bytes crate for use within derived code.
9
#[doc(hidden)]
10
pub use bytes;
11
12
mod error;
13
mod message;
14
mod types;
15
16
#[doc(hidden)]
17
pub mod encoding;
18
19
pub use crate::error::{DecodeError, EncodeError};
20
pub use crate::message::Message;
21
22
use bytes::{Buf, BufMut};
23
24
use 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"))]
29
const 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.
37
0
pub fn encode_length_delimiter<B>(length: usize, buf: &mut B) -> Result<(), EncodeError>
38
0
where
39
0
    B: BufMut,
40
{
41
0
    let length = length as u64;
42
0
    let required = encoded_len_varint(length);
43
0
    let remaining = buf.remaining_mut();
44
0
    if required > remaining {
45
0
        return Err(EncodeError::new(required, remaining));
46
0
    }
47
0
    encode_varint(length, buf);
48
0
    Ok(())
49
0
}
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.
55
pub 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.
70
0
pub fn decode_length_delimiter<B>(mut buf: B) -> Result<usize, DecodeError>
71
0
where
72
0
    B: Buf,
73
{
74
0
    let length = decode_varint(&mut buf)?;
75
0
    if length > usize::max_value() as u64 {
76
0
        return Err(DecodeError::new(
77
0
            "length delimiter exceeds maximum usize value",
78
0
        ));
79
0
    }
80
0
    Ok(length as usize)
81
0
}
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]
90
extern crate prost_derive;
91
#[cfg(feature = "prost-derive")]
92
#[doc(hidden)]
93
pub use prost_derive::*;