1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
//! Items specific to `optional` fields.
#![allow(dead_code)]
#![allow(unused)]
use crate::__internal::Private;
use crate::{Mut, MutProxied, MutProxy, Proxied, View, ViewProxy};
use std::convert::{AsMut, AsRef};
use std::fmt::{self, Debug};
use std::panic;
use std::ptr;
/// A protobuf value from a field that may not be set.
///
/// This can be pattern matched with `match` or `if let` to determine if the
/// field is set and access the field data.
///
/// Two `Optional`s are equal if they match both presence and the field values.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Optional<T> {
/// The field is set; it is present in the serialized message.
///
/// - For an `_opt()` accessor, this contains a `View<impl Proxied>`.
/// - For a `_mut()` accessor, this contains a [`PresentField`] that can be
/// used to access the current value, convert to [`Mut`], clear presence,
/// or set a new value.
Set(T),
/// The field is unset; it is absent in the serialized message.
///
/// - For an `_opt()` accessor, this contains a `View<impl Proxied>` with
/// the default value.
/// - For a `_mut()` accessor, this contains an [`AbsentField`] that can be
/// used to access the default or set a new value.
Unset(T),
}
impl<T> Optional<T> {
/// Gets the field value, ignoring whether it was set or not.
pub fn into_inner(self) -> T {
match self {
Optional::Set(x) | Optional::Unset(x) => x,
}
}
/// Constructs an `Optional<T>` with a `T` value and presence bit.
pub fn new(val: T, is_set: bool) -> Self {
if is_set { Optional::Set(val) } else { Optional::Unset(val) }
}
/// Converts into an `Option` of the set value, ignoring any unset value.
pub fn into_option(self) -> Option<T> {
if let Optional::Set(x) = self { Some(x) } else { None }
}
/// Returns if the field is set.
pub fn is_set(&self) -> bool {
matches!(self, Optional::Set(_))
}
/// Returns if the field is unset.
pub fn is_unset(&self) -> bool {
matches!(self, Optional::Unset(_))
}
}
impl<T> From<Optional<T>> for Option<T> {
fn from(x: Optional<T>) -> Option<T> {
x.into_option()
}
}
|