pub struct DynamicMessage { /* private fields */ }Expand description
A dynamically-typed protobuf message instance.
Construct one with DynamicMessage::new, populate it with
DynamicMessage::set_field, and serialise with
DynamicMessage::encode_to_vec. Decode bytes with
DynamicMessage::decode.
Two DynamicMessages compare equal when they share the same descriptor and
carry equal field values and unknown fields.
Implementations§
Source§impl DynamicMessage
impl DynamicMessage
Sourcepub fn new(desc: MessageDescriptor) -> Self
pub fn new(desc: MessageDescriptor) -> Self
Create an empty message for the given descriptor.
Sourcepub fn descriptor(&self) -> MessageDescriptor
pub fn descriptor(&self) -> MessageDescriptor
The descriptor describing this message’s schema.
Sourcepub fn unknown_fields(&self) -> &UnknownFields
pub fn unknown_fields(&self) -> &UnknownFields
Borrow the preserved unknown fields.
Sourcepub fn unknown_fields_mut(&mut self) -> &mut UnknownFields
pub fn unknown_fields_mut(&mut self) -> &mut UnknownFields
Mutably borrow the preserved unknown fields.
Sourcepub fn has_field(&self, field: &FieldDescriptor) -> bool
pub fn has_field(&self, field: &FieldDescriptor) -> bool
Returns true if the field is explicitly set to a non-default value.
For proto3 singular scalar fields, a value equal to the type default is considered not present. For message, repeated, and map fields, presence means the entry exists and is non-empty.
Sourcepub fn get_field(&self, field: &FieldDescriptor) -> Cow<'_, Value>
pub fn get_field(&self, field: &FieldDescriptor) -> Cow<'_, Value>
Get the value of a field, returning the field’s default (as an owned value) if it is not set.
The returned Cow borrows the stored value when present and owns a
freshly-constructed default otherwise.
Sourcepub fn set_field(&mut self, field: &FieldDescriptor, value: Value)
pub fn set_field(&mut self, field: &FieldDescriptor, value: Value)
Set the value of a field.
If the field is a member of a (real, non-synthetic) oneof, all sibling arms of that oneof are cleared first, enforcing oneof exclusivity.
Sourcepub fn clear_field(&mut self, field: &FieldDescriptor)
pub fn clear_field(&mut self, field: &FieldDescriptor)
Clear a field, removing any stored value.
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 a field by name. Returns None if the name is not a field of this
message’s descriptor.
Sourcepub fn set_field_by_name(&mut self, name: &str, value: Value) -> bool
pub fn set_field_by_name(&mut self, name: &str, value: Value) -> bool
Set a field by name. Returns false (and does nothing) if the name is
not a field of this message’s descriptor.
Sourcepub fn which_oneof(&self, oneof_name: &str) -> Option<FieldDescriptor>
pub fn which_oneof(&self, oneof_name: &str) -> Option<FieldDescriptor>
Returns the FieldDescriptor of whichever arm of oneof is set, if
any. oneof is identified by name.
Sourcepub fn iter_fields(
&self,
) -> impl Iterator<Item = (FieldDescriptor, &Value)> + '_
pub fn iter_fields( &self, ) -> impl Iterator<Item = (FieldDescriptor, &Value)> + '_
Iterate over the explicitly-set fields as (descriptor, value) pairs,
ordered by field number.
Source§impl DynamicMessage
impl DynamicMessage
Sourcepub fn to_json(&self) -> Result<Value, JsonError>
pub fn to_json(&self) -> Result<Value, JsonError>
Encode this message to canonical protobuf-JSON, returning a
serde_json::Value tree.
Proto3 default-valued singular scalar fields are omitted.
§Errors
Returns JsonError if the message contains an unsupported feature
(e.g. a group-kind field).
Sourcepub fn to_json_string(&self) -> Result<String, JsonError>
pub fn to_json_string(&self) -> Result<String, JsonError>
Sourcepub fn from_json(
desc: MessageDescriptor,
json: &Value,
) -> Result<Self, JsonError>
pub fn from_json( desc: MessageDescriptor, json: &Value, ) -> Result<Self, JsonError>
Decode a protobuf-JSON serde_json::Value into a new
DynamicMessage of the given descriptor.
Unknown JSON keys are silently skipped. null values are treated as
the type default (clearing the field).
§Errors
Returns JsonError if the JSON does not match the schema.
Sourcepub fn from_json_str(
desc: MessageDescriptor,
s: &str,
) -> Result<Self, JsonError>
pub fn from_json_str( desc: MessageDescriptor, s: &str, ) -> Result<Self, JsonError>
Decode a protobuf-JSON string into a new DynamicMessage of the
given descriptor.
§Errors
Returns JsonError if the string is not valid JSON or does not match
the schema.
Source§impl DynamicMessage
impl DynamicMessage
Sourcepub fn from_text(desc: MessageDescriptor, text: &str) -> Result<Self, TextError>
pub fn from_text(desc: MessageDescriptor, text: &str) -> Result<Self, TextError>
Decode a protobuf text-format string into a new DynamicMessage of
the given descriptor.
Unknown fields are silently skipped. Comments (lines starting with #)
are ignored.
§Errors
Returns TextError if the input cannot be parsed or does not match
the schema.
Source§impl DynamicMessage
impl DynamicMessage
Sourcepub fn encode_to_vec(&self) -> Result<Vec<u8>, ReflectError>
pub fn encode_to_vec(&self) -> Result<Vec<u8>, ReflectError>
Encode this message to a freshly-allocated byte vector.
Fields are written in ascending field-number order; unknown fields are appended afterwards. Proto3 singular fields equal to their type default are omitted.
§Errors
Returns ReflectError::Field if the message (or a nested message)
contains a group-kind field, which is unsupported.
Sourcepub fn encode(&self, buf: &mut EncodeBuffer) -> Result<(), ReflectError>
pub fn encode(&self, buf: &mut EncodeBuffer) -> Result<(), ReflectError>
Sourcepub fn decode(
desc: MessageDescriptor,
bytes: &[u8],
) -> Result<Self, ReflectError>
pub fn decode( desc: MessageDescriptor, bytes: &[u8], ) -> Result<Self, ReflectError>
Decode a message of the given descriptor from bytes.
Repeated scalar fields accept both packed and unpacked encodings. Fields whose numbers are not in the descriptor are preserved as unknown fields.
§Errors
Returns ReflectError::Field on malformed wire data (propagated from
the wire layer) or if a group-kind field is encountered.
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 moreSource§impl Debug for DynamicMessage
impl Debug for DynamicMessage
Source§impl PartialEq for DynamicMessage
impl PartialEq for DynamicMessage
Source§fn eq(&self, other: &DynamicMessage) -> bool
fn eq(&self, other: &DynamicMessage) -> bool
self and other values to be equal, and is used by ==.