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