Skip to content

Commit

Permalink
Remove the second 'unset' generic argument from Optional
Browse files Browse the repository at this point in the history
This is no longer needed with our new design.

PiperOrigin-RevId: 653488903
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Jul 18, 2024
1 parent be4f149 commit 7c5dd9e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
8 changes: 3 additions & 5 deletions rust/optional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ use std::ptr;
///
/// Two `Optional`s are equal if they match both presence and the field values.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Optional<SetVal, UnsetVal = SetVal> {
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(SetVal),
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(UnsetVal),
Unset(T),
}

impl<T> Optional<T> {
Expand All @@ -53,9 +53,7 @@ impl<T> Optional<T> {
pub fn new(val: T, is_set: bool) -> Self {
if is_set { Optional::Set(val) } else { Optional::Unset(val) }
}
}

impl<T, A> Optional<T, A> {
/// 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 }
Expand Down
6 changes: 2 additions & 4 deletions rust/test/shared/accessors_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,10 +609,8 @@ fn test_singular_msg_field() {
#[test]
fn test_message_opt() {
let msg = TestAllTypes::new();
let opt: Optional<
unittest_rust_proto::test_all_types::NestedMessageView<'_>,
unittest_rust_proto::test_all_types::NestedMessageView<'_>,
> = msg.optional_nested_message_opt();
let opt: Optional<unittest_rust_proto::test_all_types::NestedMessageView<'_>> =
msg.optional_nested_message_opt();
assert_that!(opt.is_set(), eq(false));
assert_that!(opt.into_inner().bb(), eq(0));
}
Expand Down

0 comments on commit 7c5dd9e

Please sign in to comment.