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<'a> ::buffa::MessageName for AnyView<'a> {
285 const PACKAGE: &'static str = "google.protobuf";
286 const NAME: &'static str = "Any";
287 const FULL_NAME: &'static str = "google.protobuf.Any";
288 const TYPE_URL: &'static str = "type.googleapis.com/google.protobuf.Any";
289}
290impl<'v> ::buffa::DefaultViewInstance for AnyView<'v> {
291 fn default_view_instance<'a>() -> &'a Self
292 where
293 Self: 'a,
294 {
295 static VALUE: ::buffa::__private::OnceBox<AnyView<'static>> = ::buffa::__private::OnceBox::new();
296 VALUE
297 .get_or_init(|| ::buffa::alloc::boxed::Box::new(
298 <AnyView<'static>>::default(),
299 ))
300 }
301}
302impl ::buffa::ViewReborrow for AnyView<'static> {
303 type Reborrowed<'b> = AnyView<'b>;
304 fn reborrow<'b>(this: &'b Self) -> &'b Self::Reborrowed<'b> {
305 this
306 }
307}
308/** Self-contained, `'static` owned view of a `Any` message.
309
310 Wraps [`::buffa::OwnedView`]`<`[`AnyView`]`<'static>>`: the decoded view and the [`::buffa::bytes::Bytes`] buffer it borrows from travel together, so the handle is `'static` and `Send + Sync` — suitable for async handlers, spawned tasks, and anywhere a `'static` bound is required.
311
312 Field accessors return borrows tied to `&self`. Use [`Self::view`] to get the full [`AnyView`] when you need struct patterns, iteration helpers, or to pass the view to lifetime-parameterised code.*/
313#[derive(Clone, Debug)]
314pub struct AnyOwnedView(::buffa::OwnedView<AnyView<'static>>);
315impl AnyOwnedView {
316 /// Decode an owned view from a [`::buffa::bytes::Bytes`] buffer.
317 ///
318 /// The view borrows directly from the buffer's data; the buffer is
319 /// retained inside the returned handle.
320 ///
321 /// # Errors
322 ///
323 /// Returns [`::buffa::DecodeError`] if the buffer contains invalid
324 /// protobuf data.
325 pub fn decode(
326 bytes: ::buffa::bytes::Bytes,
327 ) -> ::core::result::Result<Self, ::buffa::DecodeError> {
328 ::core::result::Result::Ok(AnyOwnedView(::buffa::OwnedView::decode(bytes)?))
329 }
330 /// Decode with custom [`::buffa::DecodeOptions`] (recursion limit,
331 /// max message size).
332 ///
333 /// # Errors
334 ///
335 /// Returns [`::buffa::DecodeError`] if the buffer is invalid or
336 /// exceeds the configured limits.
337 pub fn decode_with_options(
338 bytes: ::buffa::bytes::Bytes,
339 opts: &::buffa::DecodeOptions,
340 ) -> ::core::result::Result<Self, ::buffa::DecodeError> {
341 ::core::result::Result::Ok(
342 AnyOwnedView(::buffa::OwnedView::decode_with_options(bytes, opts)?),
343 )
344 }
345 /// Build from an owned message via an encode → decode round-trip.
346 ///
347 /// # Errors
348 ///
349 /// Returns [`::buffa::DecodeError`] if the re-encoded bytes are
350 /// somehow invalid (should not happen for well-formed messages).
351 pub fn from_owned(
352 msg: &super::super::Any,
353 ) -> ::core::result::Result<Self, ::buffa::DecodeError> {
354 ::core::result::Result::Ok(AnyOwnedView(::buffa::OwnedView::from_owned(msg)?))
355 }
356 /// Borrow the full [`AnyView`] with its lifetime tied to `&self`.
357 #[must_use]
358 pub fn view(&self) -> &AnyView<'_> {
359 self.0.reborrow()
360 }
361 /// Convert to the owned message type.
362 #[must_use]
363 pub fn to_owned_message(&self) -> super::super::Any {
364 self.0.to_owned_message()
365 }
366 /// The underlying bytes buffer.
367 #[must_use]
368 pub fn bytes(&self) -> &::buffa::bytes::Bytes {
369 self.0.bytes()
370 }
371 /// Consume the handle, returning the underlying bytes buffer.
372 #[must_use]
373 pub fn into_bytes(self) -> ::buffa::bytes::Bytes {
374 self.0.into_bytes()
375 }
376 /// A URL/resource name that uniquely identifies the type of the serialized
377 /// protocol buffer message. This string must contain at least
378 /// one "/" character. The last segment of the URL's path must represent
379 /// the fully qualified name of the type (as in
380 /// `path/google.protobuf.Duration`). The name should be in a canonical form
381 /// (e.g., leading "." is not accepted).
382 ///
383 /// In practice, teams usually precompile into the binary all types that they
384 /// expect it to use in the context of Any. However, for URLs which use the
385 /// scheme `http`, `https`, or no scheme, one can optionally set up a type
386 /// server that maps type URLs to message definitions as follows:
387 ///
388 /// * If no scheme is provided, `https` is assumed.
389 /// * An HTTP GET on the URL must yield a \[google.protobuf.Type\]\[\]
390 /// value in binary format, or produce an error.
391 /// * Applications are allowed to cache lookup results based on the
392 /// URL, or have them precompiled into a binary to avoid any
393 /// lookup. Therefore, binary compatibility needs to be preserved
394 /// on changes to types. (Use versioned type names to manage
395 /// breaking changes.)
396 ///
397 /// Note: this functionality is not currently available in the official
398 /// protobuf release, and it is not used for type URLs beginning with
399 /// type.googleapis.com. As of May 2023, there are no widely used type server
400 /// implementations and no plans to implement one.
401 ///
402 /// Schemes other than `http`, `https` (or the empty scheme) might be
403 /// used with implementation specific semantics.
404 ///
405 /// Field 1: `type_url`
406 #[must_use]
407 pub fn type_url(&self) -> &'_ str {
408 self.0.reborrow().type_url
409 }
410 /// Must be a valid serialized protocol buffer of the above specified type.
411 ///
412 /// Field 2: `value`
413 #[must_use]
414 pub fn value(&self) -> &'_ [u8] {
415 self.0.reborrow().value
416 }
417}
418impl ::core::convert::From<::buffa::OwnedView<AnyView<'static>>> for AnyOwnedView {
419 fn from(inner: ::buffa::OwnedView<AnyView<'static>>) -> Self {
420 AnyOwnedView(inner)
421 }
422}
423impl ::core::convert::From<AnyOwnedView> for ::buffa::OwnedView<AnyView<'static>> {
424 fn from(wrapper: AnyOwnedView) -> Self {
425 wrapper.0
426 }
427}
428impl ::core::convert::AsRef<::buffa::OwnedView<AnyView<'static>>> for AnyOwnedView {
429 fn as_ref(&self) -> &::buffa::OwnedView<AnyView<'static>> {
430 &self.0
431 }
432}
433impl ::buffa::HasMessageView for super::super::Any {
434 type View<'a> = AnyView<'a>;
435 type ViewHandle = AnyOwnedView;
436}
437#[cfg(feature = "reflect")]
438const _: () = {
439 impl<'a> ::buffa_descriptor::reflect::ReflectMessage for AnyView<'a> {
440 fn message_descriptor(&self) -> &::buffa_descriptor::MessageDescriptor {
441 super::super::__buffa::reflect::descriptor_pool()
442 .message(Self::__buffa_reflect_message_index())
443 }
444 fn pool(
445 &self,
446 ) -> &::buffa::alloc::sync::Arc<::buffa_descriptor::DescriptorPool> {
447 super::super::__buffa::reflect::descriptor_pool()
448 }
449 fn get(
450 &self,
451 field: &::buffa_descriptor::FieldDescriptor,
452 ) -> ::buffa_descriptor::reflect::ValueRef<'_> {
453 #[allow(unused_imports)]
454 use ::buffa::Enumeration as _;
455 match field.number() {
456 1u32 => ::buffa_descriptor::reflect::ValueRef::String(self.type_url),
457 2u32 => ::buffa_descriptor::reflect::ValueRef::Bytes(self.value),
458 _ => {
459 ::core::debug_assert!(
460 false,
461 "field number {} is not a member of this view's reflect get()",
462 field.number(),
463 );
464 ::buffa_descriptor::reflect::ValueRef::Bool(false)
465 }
466 }
467 }
468 fn has(&self, field: &::buffa_descriptor::FieldDescriptor) -> bool {
469 match field.number() {
470 1u32 => !self.type_url.is_empty(),
471 2u32 => !self.value.is_empty(),
472 _ => false,
473 }
474 }
475 fn for_each_set(
476 &self,
477 f: &mut dyn ::core::ops::FnMut(
478 &::buffa_descriptor::FieldDescriptor,
479 ::buffa_descriptor::reflect::ValueRef<'_>,
480 ),
481 ) {
482 let md = ::buffa_descriptor::reflect::ReflectMessage::message_descriptor(
483 self,
484 );
485 for fd in md.fields() {
486 if ::buffa_descriptor::reflect::ReflectMessage::has(self, fd) {
487 f(fd, ::buffa_descriptor::reflect::ReflectMessage::get(self, fd));
488 }
489 }
490 }
491 fn to_dynamic(&self) -> ::buffa_descriptor::reflect::DynamicMessage {
492 let bytes = ::buffa::ViewEncode::encode_to_vec(self);
493 ::buffa_descriptor::reflect::DynamicMessage::decode(
494 ::buffa::alloc::sync::Arc::clone(
495 super::super::__buffa::reflect::descriptor_pool(),
496 ),
497 Self::__buffa_reflect_message_index(),
498 &bytes,
499 )
500 .expect("view re-encodes to bytes decodable against its own descriptor")
501 }
502 }
503 impl<'a> ::buffa_descriptor::reflect::ReflectElement for AnyView<'a> {
504 fn as_value_ref(&self) -> ::buffa_descriptor::reflect::ValueRef<'_> {
505 ::buffa_descriptor::reflect::ValueRef::Message(
506 ::buffa_descriptor::reflect::ReflectCow::Borrowed(self),
507 )
508 }
509 }
510 impl<'a> AnyView<'a> {
511 /// Memoized `MessageIndex` for this view's message type, resolved
512 /// once against the package's embedded descriptor pool. An inherent
513 /// associated fn (not a free fn) so sibling views in the same module
514 /// do not collide.
515 #[doc(hidden)]
516 fn __buffa_reflect_message_index() -> ::buffa_descriptor::MessageIndex {
517 static IDX: ::std::sync::OnceLock<::buffa_descriptor::MessageIndex> = ::std::sync::OnceLock::new();
518 *IDX
519 .get_or_init(|| {
520 super::super::__buffa::reflect::descriptor_pool()
521 .message_index(<Self as ::buffa::MessageName>::FULL_NAME)
522 .expect(
523 "generated view type is registered in the embedded descriptor pool",
524 )
525 })
526 }
527 }
528};