Skip to main content

google_protobuf/
shared.rs

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#![deny(unsafe_op_in_unsafe_fn)]
9
10use std::fmt;
11
12// There are a number of manual `Debug` and similar impls instead of using their
13// derives, in order to to avoid unnecessary bounds on a generic `T`.
14// This problem is referred to as "perfect derive".
15// https://smallcultfollowing.com/babysteps/blog/2022/04/12/implied-bounds-and-perfect-derive/
16
17#[doc(inline)]
18pub use crate::__internal::runtime::message_eq;
19
20pub use crate::__internal::runtime::interop::*;
21#[cfg(not(lite_runtime))]
22pub use crate::codegen_traits::WithReflection;
23pub use crate::codegen_traits::{
24    create::Parse,
25    read::Serialize,
26    write::{Clear, ClearAndParse, CopyFrom, MergeFrom, TakeFrom},
27    Message, MessageMut, MessageType, MessageView,
28};
29pub use crate::cord::{ProtoBytesCow, ProtoStringCow};
30pub use crate::extension::ExtensionId;
31pub use crate::map::{Map, MapIter, MapKey, MapMut, MapValue, MapView};
32
33pub use crate::proxied::{
34    AsMut, AsView, IntoMut, IntoProxied, IntoView, Mut, MutProtoObject, ProtoObject, View,
35};
36#[allow(deprecated)]
37pub use crate::proxied::{MutProxied, Proxied};
38pub use crate::r#enum::{Enum, UnknownEnumValue};
39pub use crate::repeated::{Repeated, RepeatedIter, RepeatedMut, RepeatedView};
40pub use crate::singular::Singular;
41pub use crate::string::{ProtoBytes, ProtoStr, ProtoString, Utf8Error};
42pub use protobuf_macros::proto_proc as proto;
43
44// TODO: Remove this alias once we have confirmed there are no incoming references.
45pub use Singular as ProxiedInRepeated;
46
47pub mod prelude;
48
49/// The `__internal` module is for necessary encapsulation breaks between
50/// generated code and the runtime.
51///
52/// These symbols are never intended to be used by application code under any
53/// circumstances.
54///
55/// In blaze/bazel builds, this symbol is actively hidden from application
56/// code by having a shim crate in front that does not re-export this symbol,
57/// and a different BUILD visibility-restricted target that is used by the
58/// generated code.
59///
60/// In Cargo builds we have no good way to technically hide this
61/// symbol while still allowing it from codegen, so it is only by private by
62/// convention. As application code should never use this module, anything
63/// changes under `__internal` is not considered a semver breaking change.
64#[path = "internal.rs"]
65pub mod __internal;
66
67mod codegen_traits;
68mod cord;
69#[path = "enum.rs"]
70mod r#enum;
71mod extension;
72mod map;
73mod primitive;
74mod proxied;
75mod repeated;
76mod singular;
77mod string;
78
79#[cfg(not(bzl))]
80#[path = "upb/lib.rs"]
81mod upb;
82
83// If the Upb and C++ kernels are both linked into the same binary, this symbol
84// will be defined twice and cause a link error.
85#[unsafe(no_mangle)]
86unsafe extern "C" fn __Disallow_Upb_And_Cpp_In_Same_Binary() {}
87
88/// An error that happened during parsing.
89#[derive(Debug, Clone)]
90pub struct ParseError;
91
92impl std::error::Error for ParseError {}
93
94impl fmt::Display for ParseError {
95    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
96        write!(f, "Couldn't deserialize given bytes into a proto")
97    }
98}
99
100/// An error that happened during serialization.
101#[derive(Debug, Clone)]
102pub struct SerializeError;
103
104impl std::error::Error for SerializeError {}
105
106impl fmt::Display for SerializeError {
107    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
108        write!(f, "Couldn't serialize proto into bytes (depth too deep or missing required fields)")
109    }
110}