#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![deny(rustdoc::broken_intra_doc_links)]
#[doc(hidden)]
pub extern crate alloc;
#[doc(hidden)]
pub use ::bytes;
#[cfg(feature = "json")]
#[doc(hidden)]
pub use ::serde_json;
#[macro_export]
macro_rules! include_proto {
($pkg:literal) => {
include!(concat!(env!("OUT_DIR"), "/", $pkg, ".mod.rs"));
};
}
#[macro_export]
macro_rules! include_proto_relative {
($dir:literal, $pkg:literal) => {
include!(concat!($dir, "/", $pkg, ".mod.rs"));
};
}
#[cfg(test)]
pub(crate) fn test_ctx(depth: u32) -> DecodeContext<'static> {
let limit = alloc::boxed::Box::leak(alloc::boxed::Box::new(core::cell::Cell::new(
DEFAULT_UNKNOWN_FIELD_LIMIT,
)));
DecodeContext::new(depth, limit)
}
#[cfg(feature = "json")]
pub mod any_registry;
pub mod editions;
pub mod encode_sink;
pub mod encoding;
pub mod enumeration;
pub mod error;
pub mod extension;
#[cfg(feature = "json")]
pub mod extension_registry;
#[cfg(feature = "json")]
pub mod json;
#[cfg(feature = "json")]
pub mod json_helpers;
#[doc(hidden)]
pub mod map_codec;
pub mod message;
pub mod message_field;
pub mod message_set;
pub mod oneof;
mod size_cache;
#[doc(hidden)]
pub mod table;
#[cfg(test)]
pub(crate) mod test_doubles;
#[cfg(feature = "text")]
pub mod text;
#[cfg(any(feature = "json", feature = "text"))]
pub mod type_registry;
pub mod types;
pub mod unknown_fields;
pub mod view;
pub use encode_sink::{EncodeSink, Rope, RopeBuf, DEFAULT_MIN_SEGMENT};
pub use enumeration::{EnumValue, Enumeration};
pub use error::{DecodeError, EncodeError};
pub use extension::{Extension, ExtensionCodec, ExtensionSet};
pub use foldhash;
pub use map_codec::{Map, MapStorage};
pub use message::{
checked_encode_size, saturate_size, DecodeContext, DecodeOptions, Message, MessageName,
DEFAULT_ELEMENT_MEMORY_LIMIT, DEFAULT_UNKNOWN_FIELD_LIMIT, MAX_MESSAGE_BYTES, RECURSION_LIMIT,
};
#[doc(hidden)]
pub use message::{debug_assert_two_pass, encode_size_overflow};
pub use message_field::{DefaultInstance, Inline, MessageField, ProtoBox};
pub use oneof::Oneof;
pub use size_cache::{SizeCache, SizeCachePool};
pub use types::{AsSharedBytes, FieldPayload, ProtoBytes, ProtoList, ProtoString, WirePayload};
pub use unknown_fields::{UnknownField, UnknownFieldData, UnknownFields};
#[cfg(feature = "text")]
pub use text::TextFormat;
pub use view::{
DefaultViewInstance, HasMessageView, LazyMessageFieldView, LazyMessageView, LazyRepeatedView,
MapView, MessageFieldView, MessageView, OwnedView, RepeatedView, UnknownFieldsView, ViewEncode,
ViewLifetimeParametric, ViewReborrow,
};
#[doc(hidden)]
pub mod __private {
#[cfg(feature = "json")]
#[must_use]
pub fn strict_any_type_urls() -> bool {
crate::json::strict_any_type_urls()
}
#[inline(always)]
#[must_use]
pub fn element_footprint<T>(_element: &T) -> usize {
core::mem::size_of::<T>()
}
pub use once_cell::race::OnceBox;
#[cfg(not(feature = "std"))]
pub type HashMap<K, V> = hashbrown::HashMap<K, V>;
#[cfg(feature = "std")]
pub type HashMap<K, V> = std::collections::HashMap<K, V, foldhash::fast::RandomState>;
#[cfg(feature = "arbitrary")]
pub fn arbitrary_proto_string<S: crate::ProtoString>(
u: &mut ::arbitrary::Unstructured<'_>,
) -> ::arbitrary::Result<S> {
let s: ::alloc::string::String = ::arbitrary::Arbitrary::arbitrary(u)?;
Ok(S::from(s))
}
#[cfg(feature = "arbitrary")]
pub fn arbitrary_proto_string_opt<S: crate::ProtoString>(
u: &mut ::arbitrary::Unstructured<'_>,
) -> ::arbitrary::Result<::core::option::Option<S>> {
let opt: ::core::option::Option<::alloc::string::String> =
::arbitrary::Arbitrary::arbitrary(u)?;
Ok(opt.map(S::from))
}
#[cfg(feature = "arbitrary")]
pub fn arbitrary_proto_string_vec<S, L>(
u: &mut ::arbitrary::Unstructured<'_>,
) -> ::arbitrary::Result<L>
where
S: crate::ProtoString,
L: ::core::ops::Deref<Target = [S]> + ::core::convert::From<::alloc::vec::Vec<S>>,
{
let vv: ::alloc::vec::Vec<::alloc::string::String> = ::arbitrary::Arbitrary::arbitrary(u)?;
Ok(L::from(vv.into_iter().map(S::from).collect()))
}
#[cfg(feature = "arbitrary")]
pub fn arbitrary_proto_bytes<B: crate::ProtoBytes>(
u: &mut ::arbitrary::Unstructured<'_>,
) -> ::arbitrary::Result<B> {
let v: ::alloc::vec::Vec<u8> = ::arbitrary::Arbitrary::arbitrary(u)?;
Ok(B::from(v))
}
#[cfg(feature = "arbitrary")]
pub fn arbitrary_proto_bytes_opt<B: crate::ProtoBytes>(
u: &mut ::arbitrary::Unstructured<'_>,
) -> ::arbitrary::Result<::core::option::Option<B>> {
let opt: ::core::option::Option<::alloc::vec::Vec<u8>> =
::arbitrary::Arbitrary::arbitrary(u)?;
Ok(opt.map(B::from))
}
#[cfg(feature = "arbitrary")]
pub fn arbitrary_proto_bytes_vec<B, L>(
u: &mut ::arbitrary::Unstructured<'_>,
) -> ::arbitrary::Result<L>
where
B: crate::ProtoBytes,
L: ::core::ops::Deref<Target = [B]> + ::core::convert::From<::alloc::vec::Vec<B>>,
{
let vv: ::alloc::vec::Vec<::alloc::vec::Vec<u8>> = ::arbitrary::Arbitrary::arbitrary(u)?;
Ok(L::from(vv.into_iter().map(B::from).collect()))
}
#[cfg(feature = "arbitrary")]
fn arbitrary_map_from<'a, C, K, V>(
u: &mut ::arbitrary::Unstructured<'a>,
) -> ::arbitrary::Result<C>
where
C: crate::map_codec::MapStorage + Default,
C::Key: ::core::convert::From<K>,
C::Value: ::core::convert::From<V>,
K: ::arbitrary::Arbitrary<'a>,
V: ::arbitrary::Arbitrary<'a>,
{
let mut out = C::default();
for entry in u.arbitrary_iter::<(K, V)>()? {
let (key, value) = entry?;
out.storage_insert(key.into(), value.into());
}
Ok(out)
}
#[cfg(feature = "arbitrary")]
pub fn arbitrary_proto_bytes_map<'a, C>(
u: &mut ::arbitrary::Unstructured<'a>,
) -> ::arbitrary::Result<C>
where
C: crate::map_codec::MapStorage + Default,
C::Key: ::arbitrary::Arbitrary<'a>,
C::Value: crate::ProtoBytes,
{
arbitrary_map_from::<C, C::Key, ::alloc::vec::Vec<u8>>(u)
}
#[cfg(feature = "arbitrary")]
pub fn arbitrary_proto_string_map_key<'a, C>(
u: &mut ::arbitrary::Unstructured<'a>,
) -> ::arbitrary::Result<C>
where
C: crate::map_codec::MapStorage + Default,
C::Key: crate::ProtoString,
C::Value: ::arbitrary::Arbitrary<'a>,
{
arbitrary_map_from::<C, ::alloc::string::String, C::Value>(u)
}
#[cfg(feature = "arbitrary")]
pub fn arbitrary_proto_string_map_value<'a, C>(
u: &mut ::arbitrary::Unstructured<'a>,
) -> ::arbitrary::Result<C>
where
C: crate::map_codec::MapStorage + Default,
C::Key: ::arbitrary::Arbitrary<'a>,
C::Value: crate::ProtoString,
{
arbitrary_map_from::<C, C::Key, ::alloc::string::String>(u)
}
#[cfg(feature = "arbitrary")]
pub fn arbitrary_proto_string_map<'a, C>(
u: &mut ::arbitrary::Unstructured<'a>,
) -> ::arbitrary::Result<C>
where
C: crate::map_codec::MapStorage + Default,
C::Key: crate::ProtoString,
C::Value: crate::ProtoString,
{
arbitrary_map_from::<C, ::alloc::string::String, ::alloc::string::String>(u)
}
#[cfg(feature = "arbitrary")]
pub fn arbitrary_proto_string_bytes_map<'a, C>(
u: &mut ::arbitrary::Unstructured<'a>,
) -> ::arbitrary::Result<C>
where
C: crate::map_codec::MapStorage + Default,
C::Key: crate::ProtoString,
C::Value: crate::ProtoBytes,
{
arbitrary_map_from::<C, ::alloc::string::String, ::alloc::vec::Vec<u8>>(u)
}
}
#[cfg(all(test, feature = "arbitrary"))]
mod arbitrary_tests {
use super::__private::{
arbitrary_proto_bytes, arbitrary_proto_bytes_map, arbitrary_proto_bytes_opt,
arbitrary_proto_bytes_vec, arbitrary_proto_string_bytes_map, arbitrary_proto_string_map,
arbitrary_proto_string_map_key, arbitrary_proto_string_map_value, HashMap,
};
use ::bytes::Bytes;
use alloc::{string::String, vec::Vec};
use arbitrary::{Arbitrary, Unstructured};
const SEED: [u8; 128] = {
let mut a = [0u8; 128];
let mut i = 0;
while i < 128 {
a[i] = (i as u8).wrapping_mul(31).wrapping_add(7);
i += 1;
}
a
};
#[test]
fn arbitrary_proto_bytes_matches_vec_u8() {
let b: Bytes = arbitrary_proto_bytes(&mut Unstructured::new(&SEED)).unwrap();
let v: Vec<u8> = Arbitrary::arbitrary(&mut Unstructured::new(&SEED)).unwrap();
assert_eq!(b.as_ref(), v.as_slice());
assert!(b.len() <= SEED.len());
}
#[test]
fn arbitrary_proto_bytes_opt_matches_option_vec_u8() {
let b: Option<Bytes> = arbitrary_proto_bytes_opt(&mut Unstructured::new(&SEED)).unwrap();
let v: Option<Vec<u8>> = Arbitrary::arbitrary(&mut Unstructured::new(&SEED)).unwrap();
assert_eq!(b.is_some(), v.is_some());
assert_eq!(
b.as_ref().map(|x| x.to_vec()),
v,
"Option<Bytes> builder must mirror Option<Vec<u8>>"
);
}
#[test]
fn arbitrary_proto_bytes_vec_matches_vec_vec_u8() {
let bs: Vec<Bytes> = arbitrary_proto_bytes_vec(&mut Unstructured::new(&SEED)).unwrap();
let vs: Vec<Vec<u8>> = Arbitrary::arbitrary(&mut Unstructured::new(&SEED)).unwrap();
assert_eq!(bs.len(), vs.len());
for (b, v) in bs.iter().zip(&vs) {
assert_eq!(b.as_ref(), v.as_slice());
}
}
#[derive(Debug, PartialEq)]
struct List<T>(Vec<T>);
impl<T> core::ops::Deref for List<T> {
type Target = [T];
fn deref(&self) -> &[T] {
&self.0
}
}
impl<T> From<Vec<T>> for List<T> {
fn from(v: Vec<T>) -> Self {
List(v)
}
}
#[test]
fn arbitrary_proto_vec_builders_fill_a_custom_collection() {
use super::__private::arbitrary_proto_string_vec;
let bs: List<Bytes> = arbitrary_proto_bytes_vec(&mut Unstructured::new(&SEED)).unwrap();
let vs: Vec<Vec<u8>> = Arbitrary::arbitrary(&mut Unstructured::new(&SEED)).unwrap();
assert!(!vs.is_empty(), "the seed must produce elements");
assert_eq!(bs.iter().map(|b| b.to_vec()).collect::<Vec<_>>(), vs);
let ss: List<String> = arbitrary_proto_string_vec(&mut Unstructured::new(&SEED)).unwrap();
let vs: Vec<String> = Arbitrary::arbitrary(&mut Unstructured::new(&SEED)).unwrap();
assert!(!vs.is_empty(), "the seed must produce elements");
assert_eq!(ss, List(vs));
}
#[test]
fn arbitrary_proto_string_matches_string() {
use super::__private::arbitrary_proto_string;
let s: String = arbitrary_proto_string(&mut Unstructured::new(&SEED)).unwrap();
let v: String = Arbitrary::arbitrary(&mut Unstructured::new(&SEED)).unwrap();
assert_eq!(s, v);
}
fn native_map<K, V>() -> HashMap<K, V>
where
K: for<'a> Arbitrary<'a> + Eq + core::hash::Hash,
V: for<'a> Arbitrary<'a>,
{
let native: HashMap<K, V> = Arbitrary::arbitrary(&mut Unstructured::new(&SEED)).unwrap();
assert!(!native.is_empty(), "the seed must produce map entries");
native
}
#[test]
fn arbitrary_proto_string_map_key_matches_native_map() {
let mapped: HashMap<String, u32> =
arbitrary_proto_string_map_key(&mut Unstructured::new(&SEED)).unwrap();
assert_eq!(mapped, native_map::<String, u32>());
}
#[test]
fn arbitrary_proto_string_map_value_matches_native_map() {
let mapped: HashMap<u32, String> =
arbitrary_proto_string_map_value(&mut Unstructured::new(&SEED)).unwrap();
assert_eq!(mapped, native_map::<u32, String>());
}
#[test]
fn arbitrary_proto_string_map_matches_native_map() {
let mapped: HashMap<String, String> =
arbitrary_proto_string_map(&mut Unstructured::new(&SEED)).unwrap();
assert_eq!(mapped, native_map::<String, String>());
}
#[test]
fn arbitrary_proto_string_map_key_fills_a_btree_map() {
use alloc::collections::BTreeMap;
let mapped: BTreeMap<String, u32> =
arbitrary_proto_string_map_key(&mut Unstructured::new(&SEED)).unwrap();
let native = native_map::<String, u32>();
assert_eq!(mapped, native.into_iter().collect());
}
#[derive(Default)]
struct PairList(Vec<(String, u32)>);
impl crate::map_codec::MapStorage for PairList {
type Key = String;
type Value = u32;
fn storage_len(&self) -> usize {
self.0.len()
}
fn storage_insert(&mut self, key: String, value: u32) {
self.0.retain(|(k, _)| *k != key);
self.0.push((key, value));
}
fn storage_clear(&mut self) {
self.0.clear();
}
fn storage_iter<'a>(&'a self) -> impl Iterator<Item = (&'a String, &'a u32)>
where
String: 'a,
u32: 'a,
{
self.0.iter().map(|(k, v)| (k, v))
}
}
#[test]
fn arbitrary_proto_string_map_key_fills_a_container_without_arbitrary() {
let mapped: PairList =
arbitrary_proto_string_map_key(&mut Unstructured::new(&SEED)).unwrap();
let mapped: HashMap<String, u32> = mapped.0.into_iter().collect();
assert_eq!(mapped, native_map::<String, u32>());
}
fn to_vec_values<K: Eq + core::hash::Hash>(map: HashMap<K, Bytes>) -> HashMap<K, Vec<u8>> {
map.into_iter().map(|(k, v)| (k, v.to_vec())).collect()
}
#[test]
fn arbitrary_proto_bytes_map_matches_native_map() {
let mapped: HashMap<u32, Bytes> =
arbitrary_proto_bytes_map(&mut Unstructured::new(&SEED)).unwrap();
assert_eq!(to_vec_values(mapped), native_map::<u32, Vec<u8>>());
}
#[test]
fn arbitrary_proto_string_bytes_map_matches_native_map() {
let mapped: HashMap<String, Bytes> =
arbitrary_proto_string_bytes_map(&mut Unstructured::new(&SEED)).unwrap();
assert_eq!(to_vec_values(mapped), native_map::<String, Vec<u8>>());
}
}
#[doc(hidden)]
pub mod __doctest_fixtures {
use crate::*;
#[derive(Clone, Default, PartialEq)]
pub struct Person {
pub name: alloc::string::String,
pub id: i32,
}
impl DefaultInstance for Person {
fn default_instance() -> &'static Self {
static INST: __private::OnceBox<Person> = __private::OnceBox::new();
INST.get_or_init(|| alloc::boxed::Box::new(Self::default()))
}
}
impl Message for Person {
fn compute_size(&self, _cache: &mut SizeCache) -> u32 {
0
}
fn write_to(&self, _cache: &mut SizeCache, _buf: &mut impl crate::EncodeSink) {}
fn merge_field(
&mut self,
tag: crate::encoding::Tag,
buf: &mut impl bytes::Buf,
_ctx: DecodeContext<'_>,
) -> Result<(), DecodeError> {
crate::encoding::skip_field(tag, buf)
}
fn clear(&mut self) {
*self = Self::default();
}
}
#[derive(Clone, Debug, Default)]
pub struct PersonView<'a> {
pub name: &'a str,
pub id: i32,
}
impl<'a> view::MessageView<'a> for PersonView<'a> {
type Owned = Person;
fn decode_view(_buf: &'a [u8]) -> Result<Self, DecodeError> {
Ok(PersonView::default())
}
fn merge_view_field(
&mut self,
_tag: encoding::Tag,
cur: &'a [u8],
_before_tag: &'a [u8],
_ctx: DecodeContext<'_>,
) -> Result<&'a [u8], DecodeError> {
Ok(cur)
}
fn to_owned_message(&self) -> Result<Person, DecodeError> {
Ok(Person {
name: self.name.into(),
id: self.id,
})
}
}
impl view::ViewReborrow for PersonView<'static> {
type Reborrowed<'b> = PersonView<'b>;
fn reborrow<'b>(this: &'b Self) -> &'b Self::Reborrowed<'b> {
this
}
}
unsafe impl view::ViewLifetimeParametric for PersonView<'static> {}
}
#[cfg(feature = "zeroize")]
pub mod sensitive;
mod erasing;