pub struct DynamicMessage { /* private fields */ }dynamic only.Expand description
A protobuf message whose schema is known at runtime via a
MessageDescriptor.
DynamicMessage is the workhorse type that lets a single binary
transcode, inspect, or mutate any proto message in any
DescriptorPool. See the module-level docs for examples.
Implementations§
Source§impl DynamicMessage
impl DynamicMessage
Sourcepub fn new(desc: MessageDescriptor) -> Self
pub fn new(desc: MessageDescriptor) -> Self
Construct an empty message of the given descriptor.
Sourcepub fn descriptor(&self) -> MessageDescriptor
pub fn descriptor(&self) -> MessageDescriptor
The owning descriptor.
Sourcepub fn parent_pool(&self) -> DescriptorPool
pub fn parent_pool(&self) -> DescriptorPool
The pool the descriptor was built from. Cloning is Arc-cheap.
Sourcepub fn decode<B: Buf>(
desc: MessageDescriptor,
buf: B,
) -> Result<Self, DecodeError>
pub fn decode<B: Buf>( desc: MessageDescriptor, buf: B, ) -> Result<Self, DecodeError>
Sourcepub fn decode_with_options<B: Buf>(
desc: MessageDescriptor,
buf: B,
opts: DecodeOptions,
) -> Result<Self, DecodeError>
pub fn decode_with_options<B: Buf>( desc: MessageDescriptor, buf: B, opts: DecodeOptions, ) -> Result<Self, DecodeError>
Sourcepub fn merge<B: Buf>(&mut self, buf: B) -> Result<(), DecodeError>
pub fn merge<B: Buf>(&mut self, buf: B) -> Result<(), DecodeError>
Merge wire bytes into self, accumulating fields atop the
existing ones (matching Message::merge semantics).
§Errors
See DecodeError.
Sourcepub fn encoded_len(&self) -> usize
pub fn encoded_len(&self) -> usize
Encoded byte length, including unknown fields.
Sourcepub fn encode<B: BufMut>(&self, buf: &mut B) -> Result<(), EncodeError>
pub fn encode<B: BufMut>(&self, buf: &mut B) -> Result<(), EncodeError>
Encode to buf. Iterates fields in number order; unknown fields
re-emit at their original positions.
§Errors
Bubbles up encoder failures. With well-formed in-memory state
the only realistic source of failure is BufMut::remaining_mut
running short.
Sourcepub fn encode_to_vec(&self) -> Vec<u8> ⓘ
pub fn encode_to_vec(&self) -> Vec<u8> ⓘ
Encode to a fresh Vec<u8>.
Sourcepub fn encode_to_bytes(&self) -> Bytes
pub fn encode_to_bytes(&self) -> Bytes
Encode to a fresh Bytes.
Sourcepub fn transcode_from<T: Message>(
&mut self,
value: &T,
) -> Result<(), DecodeError>
pub fn transcode_from<T: Message>( &mut self, value: &T, ) -> Result<(), DecodeError>
Merge a typed T’s wire bytes into self.
§Errors
See DecodeError. Errors imply T’s schema and self.descriptor()
are incompatible.
Sourcepub fn transcode_to<T: Message + Default>(&self) -> Result<T, DecodeError>
pub fn transcode_to<T: Message + Default>(&self) -> Result<T, DecodeError>
Decode self as the typed T.
§Errors
See DecodeError. Errors imply T’s schema and the dynamic
message’s descriptor are incompatible.
Sourcepub fn transcode_to_dynamic(&self) -> Self
pub fn transcode_to_dynamic(&self) -> Self
Specialisation of transcode_to_dynamic — returns self.clone()
without the wire round-trip a typed ReflectMessage would pay.
Sourcepub fn fields(&self) -> impl Iterator<Item = (FieldDescriptor, &Value)> + '_
pub fn fields(&self) -> impl Iterator<Item = (FieldDescriptor, &Value)> + '_
Iterate every populated known field in field-number order.
Sourcepub fn iter_with_options<'a>(
&'a self,
include_default: bool,
index_order: bool,
) -> Box<dyn Iterator<Item = (FieldDescriptor, Cow<'a, Value>)> + 'a>
pub fn iter_with_options<'a>( &'a self, include_default: bool, index_order: bool, ) -> Box<dyn Iterator<Item = (FieldDescriptor, Cow<'a, Value>)> + 'a>
Iterate fields with explicit knobs over default-omission and declaration vs number order. See the design doc §6 for the rationale.
Sourcepub fn has_field(&self, field: &FieldDescriptor) -> bool
pub fn has_field(&self, field: &FieldDescriptor) -> bool
True iff a known value exists at field’s number, or for
non-presence-tracking fields, the value is non-default.
Sourcepub fn get_field(&self, field: &FieldDescriptor) -> Cow<'_, Value>
pub fn get_field(&self, field: &FieldDescriptor) -> Cow<'_, Value>
Read field — borrows the populated value or synthesises a
default.
Sourcepub fn get_field_mut(&mut self, field: &FieldDescriptor) -> &mut Value
pub fn get_field_mut(&mut self, field: &FieldDescriptor) -> &mut Value
Mutable accessor — clears any active oneof sibling first, then inserts a default value if the slot was empty.
Sourcepub fn has_field_by_name(&self, name: &str) -> bool
pub fn has_field_by_name(&self, name: &str) -> bool
has_field keyed by proto name.
Sourcepub fn get_field_by_name(&self, name: &str) -> Option<Cow<'_, Value>>
pub fn get_field_by_name(&self, name: &str) -> Option<Cow<'_, Value>>
get_field keyed by proto name.
Sourcepub fn get_field_by_name_mut(&mut self, name: &str) -> Option<&mut Value>
pub fn get_field_by_name_mut(&mut self, name: &str) -> Option<&mut Value>
get_field_mut keyed by proto name.
Sourcepub fn has_field_by_number(&self, number: u32) -> bool
pub fn has_field_by_number(&self, number: u32) -> bool
has_field keyed by tag number.
Sourcepub fn get_field_by_number(&self, number: u32) -> Option<Cow<'_, Value>>
pub fn get_field_by_number(&self, number: u32) -> Option<Cow<'_, Value>>
get_field keyed by tag number.
Sourcepub fn get_field_by_number_mut(&mut self, number: u32) -> Option<&mut Value>
pub fn get_field_by_number_mut(&mut self, number: u32) -> Option<&mut Value>
get_field_mut keyed by tag number.
Sourcepub fn set_field(&mut self, field: &FieldDescriptor, value: Value)
pub fn set_field(&mut self, field: &FieldDescriptor, value: Value)
Set field to value. Validates with debug_assert! (zero
cost in release builds); panics on type mismatch in debug. Use
Self::try_set_field when the value’s shape is data-driven.
§Panics
Panics in debug builds when value doesn’t match field’s
declared shape. In release builds the bad value is stored and
will surface as an encode-time mismatch.
Sourcepub fn try_set_field(
&mut self,
field: &FieldDescriptor,
value: Value,
) -> Result<(), SetFieldError>
pub fn try_set_field( &mut self, field: &FieldDescriptor, value: Value, ) -> Result<(), SetFieldError>
Validating equivalent of Self::set_field: returns
SetFieldError::InvalidType when value doesn’t match
field.
§Errors
See SetFieldError.
Sourcepub fn set_field_by_number(&mut self, number: u32, value: Value)
pub fn set_field_by_number(&mut self, number: u32, value: Value)
set_field keyed by tag number.
§Panics
Panics if number is unknown to the descriptor (in addition to
the type-mismatch panic inherited from Self::set_field).
Sourcepub fn try_set_field_by_number(
&mut self,
number: u32,
value: Value,
) -> Result<(), SetFieldError>
pub fn try_set_field_by_number( &mut self, number: u32, value: Value, ) -> Result<(), SetFieldError>
Sourcepub fn set_field_by_name(&mut self, name: &str, value: Value)
pub fn set_field_by_name(&mut self, name: &str, value: Value)
set_field keyed by proto name.
§Panics
Panics if name is unknown to the descriptor (in addition to
the type-mismatch panic inherited from Self::set_field).
Sourcepub fn try_set_field_by_name(
&mut self,
name: &str,
value: Value,
) -> Result<(), SetFieldError>
pub fn try_set_field_by_name( &mut self, name: &str, value: Value, ) -> Result<(), SetFieldError>
Sourcepub fn clear_field(&mut self, field: &FieldDescriptor)
pub fn clear_field(&mut self, field: &FieldDescriptor)
Clear field (and any sibling oneof member).
Sourcepub fn clear_field_by_name(&mut self, name: &str)
pub fn clear_field_by_name(&mut self, name: &str)
Clear by name; no-op when the name is unknown.
Sourcepub fn clear_field_by_number(&mut self, number: u32)
pub fn clear_field_by_number(&mut self, number: u32)
Clear by number; no-op when the number is unknown.
Sourcepub fn unknown_fields(
&self,
) -> impl Iterator<Item = (u32, &UnknownFieldSet)> + '_
pub fn unknown_fields( &self, ) -> impl Iterator<Item = (u32, &UnknownFieldSet)> + '_
Iterate every recorded unknown-field set in number order.
Sourcepub fn drain_unknown_fields(
&mut self,
) -> impl Iterator<Item = (u32, UnknownFieldSet)>
pub fn drain_unknown_fields( &mut self, ) -> impl Iterator<Item = (u32, UnknownFieldSet)>
Move every unknown-field set out of self. The returned vector
is in number order.
Trait Implementations§
Source§impl Clone for DynamicMessage
impl Clone for DynamicMessage
Source§fn clone(&self) -> DynamicMessage
fn clone(&self) -> DynamicMessage
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more