Add SettableValue for generic field setting
This is a prerequisite for optional types to have proper setters.
PiperOrigin-RevId: 542588427
diff --git a/rust/optional.rs b/rust/optional.rs
index b5b4169..5adab27 100644
--- a/rust/optional.rs
+++ b/rust/optional.rs
@@ -32,7 +32,8 @@
#![allow(dead_code)]
#![allow(unused)]
-use crate::{Mut, MutProxy, Proxied, View, ViewProxy};
+use crate::__internal::Private;
+use crate::{Mut, MutProxy, Proxied, SettableValue, View, ViewProxy};
use std::convert::{AsMut, AsRef};
use std::fmt::{self, Debug};
@@ -118,8 +119,11 @@
/// Sets the value of this field to `val`.
///
/// Equivalent to `self.or_default().set(val)`.
- pub fn set(&mut self, val: Todo) {
- todo!("b/285308646: Requires a trait method")
+ pub fn set(&mut self, val: impl SettableValue<T>) {
+ match self {
+ Optional::Set(x) => x.set(val),
+ Optional::Unset(x) => todo!(),
+ }
}
/// Clears the field; `is_set()` will return `false`.
@@ -181,8 +185,8 @@
self.into_view()
}
- pub fn set(&mut self, val: Todo) {
- todo!("b/285308646: Requires a trait method")
+ pub fn set(&mut self, val: impl SettableValue<T>) {
+ val.set_on(Private, self.as_mut())
}
/// See [`FieldEntry::clear`].
@@ -253,7 +257,7 @@
/// See [`FieldEntry::set`]. Note that this consumes and returns a
/// `PresentField`.
- pub fn set(self, val: Todo) -> PresentField<'msg, T> {
+ pub fn set(self, val: impl SettableValue<T>) -> PresentField<'msg, T> {
todo!("b/285308646: Requires a trait method")
}