Skip to main content

buffa_types/generated/
google.protobuf.duration.__view.rs

1// @generated by buffa-codegen. DO NOT EDIT.
2// source: google/protobuf/duration.proto
3
4/// A Duration represents a signed, fixed-length span of time represented
5/// as a count of seconds and fractions of seconds at nanosecond
6/// resolution. It is independent of any calendar and concepts like "day"
7/// or "month". It is related to Timestamp in that the difference between
8/// two Timestamp values is a Duration and it can be added or subtracted
9/// from a Timestamp. Range is approximately +-10,000 years.
10///
11/// # Examples
12///
13/// Example 1: Compute Duration from two Timestamps in pseudo code.
14///
15/// ```text
16/// Timestamp start = ...;
17/// Timestamp end = ...;
18/// Duration duration = ...;
19///
20/// duration.seconds = end.seconds - start.seconds;
21/// duration.nanos = end.nanos - start.nanos;
22///
23/// if (duration.seconds < 0 && duration.nanos > 0) {
24///   duration.seconds += 1;
25///   duration.nanos -= 1000000000;
26/// } else if (duration.seconds > 0 && duration.nanos < 0) {
27///   duration.seconds -= 1;
28///   duration.nanos += 1000000000;
29/// }
30/// ```
31///
32/// Example 2: Compute Timestamp from Timestamp + Duration in pseudo code.
33///
34/// ```text
35/// Timestamp start = ...;
36/// Duration duration = ...;
37/// Timestamp end = ...;
38///
39/// end.seconds = start.seconds + duration.seconds;
40/// end.nanos = start.nanos + duration.nanos;
41///
42/// if (end.nanos < 0) {
43///   end.seconds -= 1;
44///   end.nanos += 1000000000;
45/// } else if (end.nanos >= 1000000000) {
46///   end.seconds += 1;
47///   end.nanos -= 1000000000;
48/// }
49/// ```
50///
51/// Example 3: Compute Duration from datetime.timedelta in Python.
52///
53/// ```text
54/// td = datetime.timedelta(days=3, minutes=10)
55/// duration = Duration()
56/// duration.FromTimedelta(td)
57/// ```
58///
59/// # JSON Mapping
60///
61/// In JSON format, the Duration type is encoded as a string rather than an
62/// object, where the string ends in the suffix "s" (indicating seconds) and
63/// is preceded by the number of seconds, with nanoseconds expressed as
64/// fractional seconds. For example, 3 seconds with 0 nanoseconds should be
65/// encoded in JSON format as "3s", while 3 seconds and 1 nanosecond should
66/// be expressed in JSON format as "3.000000001s", and 3 seconds and 1
67/// microsecond should be expressed in JSON format as "3.000001s".
68#[derive(Clone, Debug, Default)]
69pub struct DurationView<'a> {
70    /// Signed seconds of the span of time. Must be from -315,576,000,000
71    /// to +315,576,000,000 inclusive. Note: these bounds are computed from:
72    /// 60 sec/min * 60 min/hr * 24 hr/day * 365.25 days/year * 10000 years
73    ///
74    /// Field 1: `seconds`
75    pub seconds: i64,
76    /// Signed fractions of a second at nanosecond resolution of the span
77    /// of time. Durations less than one second are represented with a 0
78    /// `seconds` field and a positive or negative `nanos` field. For durations
79    /// of one second or more, a non-zero value for the `nanos` field must be
80    /// of the same sign as the `seconds` field. Must be from -999,999,999
81    /// to +999,999,999 inclusive.
82    ///
83    /// Field 2: `nanos`
84    pub nanos: i32,
85    pub __buffa_unknown_fields: ::buffa::UnknownFieldsView<'a>,
86}
87impl<'a> ::buffa::MessageView<'a> for DurationView<'a> {
88    type Owned = super::super::Duration;
89    fn decode_view(buf: &'a [u8]) -> ::core::result::Result<Self, ::buffa::DecodeError> {
90        let __limit = ::core::cell::Cell::new(::buffa::DEFAULT_UNKNOWN_FIELD_LIMIT);
91        <Self as ::buffa::MessageView>::decode_view_ctx(
92            buf,
93            ::buffa::DecodeContext::new(::buffa::RECURSION_LIMIT, &__limit),
94        )
95    }
96    fn decode_view_with_ctx(
97        buf: &'a [u8],
98        ctx: ::buffa::DecodeContext<'_>,
99    ) -> ::core::result::Result<Self, ::buffa::DecodeError> {
100        <Self as ::buffa::MessageView>::decode_view_ctx(buf, ctx)
101    }
102    fn merge_view_field(
103        &mut self,
104        tag: ::buffa::encoding::Tag,
105        cur: &'a [u8],
106        before_tag: &'a [u8],
107        ctx: ::buffa::DecodeContext<'_>,
108    ) -> ::core::result::Result<&'a [u8], ::buffa::DecodeError> {
109        let _ = ctx;
110        #[allow(unused_variables)]
111        let view = self;
112        let mut cur = cur;
113        match tag.field_number() {
114            1u32 => {
115                ::buffa::encoding::check_wire_type(
116                    tag,
117                    ::buffa::encoding::WireType::Varint,
118                )?;
119                view.seconds = ::buffa::types::decode_int64(&mut cur)?;
120            }
121            2u32 => {
122                ::buffa::encoding::check_wire_type(
123                    tag,
124                    ::buffa::encoding::WireType::Varint,
125                )?;
126                view.nanos = ::buffa::types::decode_int32(&mut cur)?;
127            }
128            _ => {
129                ::buffa::encoding::skip_field_depth(tag, &mut cur, ctx.depth())?;
130                let span_len = before_tag.len() - cur.len();
131                view.__buffa_unknown_fields.push_record(before_tag, span_len, ctx)?;
132            }
133        }
134        ::core::result::Result::Ok(cur)
135    }
136    fn to_owned_message(
137        &self,
138    ) -> ::core::result::Result<super::super::Duration, ::buffa::DecodeError> {
139        self.to_owned_from_source(None)
140    }
141    #[allow(clippy::useless_conversion, clippy::needless_update)]
142    fn to_owned_from_source(
143        &self,
144        __buffa_src: ::core::option::Option<&::buffa::bytes::Bytes>,
145    ) -> ::core::result::Result<super::super::Duration, ::buffa::DecodeError> {
146        #[allow(unused_imports)]
147        use ::buffa::alloc::string::ToString as _;
148        let _ = __buffa_src;
149        ::core::result::Result::Ok(super::super::Duration {
150            seconds: self.seconds,
151            nanos: self.nanos,
152            __buffa_unknown_fields: self.__buffa_unknown_fields.to_owned()?.into(),
153            ..::core::default::Default::default()
154        })
155    }
156}
157impl<'a> ::buffa::ViewEncode<'a> for DurationView<'a> {
158    #[allow(clippy::needless_borrow, clippy::let_and_return)]
159    fn compute_size(&self, _cache: &mut ::buffa::SizeCache) -> u32 {
160        #[allow(unused_imports)]
161        use ::buffa::Enumeration as _;
162        let mut size = 0u32;
163        if self.seconds != 0i64 {
164            size += 1u32 + ::buffa::types::int64_encoded_len(self.seconds) as u32;
165        }
166        if self.nanos != 0i32 {
167            size += 1u32 + ::buffa::types::int32_encoded_len(self.nanos) as u32;
168        }
169        size += self.__buffa_unknown_fields.encoded_len() as u32;
170        size
171    }
172    #[allow(clippy::needless_borrow)]
173    fn write_to(
174        &self,
175        _cache: &mut ::buffa::SizeCache,
176        buf: &mut impl ::buffa::bytes::BufMut,
177    ) {
178        #[allow(unused_imports)]
179        use ::buffa::Enumeration as _;
180        if self.seconds != 0i64 {
181            ::buffa::types::put_int64_field(1u32, self.seconds, buf);
182        }
183        if self.nanos != 0i32 {
184            ::buffa::types::put_int32_field(2u32, self.nanos, buf);
185        }
186        self.__buffa_unknown_fields.write_to(buf);
187    }
188}
189impl<'a> ::buffa::MessageName for DurationView<'a> {
190    const PACKAGE: &'static str = "google.protobuf";
191    const NAME: &'static str = "Duration";
192    const FULL_NAME: &'static str = "google.protobuf.Duration";
193    const TYPE_URL: &'static str = "type.googleapis.com/google.protobuf.Duration";
194}
195::buffa::impl_default_view_instance!(DurationView);
196::buffa::impl_view_reborrow!(DurationView);
197/** Self-contained, `'static` owned view of a `Duration` message.
198
199 Wraps [`::buffa::OwnedView`]`<`[`DurationView`]`<'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.
200
201 Field accessors return borrows tied to `&self`. Use [`Self::view`] to get the full [`DurationView`] when you need struct patterns, iteration helpers, or to pass the view to lifetime-parameterised code.*/
202#[derive(Clone, Debug)]
203pub struct DurationOwnedView(::buffa::OwnedView<DurationView<'static>>);
204impl DurationOwnedView {
205    /// Decode an owned view from a [`::buffa::bytes::Bytes`] buffer.
206    ///
207    /// The view borrows directly from the buffer's data; the buffer is
208    /// retained inside the returned handle.
209    ///
210    /// # Errors
211    ///
212    /// Returns [`::buffa::DecodeError`] if the buffer contains invalid
213    /// protobuf data.
214    pub fn decode(
215        bytes: ::buffa::bytes::Bytes,
216    ) -> ::core::result::Result<Self, ::buffa::DecodeError> {
217        ::core::result::Result::Ok(DurationOwnedView(::buffa::OwnedView::decode(bytes)?))
218    }
219    /// Decode with custom [`::buffa::DecodeOptions`] (recursion limit,
220    /// max message size).
221    ///
222    /// # Errors
223    ///
224    /// Returns [`::buffa::DecodeError`] if the buffer is invalid or
225    /// exceeds the configured limits.
226    pub fn decode_with_options(
227        bytes: ::buffa::bytes::Bytes,
228        opts: &::buffa::DecodeOptions,
229    ) -> ::core::result::Result<Self, ::buffa::DecodeError> {
230        ::core::result::Result::Ok(
231            DurationOwnedView(::buffa::OwnedView::decode_with_options(bytes, opts)?),
232        )
233    }
234    /// Build from an owned message via an encode → decode round-trip.
235    ///
236    /// # Errors
237    ///
238    /// Returns [`::buffa::DecodeError`] if the re-encoded bytes are
239    /// somehow invalid (should not happen for well-formed messages).
240    pub fn from_owned(
241        msg: &super::super::Duration,
242    ) -> ::core::result::Result<Self, ::buffa::DecodeError> {
243        ::core::result::Result::Ok(
244            DurationOwnedView(::buffa::OwnedView::from_owned(msg)?),
245        )
246    }
247    /// Borrow the full [`DurationView`] with its lifetime tied to `&self`.
248    #[must_use]
249    pub fn view(&self) -> &DurationView<'_> {
250        self.0.reborrow()
251    }
252    /// Convert to the owned message type.
253    ///
254    /// # Errors
255    ///
256    /// Returns an error if re-materializing preserved unknown fields
257    /// fails (e.g. the unknown-field limit is exceeded).
258    pub fn to_owned_message(
259        &self,
260    ) -> ::core::result::Result<super::super::Duration, ::buffa::DecodeError> {
261        self.0.to_owned_message()
262    }
263    /// The underlying bytes buffer.
264    #[must_use]
265    pub fn bytes(&self) -> &::buffa::bytes::Bytes {
266        self.0.bytes()
267    }
268    /// Consume the handle, returning the underlying bytes buffer.
269    #[must_use]
270    pub fn into_bytes(self) -> ::buffa::bytes::Bytes {
271        self.0.into_bytes()
272    }
273    /// Signed seconds of the span of time. Must be from -315,576,000,000
274    /// to +315,576,000,000 inclusive. Note: these bounds are computed from:
275    /// 60 sec/min * 60 min/hr * 24 hr/day * 365.25 days/year * 10000 years
276    ///
277    /// Field 1: `seconds`
278    #[must_use]
279    pub fn seconds(&self) -> i64 {
280        self.0.reborrow().seconds
281    }
282    /// Signed fractions of a second at nanosecond resolution of the span
283    /// of time. Durations less than one second are represented with a 0
284    /// `seconds` field and a positive or negative `nanos` field. For durations
285    /// of one second or more, a non-zero value for the `nanos` field must be
286    /// of the same sign as the `seconds` field. Must be from -999,999,999
287    /// to +999,999,999 inclusive.
288    ///
289    /// Field 2: `nanos`
290    #[must_use]
291    pub fn nanos(&self) -> i32 {
292        self.0.reborrow().nanos
293    }
294}
295impl ::core::convert::From<::buffa::OwnedView<DurationView<'static>>>
296for DurationOwnedView {
297    fn from(inner: ::buffa::OwnedView<DurationView<'static>>) -> Self {
298        DurationOwnedView(inner)
299    }
300}
301impl ::core::convert::From<DurationOwnedView>
302for ::buffa::OwnedView<DurationView<'static>> {
303    fn from(wrapper: DurationOwnedView) -> Self {
304        wrapper.0
305    }
306}
307impl ::core::convert::AsRef<::buffa::OwnedView<DurationView<'static>>>
308for DurationOwnedView {
309    fn as_ref(&self) -> &::buffa::OwnedView<DurationView<'static>> {
310        &self.0
311    }
312}
313impl ::buffa::HasMessageView for super::super::Duration {
314    type View<'a> = DurationView<'a>;
315    type ViewHandle = DurationOwnedView;
316}
317#[cfg(feature = "reflect")]
318const _: () = {
319    impl<'a> ::buffa_descriptor::reflect::ReflectMessage for DurationView<'a> {
320        fn message_descriptor(&self) -> &::buffa_descriptor::MessageDescriptor {
321            super::super::__buffa::reflect::descriptor_pool()
322                .message(Self::__buffa_reflect_message_index())
323        }
324        fn pool(
325            &self,
326        ) -> &::buffa::alloc::sync::Arc<::buffa_descriptor::DescriptorPool> {
327            super::super::__buffa::reflect::descriptor_pool()
328        }
329        fn get(
330            &self,
331            field: &::buffa_descriptor::FieldDescriptor,
332        ) -> ::buffa_descriptor::reflect::ValueRef<'_> {
333            #[allow(unused_imports)]
334            use ::buffa::Enumeration as _;
335            match field.number() {
336                1u32 => ::buffa_descriptor::reflect::ValueRef::I64(self.seconds),
337                2u32 => ::buffa_descriptor::reflect::ValueRef::I32(self.nanos),
338                _ => {
339                    ::core::debug_assert!(
340                        false,
341                        "field number {} is not a member of this view's reflect get()",
342                        field.number(),
343                    );
344                    ::buffa_descriptor::reflect::ValueRef::Bool(false)
345                }
346            }
347        }
348        fn has(&self, field: &::buffa_descriptor::FieldDescriptor) -> bool {
349            match field.number() {
350                1u32 => self.seconds != 0,
351                2u32 => self.nanos != 0,
352                _ => false,
353            }
354        }
355        fn for_each_set(
356            &self,
357            f: &mut dyn ::core::ops::FnMut(
358                &::buffa_descriptor::FieldDescriptor,
359                ::buffa_descriptor::reflect::ValueRef<'_>,
360            ),
361        ) {
362            let md = ::buffa_descriptor::reflect::ReflectMessage::message_descriptor(
363                self,
364            );
365            for fd in md.fields() {
366                if ::buffa_descriptor::reflect::ReflectMessage::has(self, fd) {
367                    f(fd, ::buffa_descriptor::reflect::ReflectMessage::get(self, fd));
368                }
369            }
370        }
371        fn to_dynamic(&self) -> ::buffa_descriptor::reflect::DynamicMessage {
372            let bytes = ::buffa::ViewEncode::encode_to_vec(self);
373            ::buffa_descriptor::reflect::DynamicMessage::decode(
374                    ::buffa::alloc::sync::Arc::clone(
375                        super::super::__buffa::reflect::descriptor_pool(),
376                    ),
377                    Self::__buffa_reflect_message_index(),
378                    &bytes,
379                )
380                .expect("view re-encodes to bytes decodable against its own descriptor")
381        }
382    }
383    impl<'a> ::buffa_descriptor::reflect::ReflectElement for DurationView<'a> {
384        fn as_value_ref(&self) -> ::buffa_descriptor::reflect::ValueRef<'_> {
385            ::buffa_descriptor::reflect::ValueRef::Message(
386                ::buffa_descriptor::reflect::ReflectCow::Borrowed(self),
387            )
388        }
389    }
390    impl<'a> DurationView<'a> {
391        /// Memoized `MessageIndex` for this view's message type, resolved
392        /// once against the package's embedded descriptor pool. An inherent
393        /// associated fn (not a free fn) so sibling views in the same module
394        /// do not collide.
395        #[doc(hidden)]
396        fn __buffa_reflect_message_index() -> ::buffa_descriptor::MessageIndex {
397            static IDX: ::std::sync::OnceLock<::buffa_descriptor::MessageIndex> = ::std::sync::OnceLock::new();
398            *IDX
399                .get_or_init(|| {
400                    super::super::__buffa::reflect::descriptor_pool()
401                        .message_index(<Self as ::buffa::MessageName>::FULL_NAME)
402                        .expect(
403                            "generated view type is registered in the embedded descriptor pool",
404                        )
405                })
406        }
407    }
408};