1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google LLC. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7
8 //! Kernel-agnostic logic for the Rust Protobuf Runtime.
9 //!
10 //! For kernel-specific logic this crate delegates to the respective `__runtime`
11 //! crate.
12 #![deny(unsafe_op_in_unsafe_fn)]
13
14 use std::fmt;
15
16 // There are a number of manual `Debug` and similar impls instead of using their
17 // derives, in order to to avoid unnecessary bounds on a generic `T`.
18 // This problem is referred to as "perfect derive".
19 // https://smallcultfollowing.com/babysteps/blog/2022/04/12/implied-bounds-and-perfect-derive/
20
21 /// Everything in `__public` is re-exported in `protobuf.rs`.
22 /// These are the items protobuf users can access directly.
23 #[doc(hidden)]
24 pub mod __public {
25 pub use crate::codegen_traits::{
26 create::Parse,
27 interop::{MessageMutInterop, MessageViewInterop, OwnedMessageInterop},
28 read::Serialize,
29 write::{Clear, ClearAndParse, MergeFrom},
30 Message, MessageMut, MessageView,
31 };
32 pub use crate::cord::{ProtoBytesCow, ProtoStringCow};
33 pub use crate::r#enum::{Enum, UnknownEnumValue};
34 pub use crate::map::{Map, MapIter, MapMut, MapView, ProxiedInMapValue};
35 pub use crate::optional::Optional;
36 pub use crate::proto;
37 pub use crate::proxied::{
38 AsMut, AsView, IntoMut, IntoProxied, IntoView, Mut, MutProxied, MutProxy, Proxied, Proxy,
39 View, ViewProxy,
40 };
41 pub use crate::repeated::{
42 ProxiedInRepeated, Repeated, RepeatedIter, RepeatedMut, RepeatedView,
43 };
44 pub use crate::string::{ProtoBytes, ProtoStr, ProtoString};
45 pub use crate::{ParseError, SerializeError};
46 }
47 pub use __public::*;
48
49 pub mod prelude;
50
51 /// Everything in `__internal` is allowed to change without it being considered
52 /// a breaking change for the protobuf library. Nothing in here should be
53 /// exported in `protobuf.rs`.
54 #[path = "internal.rs"]
55 pub mod __internal;
56
57 /// Everything in `__runtime` is allowed to change without it being considered
58 /// a breaking change for the protobuf library. Nothing in here should be
59 /// exported in `protobuf.rs`.
60 #[cfg(all(bzl, cpp_kernel))]
61 #[path = "cpp.rs"]
62 pub mod __runtime;
63 #[cfg(any(not(bzl), upb_kernel))]
64 #[path = "upb.rs"]
65 pub mod __runtime;
66
67 mod codegen_traits;
68 mod cord;
69 #[path = "enum.rs"]
70 mod r#enum;
71 mod map;
72 mod optional;
73 mod primitive;
74 mod proto_macro;
75 mod proxied;
76 mod repeated;
77 mod string;
78
79 #[cfg(not(bzl))]
80 #[path = "upb/lib.rs"]
81 mod upb;
82
83 #[cfg(not(bzl))]
84 mod utf8;
85
86 // Forces the utf8 crate to be accessible from crate::.
87 #[cfg(bzl)]
88 #[allow(clippy::single_component_path_imports)]
89 use utf8;
90
91 // If the Upb and C++ kernels are both linked into the same binary, this symbol
92 // will be defined twice and cause a link error.
93 #[no_mangle]
__Disallow_Upb_And_Cpp_In_Same_Binary()94 extern "C" fn __Disallow_Upb_And_Cpp_In_Same_Binary() {}
95
96 /// An error that happened during parsing.
97 #[derive(Debug, Clone)]
98 pub struct ParseError;
99
100 impl std::error::Error for ParseError {}
101
102 impl fmt::Display for ParseError {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result103 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
104 write!(f, "Couldn't deserialize given bytes into a proto")
105 }
106 }
107
108 /// An error that happened during serialization.
109 #[derive(Debug, Clone)]
110 pub struct SerializeError;
111
112 impl std::error::Error for SerializeError {}
113
114 impl fmt::Display for SerializeError {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result115 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
116 write!(f, "Couldn't serialize proto into bytes (depth too deep or missing required fields)")
117 }
118 }
119
get_repeated_default_value<T: repeated::ProxiedInRepeated + Default>( _: __internal::Private, _: repeated::RepeatedView<'_, T>, ) -> T120 pub fn get_repeated_default_value<T: repeated::ProxiedInRepeated + Default>(
121 _: __internal::Private,
122 _: repeated::RepeatedView<'_, T>,
123 ) -> T {
124 Default::default()
125 }
126