pub struct MessageField<T>where
T: Default,{ /* private fields */ }Expand description
A wrapper for optional message fields that provides transparent access to a default instance when the field is not set.
This type is used for singular message fields in generated code. It avoids
the ergonomic pain of Option<Box<M>> while still being heap-allocated
only when set (no allocation for unset fields).
§Access patterns
// Reading through an unset field gives the default:
let msg = Outer::default();
assert_eq!(msg.inner.name, ""); // No unwrap needed, derefs to default
// Check if set:
if msg.inner.is_set() { ... }
// Set a value:
msg.inner = MessageField::some(Inner { name: "hello".into(), ..Default::default() });
// Clear:
msg.inner = MessageField::none();Implementations§
Source§impl<T> MessageField<T>where
T: Default,
impl<T> MessageField<T>where
T: Default,
Sourcepub const fn none() -> MessageField<T>
pub const fn none() -> MessageField<T>
Create a MessageField with no value set.
Sourcepub fn some(value: T) -> MessageField<T>
pub fn some(value: T) -> MessageField<T>
Create a MessageField with a value.
Sourcepub fn from_box(value: Box<T>) -> MessageField<T>
pub fn from_box(value: Box<T>) -> MessageField<T>
Create a MessageField from a boxed value.
Sourcepub fn as_option_mut(&mut self) -> Option<&mut T>
pub fn as_option_mut(&mut self) -> Option<&mut T>
Get a mutable reference to the inner value, or None if unset.
Sourcepub fn get_or_insert_default(&mut self) -> &mut T
pub fn get_or_insert_default(&mut self) -> &mut T
Get a mutable reference to the value, initializing to the default if unset.
Sourcepub fn modify<F>(&mut self, f: F)
pub fn modify<F>(&mut self, f: F)
Call f with a mutable reference to the inner value, initializing to
the default if the field is currently unset.
This is the ergonomic write counterpart to the transparent read
provided by Deref. Instead of calling get_or_insert_default once
per assignment:
msg.address.get_or_insert_default().street = "123 Main St".into();
msg.address.get_or_insert_default().city = "Springfield".into();use modify to initialize the field once and set all sub-fields in
the closure:
msg.address.modify(|a| {
a.street = "123 Main St".into();
a.city = "Springfield".into();
});Sourcepub fn into_option(self) -> Option<T>
pub fn into_option(self) -> Option<T>
Consume the field, returning Some(T) if set or None if unset.
This unboxes the inner value. For in-place extraction that leaves the
field unset without consuming the enclosing struct, see take.
Sourcepub fn ok_or<E>(self, err: E) -> Result<T, E>
pub fn ok_or<E>(self, err: E) -> Result<T, E>
Consume the field, returning Ok(T) if set or Err(err) if unset.
Mirrors Option::ok_or. Useful for enforcing presence of
semantically-required fields that the proto schema leaves optional:
let cmd = request.normalized_command.ok_or(Error::MissingCommand)?;Sourcepub fn ok_or_else<E, F>(self, err: F) -> Result<T, E>where
F: FnOnce() -> E,
pub fn ok_or_else<E, F>(self, err: F) -> Result<T, E>where
F: FnOnce() -> E,
Consume the field, returning Ok(T) if set or Err(err()) if unset.
Mirrors Option::ok_or_else. The closure is only called if the
field is unset, so use this over ok_or when
constructing the error is non-trivial:
let cmd = request.normalized_command.ok_or_else(|| {
ConnectError::invalid_argument("missing normalized_command in request")
})?;Trait Implementations§
Source§impl<T> Clone for MessageField<T>
impl<T> Clone for MessageField<T>
Source§fn clone(&self) -> MessageField<T>
fn clone(&self) -> MessageField<T>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T> Debug for MessageField<T>
impl<T> Debug for MessageField<T>
Source§impl<T> Default for MessageField<T>where
T: Default,
impl<T> Default for MessageField<T>where
T: Default,
Source§fn default() -> MessageField<T>
fn default() -> MessageField<T>
Source§impl<T> Deref for MessageField<T>where
T: DefaultInstance,
impl<T> Deref for MessageField<T>where
T: DefaultInstance,
Source§impl<T> From<Option<T>> for MessageField<T>where
T: Default,
impl<T> From<Option<T>> for MessageField<T>where
T: Default,
Source§fn from(opt: Option<T>) -> MessageField<T>
fn from(opt: Option<T>) -> MessageField<T>
Source§impl<T> From<T> for MessageField<T>where
T: Default,
impl<T> From<T> for MessageField<T>where
T: Default,
Source§fn from(value: T) -> MessageField<T>
fn from(value: T) -> MessageField<T>
Source§impl<T> PartialEq for MessageField<T>where
T: DefaultInstance + PartialEq,
impl<T> PartialEq for MessageField<T>where
T: DefaultInstance + PartialEq,
Source§fn eq(&self, other: &MessageField<T>) -> bool
fn eq(&self, other: &MessageField<T>) -> bool
self and other values to be equal, and is used by ==.