Skip to main content

DynamicMessage

Struct DynamicMessage 

Source
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

Source

pub fn new(desc: MessageDescriptor) -> Self

Create an empty message for the given descriptor.

Source

pub fn descriptor(&self) -> MessageDescriptor

The descriptor describing this message’s schema.

Source

pub fn unknown_fields(&self) -> &UnknownFields

Borrow the preserved unknown fields.

Source

pub fn unknown_fields_mut(&mut self) -> &mut UnknownFields

Mutably borrow the preserved unknown fields.

Source

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.

Source

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.

Source

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.

Source

pub fn clear_field(&mut self, field: &FieldDescriptor)

Clear a field, removing any stored value.

Source

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.

Source

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.

Source

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.

Source

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

Source

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).

Source

pub fn to_json_string(&self) -> Result<String, JsonError>

Encode this message to a canonical protobuf-JSON string.

§Errors

See DynamicMessage::to_json.

Source

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.

Source

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

Source

pub fn to_text(&self) -> Result<String, TextError>

Encode this message to the protobuf text format.

Proto3 default-valued singular fields are omitted.

§Errors

Returns TextError if encoding fails (e.g. an unsupported group field is encountered).

Source

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

Source

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.

Source

pub fn encode(&self, buf: &mut EncodeBuffer) -> Result<(), ReflectError>

Encode this message into an existing EncodeBuffer.

§Errors

See DynamicMessage::encode_to_vec.

Source

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

Source§

fn clone(&self) -> DynamicMessage

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for DynamicMessage

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for DynamicMessage

Source§

fn eq(&self, other: &DynamicMessage) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for DynamicMessage

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.