Skip to main content

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