buffa_types/generated/google.protobuf.any.__view.rs
1// @generated by buffa-codegen. DO NOT EDIT.
2// source: google/protobuf/any.proto
3
4/// `Any` contains an arbitrary serialized protocol buffer message along with a
5/// URL that describes the type of the serialized message.
6///
7/// Protobuf library provides support to pack/unpack Any values in the form
8/// of utility functions or additional generated methods of the Any type.
9///
10/// Example 1: Pack and unpack a message in C++.
11///
12/// ```text
13/// Foo foo = ...;
14/// Any any;
15/// any.PackFrom(foo);
16/// ...
17/// if (any.UnpackTo(&foo)) {
18/// ...
19/// }
20/// ```
21///
22/// Example 2: Pack and unpack a message in Java.
23///
24/// ```text
25/// Foo foo = ...;
26/// Any any = Any.pack(foo);
27/// ...
28/// if (any.is(Foo.class)) {
29/// foo = any.unpack(Foo.class);
30/// }
31/// // or ...
32/// if (any.isSameTypeAs(Foo.getDefaultInstance())) {
33/// foo = any.unpack(Foo.getDefaultInstance());
34/// }
35/// ```
36///
37/// Example 3: Pack and unpack a message in Python.
38///
39/// ```text
40/// foo = Foo(...)
41/// any = Any()
42/// any.Pack(foo)
43/// ...
44/// if any.Is(Foo.DESCRIPTOR):
45/// any.Unpack(foo)
46/// ...
47/// ```
48///
49/// Example 4: Pack and unpack a message in Go
50///
51/// ```text
52/// foo := &pb.Foo{...}
53/// any, err := anypb.New(foo)
54/// if err != nil {
55/// ...
56/// }
57/// ...
58/// foo := &pb.Foo{}
59/// if err := any.UnmarshalTo(foo); err != nil {
60/// ...
61/// }
62/// ```
63///
64/// The pack methods provided by protobuf library will by default use
65/// 'type.googleapis.com/full.type.name' as the type URL and the unpack
66/// methods only use the fully qualified type name after the last '/'
67/// in the type URL, for example "foo.bar.com/x/y.z" will yield type
68/// name "y.z".
69///
70/// JSON
71/// ====
72/// The JSON representation of an `Any` value uses the regular
73/// representation of the deserialized, embedded message, with an
74/// additional field `@type` which contains the type URL. Example:
75///
76/// ```text
77/// package google.profile;
78/// message Person {
79/// string first_name = 1;
80/// string last_name = 2;
81/// }
82///
83/// {
84/// "@type": "type.googleapis.com/google.profile.Person",
85/// "firstName": <string>,
86/// "lastName": <string>
87/// }
88/// ```
89///
90/// If the embedded message type is well-known and has a custom JSON
91/// representation, that representation will be embedded adding a field
92/// `value` which holds the custom JSON in addition to the `@type`
93/// field. Example (for message [google.protobuf.Duration](crate::google::protobuf::Duration)):
94///
95/// ```text
96/// {
97/// "@type": "type.googleapis.com/google.protobuf.Duration",
98/// "value": "1.212s"
99/// }
100/// ```
101#[derive(Clone, Debug, Default)]
102pub struct AnyView<'a> {
103 /// A URL/resource name that uniquely identifies the type of the serialized
104 /// protocol buffer message. This string must contain at least
105 /// one "/" character. The last segment of the URL's path must represent
106 /// the fully qualified name of the type (as in
107 /// `path/google.protobuf.Duration`). The name should be in a canonical form
108 /// (e.g., leading "." is not accepted).
109 ///
110 /// In practice, teams usually precompile into the binary all types that they
111 /// expect it to use in the context of Any. However, for URLs which use the
112 /// scheme `http`, `https`, or no scheme, one can optionally set up a type
113 /// server that maps type URLs to message definitions as follows:
114 ///
115 /// * If no scheme is provided, `https` is assumed.
116 /// * An HTTP GET on the URL must yield a \[google.protobuf.Type\]\[\]
117 /// value in binary format, or produce an error.
118 /// * Applications are allowed to cache lookup results based on the
119 /// URL, or have them precompiled into a binary to avoid any
120 /// lookup. Therefore, binary compatibility needs to be preserved
121 /// on changes to types. (Use versioned type names to manage
122 /// breaking changes.)
123 ///
124 /// Note: this functionality is not currently available in the official
125 /// protobuf release, and it is not used for type URLs beginning with
126 /// type.googleapis.com. As of May 2023, there are no widely used type server
127 /// implementations and no plans to implement one.
128 ///
129 /// Schemes other than `http`, `https` (or the empty scheme) might be
130 /// used with implementation specific semantics.
131 ///
132 /// Field 1: `type_url`
133 pub type_url: &'a str,
134 /// Must be a valid serialized protocol buffer of the above specified type.
135 ///
136 /// Field 2: `value`
137 pub value: &'a [u8],
138 pub __buffa_unknown_fields: ::buffa::UnknownFieldsView<'a>,
139}
140impl<'a> AnyView<'a> {
141 /// Decode from `buf`, enforcing a recursion depth limit for nested messages.
142 ///
143 /// Called by [`::buffa::MessageView::decode_view`] with [`::buffa::RECURSION_LIMIT`]
144 /// and by generated sub-message decode arms with `depth - 1`.
145 ///
146 /// **Not part of the public API.** Named with a leading underscore to
147 /// signal that it is for generated-code use only.
148 #[doc(hidden)]
149 pub fn _decode_depth(
150 buf: &'a [u8],
151 depth: u32,
152 ) -> ::core::result::Result<Self, ::buffa::DecodeError> {
153 let mut view = Self::default();
154 view._merge_into_view(buf, depth)?;
155 ::core::result::Result::Ok(view)
156 }
157 /// Merge fields from `buf` into this view (proto merge semantics).
158 ///
159 /// Repeated fields append; singular fields last-wins; singular
160 /// MESSAGE fields merge recursively. Used by sub-message decode
161 /// arms when the same field appears multiple times on the wire.
162 ///
163 /// **Not part of the public API.**
164 #[doc(hidden)]
165 pub fn _merge_into_view(
166 &mut self,
167 buf: &'a [u8],
168 depth: u32,
169 ) -> ::core::result::Result<(), ::buffa::DecodeError> {
170 let _ = depth;
171 #[allow(unused_variables)]
172 let view = self;
173 let mut cur: &'a [u8] = buf;
174 while !cur.is_empty() {
175 let before_tag = cur;
176 let tag = ::buffa::encoding::Tag::decode(&mut cur)?;
177 match tag.field_number() {
178 1u32 => {
179 if tag.wire_type() != ::buffa::encoding::WireType::LengthDelimited {
180 return ::core::result::Result::Err(::buffa::DecodeError::WireTypeMismatch {
181 field_number: 1u32,
182 expected: 2u8,
183 actual: tag.wire_type() as u8,
184 });
185 }
186 view.type_url = ::buffa::types::borrow_str(&mut cur)?;
187 }
188 2u32 => {
189 if tag.wire_type() != ::buffa::encoding::WireType::LengthDelimited {
190 return ::core::result::Result::Err(::buffa::DecodeError::WireTypeMismatch {
191 field_number: 2u32,
192 expected: 2u8,
193 actual: tag.wire_type() as u8,
194 });
195 }
196 view.value = ::buffa::types::borrow_bytes(&mut cur)?;
197 }
198 _ => {
199 ::buffa::encoding::skip_field_depth(tag, &mut cur, depth)?;
200 let span_len = before_tag.len() - cur.len();
201 view.__buffa_unknown_fields.push_raw(&before_tag[..span_len]);
202 }
203 }
204 }
205 ::core::result::Result::Ok(())
206 }
207}
208impl<'a> ::buffa::MessageView<'a> for AnyView<'a> {
209 type Owned = super::super::Any;
210 fn decode_view(buf: &'a [u8]) -> ::core::result::Result<Self, ::buffa::DecodeError> {
211 Self::_decode_depth(buf, ::buffa::RECURSION_LIMIT)
212 }
213 fn decode_view_with_limit(
214 buf: &'a [u8],
215 depth: u32,
216 ) -> ::core::result::Result<Self, ::buffa::DecodeError> {
217 Self::_decode_depth(buf, depth)
218 }
219 fn to_owned_message(&self) -> super::super::Any {
220 self.to_owned_from_source(None)
221 }
222 #[allow(clippy::useless_conversion, clippy::needless_update)]
223 fn to_owned_from_source(
224 &self,
225 __buffa_src: ::core::option::Option<&::buffa::bytes::Bytes>,
226 ) -> super::super::Any {
227 #[allow(unused_imports)]
228 use ::buffa::alloc::string::ToString as _;
229 let _ = __buffa_src;
230 super::super::Any {
231 type_url: self.type_url.to_string(),
232 value: ::buffa::view::bytes_from_source(__buffa_src, self.value),
233 __buffa_unknown_fields: self
234 .__buffa_unknown_fields
235 .to_owned()
236 .unwrap_or_default()
237 .into(),
238 ..::core::default::Default::default()
239 }
240 }
241}
242impl<'a> ::buffa::ViewEncode<'a> for AnyView<'a> {
243 #[allow(clippy::needless_borrow, clippy::let_and_return)]
244 fn compute_size(&self, _cache: &mut ::buffa::SizeCache) -> u32 {
245 #[allow(unused_imports)]
246 use ::buffa::Enumeration as _;
247 let mut size = 0u32;
248 if !self.type_url.is_empty() {
249 size += 1u32 + ::buffa::types::string_encoded_len(&self.type_url) as u32;
250 }
251 if !self.value.is_empty() {
252 size += 1u32 + ::buffa::types::bytes_encoded_len(&self.value) as u32;
253 }
254 size += self.__buffa_unknown_fields.encoded_len() as u32;
255 size
256 }
257 #[allow(clippy::needless_borrow)]
258 fn write_to(
259 &self,
260 _cache: &mut ::buffa::SizeCache,
261 buf: &mut impl ::buffa::bytes::BufMut,
262 ) {
263 #[allow(unused_imports)]
264 use ::buffa::Enumeration as _;
265 if !self.type_url.is_empty() {
266 ::buffa::encoding::Tag::new(
267 1u32,
268 ::buffa::encoding::WireType::LengthDelimited,
269 )
270 .encode(buf);
271 ::buffa::types::encode_string(&self.type_url, buf);
272 }
273 if !self.value.is_empty() {
274 ::buffa::encoding::Tag::new(
275 2u32,
276 ::buffa::encoding::WireType::LengthDelimited,
277 )
278 .encode(buf);
279 ::buffa::types::encode_bytes(&self.value, buf);
280 }
281 self.__buffa_unknown_fields.write_to(buf);
282 }
283}
284impl<'v> ::buffa::DefaultViewInstance for AnyView<'v> {
285 fn default_view_instance<'a>() -> &'a Self
286 where
287 Self: 'a,
288 {
289 static VALUE: ::buffa::__private::OnceBox<AnyView<'static>> = ::buffa::__private::OnceBox::new();
290 VALUE
291 .get_or_init(|| ::buffa::alloc::boxed::Box::new(
292 <AnyView<'static>>::default(),
293 ))
294 }
295}
296impl ::buffa::ViewReborrow for AnyView<'static> {
297 type Reborrowed<'b> = AnyView<'b>;
298 fn reborrow<'b>(this: &'b Self) -> &'b Self::Reborrowed<'b> {
299 this
300 }
301}