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](crate::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> ::buffa::MessageView<'a> for AnyView<'a> {
141 type Owned = super::super::Any;
142 fn decode_view(buf: &'a [u8]) -> ::core::result::Result<Self, ::buffa::DecodeError> {
143 let __limit = ::core::cell::Cell::new(::buffa::DEFAULT_UNKNOWN_FIELD_LIMIT);
144 let __elem = ::core::cell::Cell::new(::buffa::DEFAULT_ELEMENT_MEMORY_LIMIT);
145 <Self as ::buffa::MessageView>::decode_view_ctx(
146 buf,
147 ::buffa::DecodeContext::new(::buffa::RECURSION_LIMIT, &__limit)
148 .with_element_memory(&__elem),
149 )
150 }
151 fn decode_view_with_ctx(
152 buf: &'a [u8],
153 ctx: ::buffa::DecodeContext<'_>,
154 ) -> ::core::result::Result<Self, ::buffa::DecodeError> {
155 <Self as ::buffa::MessageView>::decode_view_ctx(buf, ctx)
156 }
157 #[inline]
158 fn merge_view_field(
159 &mut self,
160 tag: ::buffa::encoding::Tag,
161 cur: &'a [u8],
162 before_tag: &'a [u8],
163 ctx: ::buffa::DecodeContext<'_>,
164 ) -> ::core::result::Result<&'a [u8], ::buffa::DecodeError> {
165 let _ = ctx;
166 #[allow(unused_variables)]
167 let view = self;
168 let mut cur = cur;
169 match tag.field_number() {
170 1u32 => {
171 ::buffa::encoding::check_wire_type(
172 tag,
173 ::buffa::encoding::WireType::LengthDelimited,
174 )?;
175 view.type_url = ::buffa::types::borrow_str(&mut cur)?;
176 }
177 2u32 => {
178 ::buffa::encoding::check_wire_type(
179 tag,
180 ::buffa::encoding::WireType::LengthDelimited,
181 )?;
182 view.value = ::buffa::types::borrow_bytes(&mut cur)?;
183 }
184 _ => {
185 ::buffa::encoding::skip_field_depth(tag, &mut cur, ctx.depth())?;
186 let span_len = before_tag.len() - cur.len();
187 view.__buffa_unknown_fields.push_record(before_tag, span_len, ctx)?;
188 }
189 }
190 ::core::result::Result::Ok(cur)
191 }
192 fn to_owned_message(
193 &self,
194 ) -> ::core::result::Result<super::super::Any, ::buffa::DecodeError> {
195 self.to_owned_from_source(None)
196 }
197 #[allow(clippy::useless_conversion, clippy::needless_update)]
198 fn to_owned_from_source(
199 &self,
200 __buffa_src: ::core::option::Option<&::buffa::bytes::Bytes>,
201 ) -> ::core::result::Result<super::super::Any, ::buffa::DecodeError> {
202 #[allow(unused_imports)]
203 use ::buffa::alloc::string::ToString as _;
204 let _ = __buffa_src;
205 ::core::result::Result::Ok(super::super::Any {
206 type_url: self.type_url.to_string(),
207 value: ::buffa::view::bytes_from_source(__buffa_src, self.value),
208 __buffa_unknown_fields: self.__buffa_unknown_fields.to_owned()?.into(),
209 ..::core::default::Default::default()
210 })
211 }
212}
213impl<'a> ::buffa::ViewEncode<'a> for AnyView<'a> {
214 #[allow(clippy::needless_borrow, clippy::let_and_return)]
215 fn compute_size(&self, _cache: &mut ::buffa::SizeCache) -> u32 {
216 #[allow(unused_imports)]
217 use ::buffa::Enumeration as _;
218 let mut size = 0u64;
219 if !self.type_url.is_empty() {
220 size += 1u64 + ::buffa::types::string_encoded_len(&self.type_url) as u64;
221 }
222 if !self.value.is_empty() {
223 size += 1u64 + ::buffa::types::bytes_encoded_len(&self.value) as u64;
224 }
225 size += self.__buffa_unknown_fields.encoded_len() as u64;
226 ::buffa::saturate_size(size)
227 }
228 #[allow(clippy::needless_borrow)]
229 fn write_to(
230 &self,
231 _cache: &mut ::buffa::SizeCache,
232 buf: &mut impl ::buffa::EncodeSink,
233 ) {
234 #[allow(unused_imports)]
235 use ::buffa::Enumeration as _;
236 if !self.type_url.is_empty() {
237 ::buffa::types::put_string_field(1u32, &self.type_url, buf);
238 }
239 if !self.value.is_empty() {
240 ::buffa::types::put_shared_bytes_field(2u32, &self.value, buf);
241 }
242 self.__buffa_unknown_fields.write_to(buf);
243 }
244}
245impl<'a> ::buffa::MessageName for AnyView<'a> {
246 const PACKAGE: &'static str = "google.protobuf";
247 const NAME: &'static str = "Any";
248 const FULL_NAME: &'static str = "google.protobuf.Any";
249 const TYPE_URL: &'static str = "type.googleapis.com/google.protobuf.Any";
250}
251::buffa::impl_default_view_instance!(AnyView);
252::buffa::impl_view_reborrow!(AnyView);
253/** Self-contained, `'static` owned view of a `Any` message.
254
255 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.
256
257 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.*/
258#[derive(Clone, Debug)]
259pub struct AnyOwnedView(::buffa::OwnedView<AnyView<'static>>);
260impl AnyOwnedView {
261 /// Decode an owned view from a [`::buffa::bytes::Bytes`] buffer.
262 ///
263 /// The view borrows directly from the buffer's data; the buffer is
264 /// retained inside the returned handle.
265 ///
266 /// # Errors
267 ///
268 /// Returns [`::buffa::DecodeError`] if the buffer contains invalid
269 /// protobuf data.
270 pub fn decode(
271 bytes: ::buffa::bytes::Bytes,
272 ) -> ::core::result::Result<Self, ::buffa::DecodeError> {
273 ::core::result::Result::Ok(AnyOwnedView(::buffa::OwnedView::decode(bytes)?))
274 }
275 /// Decode with custom [`::buffa::DecodeOptions`] (recursion limit,
276 /// max message size).
277 ///
278 /// # Errors
279 ///
280 /// Returns [`::buffa::DecodeError`] if the buffer is invalid or
281 /// exceeds the configured limits.
282 pub fn decode_with_options(
283 bytes: ::buffa::bytes::Bytes,
284 opts: &::buffa::DecodeOptions,
285 ) -> ::core::result::Result<Self, ::buffa::DecodeError> {
286 ::core::result::Result::Ok(
287 AnyOwnedView(::buffa::OwnedView::decode_with_options(bytes, opts)?),
288 )
289 }
290 /// Build from an owned message via an encode → decode round-trip.
291 ///
292 /// # Errors
293 ///
294 /// Returns [`::buffa::DecodeError::MessageTooLarge`] if the
295 /// message's encoded size exceeds the 2 GiB protobuf limit, or
296 /// another [`::buffa::DecodeError`] if the re-encoded bytes are
297 /// somehow invalid (should not happen for well-formed messages).
298 pub fn from_owned(
299 msg: &super::super::Any,
300 ) -> ::core::result::Result<Self, ::buffa::DecodeError> {
301 ::core::result::Result::Ok(AnyOwnedView(::buffa::OwnedView::from_owned(msg)?))
302 }
303 /// Borrow the full [`AnyView`] with its lifetime tied to `&self`.
304 #[must_use]
305 pub fn view(&self) -> &AnyView<'_> {
306 self.0.reborrow()
307 }
308 /// Convert to the owned message type.
309 ///
310 /// Infallible: this type's constructors wire-decode their
311 /// buffer, and a view produced by wire decoding always
312 /// converts. Delegates to [`::buffa::OwnedView::to_owned_message`],
313 /// whose contract also governs handles converted from a raw
314 /// [`::buffa::OwnedView`].
315 #[must_use]
316 pub fn to_owned_message(&self) -> super::super::Any {
317 self.0.to_owned_message()
318 }
319 /// The underlying bytes buffer.
320 #[must_use]
321 pub fn bytes(&self) -> &::buffa::bytes::Bytes {
322 self.0.bytes()
323 }
324 /// Consume the handle, returning the underlying bytes buffer.
325 #[must_use]
326 pub fn into_bytes(self) -> ::buffa::bytes::Bytes {
327 self.0.into_bytes()
328 }
329 /// A URL/resource name that uniquely identifies the type of the serialized
330 /// protocol buffer message. This string must contain at least
331 /// one "/" character. The last segment of the URL's path must represent
332 /// the fully qualified name of the type (as in
333 /// `path/google.protobuf.Duration`). The name should be in a canonical form
334 /// (e.g., leading "." is not accepted).
335 ///
336 /// In practice, teams usually precompile into the binary all types that they
337 /// expect it to use in the context of Any. However, for URLs which use the
338 /// scheme `http`, `https`, or no scheme, one can optionally set up a type
339 /// server that maps type URLs to message definitions as follows:
340 ///
341 /// * If no scheme is provided, `https` is assumed.
342 /// * An HTTP GET on the URL must yield a [google.protobuf.Type](crate::google::protobuf::Type)
343 /// value in binary format, or produce an error.
344 /// * Applications are allowed to cache lookup results based on the
345 /// URL, or have them precompiled into a binary to avoid any
346 /// lookup. Therefore, binary compatibility needs to be preserved
347 /// on changes to types. (Use versioned type names to manage
348 /// breaking changes.)
349 ///
350 /// Note: this functionality is not currently available in the official
351 /// protobuf release, and it is not used for type URLs beginning with
352 /// type.googleapis.com. As of May 2023, there are no widely used type server
353 /// implementations and no plans to implement one.
354 ///
355 /// Schemes other than `http`, `https` (or the empty scheme) might be
356 /// used with implementation specific semantics.
357 ///
358 /// Field 1: `type_url`
359 #[must_use]
360 pub fn type_url(&self) -> &'_ str {
361 self.0.reborrow().type_url
362 }
363 /// Must be a valid serialized protocol buffer of the above specified type.
364 ///
365 /// Field 2: `value`
366 #[must_use]
367 pub fn value(&self) -> &'_ [u8] {
368 self.0.reborrow().value
369 }
370}
371impl ::core::convert::From<::buffa::OwnedView<AnyView<'static>>> for AnyOwnedView {
372 fn from(inner: ::buffa::OwnedView<AnyView<'static>>) -> Self {
373 AnyOwnedView(inner)
374 }
375}
376impl ::core::convert::From<AnyOwnedView> for ::buffa::OwnedView<AnyView<'static>> {
377 fn from(wrapper: AnyOwnedView) -> Self {
378 wrapper.0
379 }
380}
381impl ::core::convert::AsRef<::buffa::OwnedView<AnyView<'static>>> for AnyOwnedView {
382 fn as_ref(&self) -> &::buffa::OwnedView<AnyView<'static>> {
383 &self.0
384 }
385}
386impl ::buffa::HasMessageView for super::super::Any {
387 type View<'a> = AnyView<'a>;
388 type ViewHandle = AnyOwnedView;
389}
390#[cfg(feature = "reflect")]
391const _: () = {
392 impl<'a> ::buffa_descriptor::reflect::ReflectMessage for AnyView<'a> {
393 fn message_descriptor(&self) -> &::buffa_descriptor::MessageDescriptor {
394 super::super::__buffa::reflect::descriptor_pool()
395 .message(Self::__buffa_reflect_message_index())
396 }
397 fn pool(
398 &self,
399 ) -> &::buffa::alloc::sync::Arc<::buffa_descriptor::DescriptorPool> {
400 super::super::__buffa::reflect::descriptor_pool()
401 }
402 fn get(
403 &self,
404 field: &::buffa_descriptor::FieldDescriptor,
405 ) -> ::buffa_descriptor::reflect::ValueRef<'_> {
406 #[allow(unused_imports)]
407 use ::buffa::Enumeration as _;
408 match field.number() {
409 1u32 => ::buffa_descriptor::reflect::ValueRef::String(self.type_url),
410 2u32 => ::buffa_descriptor::reflect::ValueRef::Bytes(self.value),
411 _ => {
412 ::core::debug_assert!(
413 false,
414 "field number {} is not a member of this view's reflect get()",
415 field.number(),
416 );
417 ::buffa_descriptor::reflect::ValueRef::Bool(false)
418 }
419 }
420 }
421 fn has(&self, field: &::buffa_descriptor::FieldDescriptor) -> bool {
422 match field.number() {
423 1u32 => !self.type_url.is_empty(),
424 2u32 => !self.value.is_empty(),
425 _ => false,
426 }
427 }
428 fn for_each_set(
429 &self,
430 f: &mut dyn ::core::ops::FnMut(
431 &::buffa_descriptor::FieldDescriptor,
432 ::buffa_descriptor::reflect::ValueRef<'_>,
433 ),
434 ) {
435 let md = ::buffa_descriptor::reflect::ReflectMessage::message_descriptor(
436 self,
437 );
438 for fd in md.fields() {
439 if ::buffa_descriptor::reflect::ReflectMessage::has(self, fd) {
440 f(fd, ::buffa_descriptor::reflect::ReflectMessage::get(self, fd));
441 }
442 }
443 }
444 fn to_dynamic(&self) -> ::buffa_descriptor::reflect::DynamicMessage {
445 let bytes = ::buffa::ViewEncode::encode_to_vec(self);
446 let options = ::buffa::DecodeOptions::new()
447 .with_element_memory_limit(
448 bytes
449 .len()
450 .saturating_mul(128)
451 .max(::buffa::DEFAULT_ELEMENT_MEMORY_LIMIT),
452 )
453 .with_unknown_field_limit(
454 bytes.len().max(::buffa::DEFAULT_UNKNOWN_FIELD_LIMIT),
455 );
456 ::buffa_descriptor::reflect::DynamicMessage::decode_with_options(
457 ::buffa::alloc::sync::Arc::clone(
458 super::super::__buffa::reflect::descriptor_pool(),
459 ),
460 Self::__buffa_reflect_message_index(),
461 &bytes,
462 &options,
463 )
464 .expect("view re-encodes to bytes decodable against its own descriptor")
465 }
466 }
467 impl<'a> ::buffa_descriptor::reflect::ReflectElement for AnyView<'a> {
468 fn as_value_ref(&self) -> ::buffa_descriptor::reflect::ValueRef<'_> {
469 ::buffa_descriptor::reflect::ValueRef::Message(
470 ::buffa_descriptor::reflect::ReflectCow::Borrowed(self),
471 )
472 }
473 }
474 impl<'a> AnyView<'a> {
475 /// Memoized `MessageIndex` for this view's message type, resolved
476 /// once against the package's embedded descriptor pool. An inherent
477 /// associated fn (not a free fn) so sibling views in the same module
478 /// do not collide.
479 #[doc(hidden)]
480 fn __buffa_reflect_message_index() -> ::buffa_descriptor::MessageIndex {
481 static IDX: ::std::sync::OnceLock<::buffa_descriptor::MessageIndex> = ::std::sync::OnceLock::new();
482 *IDX
483 .get_or_init(|| {
484 super::super::__buffa::reflect::descriptor_pool()
485 .message_index(<Self as ::buffa::MessageName>::FULL_NAME)
486 .expect(
487 "generated view type is registered in the embedded descriptor pool",
488 )
489 })
490 }
491 }
492};