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    #[inline]
103    fn merge_view_field(
104        &mut self,
105        tag: ::buffa::encoding::Tag,
106        cur: &'a [u8],
107        before_tag: &'a [u8],
108        ctx: ::buffa::DecodeContext<'_>,
109    ) -> ::core::result::Result<&'a [u8], ::buffa::DecodeError> {
110        let _ = ctx;
111        #[allow(unused_variables)]
112        let view = self;
113        let mut cur = cur;
114        match tag.field_number() {
115            1u32 => {
116                ::buffa::encoding::check_wire_type(
117                    tag,
118                    ::buffa::encoding::WireType::Varint,
119                )?;
120                view.seconds = ::buffa::types::decode_int64(&mut cur)?;
121            }
122            2u32 => {
123                ::buffa::encoding::check_wire_type(
124                    tag,
125                    ::buffa::encoding::WireType::Varint,
126                )?;
127                view.nanos = ::buffa::types::decode_int32(&mut cur)?;
128            }
129            _ => {
130                ::buffa::encoding::skip_field_depth(tag, &mut cur, ctx.depth())?;
131                let span_len = before_tag.len() - cur.len();
132                view.__buffa_unknown_fields.push_record(before_tag, span_len, ctx)?;
133            }
134        }
135        ::core::result::Result::Ok(cur)
136    }
137    fn to_owned_message(
138        &self,
139    ) -> ::core::result::Result<super::super::Duration, ::buffa::DecodeError> {
140        self.to_owned_from_source(None)
141    }
142    #[allow(clippy::useless_conversion, clippy::needless_update)]
143    fn to_owned_from_source(
144        &self,
145        __buffa_src: ::core::option::Option<&::buffa::bytes::Bytes>,
146    ) -> ::core::result::Result<super::super::Duration, ::buffa::DecodeError> {
147        #[allow(unused_imports)]
148        use ::buffa::alloc::string::ToString as _;
149        let _ = __buffa_src;
150        ::core::result::Result::Ok(super::super::Duration {
151            seconds: self.seconds,
152            nanos: self.nanos,
153            __buffa_unknown_fields: self.__buffa_unknown_fields.to_owned()?.into(),
154            ..::core::default::Default::default()
155        })
156    }
157}
158impl<'a> ::buffa::ViewEncode<'a> for DurationView<'a> {
159    #[allow(clippy::needless_borrow, clippy::let_and_return)]
160    fn compute_size(&self, _cache: &mut ::buffa::SizeCache) -> u32 {
161        #[allow(unused_imports)]
162        use ::buffa::Enumeration as _;
163        let mut size = 0u64;
164        if self.seconds != 0i64 {
165            size += 1u64 + ::buffa::types::int64_encoded_len(self.seconds) as u64;
166        }
167        if self.nanos != 0i32 {
168            size += 1u64 + ::buffa::types::int32_encoded_len(self.nanos) as u64;
169        }
170        size += self.__buffa_unknown_fields.encoded_len() as u64;
171        ::buffa::saturate_size(size)
172    }
173    #[allow(clippy::needless_borrow)]
174    fn write_to(
175        &self,
176        _cache: &mut ::buffa::SizeCache,
177        buf: &mut impl ::buffa::EncodeSink,
178    ) {
179        #[allow(unused_imports)]
180        use ::buffa::Enumeration as _;
181        if self.seconds != 0i64 {
182            ::buffa::types::put_int64_field(1u32, self.seconds, buf);
183        }
184        if self.nanos != 0i32 {
185            ::buffa::types::put_int32_field(2u32, self.nanos, buf);
186        }
187        self.__buffa_unknown_fields.write_to(buf);
188    }
189}
190impl<'a> ::buffa::MessageName for DurationView<'a> {
191    const PACKAGE: &'static str = "google.protobuf";
192    const NAME: &'static str = "Duration";
193    const FULL_NAME: &'static str = "google.protobuf.Duration";
194    const TYPE_URL: &'static str = "type.googleapis.com/google.protobuf.Duration";
195}
196::buffa::impl_default_view_instance!(DurationView);
197::buffa::impl_view_reborrow!(DurationView);
198/** Self-contained, `'static` owned view of a `Duration` message.
199
200 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.
201
202 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.*/
203#[derive(Clone, Debug)]
204pub struct DurationOwnedView(::buffa::OwnedView<DurationView<'static>>);
205impl DurationOwnedView {
206    /// Decode an owned view from a [`::buffa::bytes::Bytes`] buffer.
207    ///
208    /// The view borrows directly from the buffer's data; the buffer is
209    /// retained inside the returned handle.
210    ///
211    /// # Errors
212    ///
213    /// Returns [`::buffa::DecodeError`] if the buffer contains invalid
214    /// protobuf data.
215    pub fn decode(
216        bytes: ::buffa::bytes::Bytes,
217    ) -> ::core::result::Result<Self, ::buffa::DecodeError> {
218        ::core::result::Result::Ok(DurationOwnedView(::buffa::OwnedView::decode(bytes)?))
219    }
220    /// Decode with custom [`::buffa::DecodeOptions`] (recursion limit,
221    /// max message size).
222    ///
223    /// # Errors
224    ///
225    /// Returns [`::buffa::DecodeError`] if the buffer is invalid or
226    /// exceeds the configured limits.
227    pub fn decode_with_options(
228        bytes: ::buffa::bytes::Bytes,
229        opts: &::buffa::DecodeOptions,
230    ) -> ::core::result::Result<Self, ::buffa::DecodeError> {
231        ::core::result::Result::Ok(
232            DurationOwnedView(::buffa::OwnedView::decode_with_options(bytes, opts)?),
233        )
234    }
235    /// Build from an owned message via an encode → decode round-trip.
236    ///
237    /// # Errors
238    ///
239    /// Returns [`::buffa::DecodeError::MessageTooLarge`] if the
240    /// message's encoded size exceeds the 2 GiB protobuf limit, or
241    /// another [`::buffa::DecodeError`] if the re-encoded bytes are
242    /// somehow invalid (should not happen for well-formed messages).
243    pub fn from_owned(
244        msg: &super::super::Duration,
245    ) -> ::core::result::Result<Self, ::buffa::DecodeError> {
246        ::core::result::Result::Ok(
247            DurationOwnedView(::buffa::OwnedView::from_owned(msg)?),
248        )
249    }
250    /// Borrow the full [`DurationView`] with its lifetime tied to `&self`.
251    #[must_use]
252    pub fn view(&self) -> &DurationView<'_> {
253        self.0.reborrow()
254    }
255    /// Convert to the owned message type.
256    ///
257    /// Infallible: this type's constructors wire-decode their
258    /// buffer, and a view produced by wire decoding always
259    /// converts. Delegates to [`::buffa::OwnedView::to_owned_message`],
260    /// whose contract also governs handles converted from a raw
261    /// [`::buffa::OwnedView`].
262    #[must_use]
263    pub fn to_owned_message(&self) -> super::super::Duration {
264        self.0.to_owned_message()
265    }
266    /// The underlying bytes buffer.
267    #[must_use]
268    pub fn bytes(&self) -> &::buffa::bytes::Bytes {
269        self.0.bytes()
270    }
271    /// Consume the handle, returning the underlying bytes buffer.
272    #[must_use]
273    pub fn into_bytes(self) -> ::buffa::bytes::Bytes {
274        self.0.into_bytes()
275    }
276    /// Signed seconds of the span of time. Must be from -315,576,000,000
277    /// to +315,576,000,000 inclusive. Note: these bounds are computed from:
278    /// 60 sec/min * 60 min/hr * 24 hr/day * 365.25 days/year * 10000 years
279    ///
280    /// Field 1: `seconds`
281    #[must_use]
282    pub fn seconds(&self) -> i64 {
283        self.0.reborrow().seconds
284    }
285    /// Signed fractions of a second at nanosecond resolution of the span
286    /// of time. Durations less than one second are represented with a 0
287    /// `seconds` field and a positive or negative `nanos` field. For durations
288    /// of one second or more, a non-zero value for the `nanos` field must be
289    /// of the same sign as the `seconds` field. Must be from -999,999,999
290    /// to +999,999,999 inclusive.
291    ///
292    /// Field 2: `nanos`
293    #[must_use]
294    pub fn nanos(&self) -> i32 {
295        self.0.reborrow().nanos
296    }
297}
298impl ::core::convert::From<::buffa::OwnedView<DurationView<'static>>>
299for DurationOwnedView {
300    fn from(inner: ::buffa::OwnedView<DurationView<'static>>) -> Self {
301        DurationOwnedView(inner)
302    }
303}
304impl ::core::convert::From<DurationOwnedView>
305for ::buffa::OwnedView<DurationView<'static>> {
306    fn from(wrapper: DurationOwnedView) -> Self {
307        wrapper.0
308    }
309}
310impl ::core::convert::AsRef<::buffa::OwnedView<DurationView<'static>>>
311for DurationOwnedView {
312    fn as_ref(&self) -> &::buffa::OwnedView<DurationView<'static>> {
313        &self.0
314    }
315}
316impl ::buffa::HasMessageView for super::super::Duration {
317    type View<'a> = DurationView<'a>;
318    type ViewHandle = DurationOwnedView;
319}
320#[cfg(feature = "reflect")]
321const _: () = {
322    impl<'a> ::buffa_descriptor::reflect::ReflectMessage for DurationView<'a> {
323        fn message_descriptor(&self) -> &::buffa_descriptor::MessageDescriptor {
324            super::super::__buffa::reflect::descriptor_pool()
325                .message(Self::__buffa_reflect_message_index())
326        }
327        fn pool(
328            &self,
329        ) -> &::buffa::alloc::sync::Arc<::buffa_descriptor::DescriptorPool> {
330            super::super::__buffa::reflect::descriptor_pool()
331        }
332        fn get(
333            &self,
334            field: &::buffa_descriptor::FieldDescriptor,
335        ) -> ::buffa_descriptor::reflect::ValueRef<'_> {
336            #[allow(unused_imports)]
337            use ::buffa::Enumeration as _;
338            match field.number() {
339                1u32 => ::buffa_descriptor::reflect::ValueRef::I64(self.seconds),
340                2u32 => ::buffa_descriptor::reflect::ValueRef::I32(self.nanos),
341                _ => {
342                    ::core::debug_assert!(
343                        false,
344                        "field number {} is not a member of this view's reflect get()",
345                        field.number(),
346                    );
347                    ::buffa_descriptor::reflect::ValueRef::Bool(false)
348                }
349            }
350        }
351        fn has(&self, field: &::buffa_descriptor::FieldDescriptor) -> bool {
352            match field.number() {
353                1u32 => self.seconds != 0,
354                2u32 => self.nanos != 0,
355                _ => false,
356            }
357        }
358        fn for_each_set(
359            &self,
360            f: &mut dyn ::core::ops::FnMut(
361                &::buffa_descriptor::FieldDescriptor,
362                ::buffa_descriptor::reflect::ValueRef<'_>,
363            ),
364        ) {
365            let md = ::buffa_descriptor::reflect::ReflectMessage::message_descriptor(
366                self,
367            );
368            for fd in md.fields() {
369                if ::buffa_descriptor::reflect::ReflectMessage::has(self, fd) {
370                    f(fd, ::buffa_descriptor::reflect::ReflectMessage::get(self, fd));
371                }
372            }
373        }
374        fn to_dynamic(&self) -> ::buffa_descriptor::reflect::DynamicMessage {
375            let bytes = ::buffa::ViewEncode::encode_to_vec(self);
376            ::buffa_descriptor::reflect::DynamicMessage::decode(
377                    ::buffa::alloc::sync::Arc::clone(
378                        super::super::__buffa::reflect::descriptor_pool(),
379                    ),
380                    Self::__buffa_reflect_message_index(),
381                    &bytes,
382                )
383                .expect("view re-encodes to bytes decodable against its own descriptor")
384        }
385    }
386    impl<'a> ::buffa_descriptor::reflect::ReflectElement for DurationView<'a> {
387        fn as_value_ref(&self) -> ::buffa_descriptor::reflect::ValueRef<'_> {
388            ::buffa_descriptor::reflect::ValueRef::Message(
389                ::buffa_descriptor::reflect::ReflectCow::Borrowed(self),
390            )
391        }
392    }
393    impl<'a> DurationView<'a> {
394        /// Memoized `MessageIndex` for this view's message type, resolved
395        /// once against the package's embedded descriptor pool. An inherent
396        /// associated fn (not a free fn) so sibling views in the same module
397        /// do not collide.
398        #[doc(hidden)]
399        fn __buffa_reflect_message_index() -> ::buffa_descriptor::MessageIndex {
400            static IDX: ::std::sync::OnceLock<::buffa_descriptor::MessageIndex> = ::std::sync::OnceLock::new();
401            *IDX
402                .get_or_init(|| {
403                    super::super::__buffa::reflect::descriptor_pool()
404                        .message_index(<Self as ::buffa::MessageName>::FULL_NAME)
405                        .expect(
406                            "generated view type is registered in the embedded descriptor pool",
407                        )
408                })
409        }
410    }
411};