Skip to main content

DynamicMessage

Struct DynamicMessage 

Source
pub struct DynamicMessage { /* private fields */ }
Available on crate feature 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

Source

pub fn new(desc: MessageDescriptor) -> Self

Construct an empty message of the given descriptor.

Source

pub fn descriptor(&self) -> MessageDescriptor

The owning descriptor.

Source

pub fn parent_pool(&self) -> DescriptorPool

The pool the descriptor was built from. Cloning is Arc-cheap.

Source

pub fn is_empty(&self) -> bool

True iff no known or unknown field has been recorded.

Source

pub fn decode<B: Buf>( desc: MessageDescriptor, buf: B, ) -> Result<Self, DecodeError>

Decode buf into a fresh DynamicMessage of the given descriptor.

§Errors

See DecodeError.

Source

pub fn decode_with_options<B: Buf>( desc: MessageDescriptor, buf: B, opts: DecodeOptions, ) -> Result<Self, DecodeError>

Decode with a custom buffa::DecodeOptions.

§Errors

See DecodeError.

Source

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.

Source

pub fn encoded_len(&self) -> usize

Encoded byte length, including unknown fields.

Source

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.

Source

pub fn encode_to_vec(&self) -> Vec<u8>

Encode to a fresh Vec<u8>.

Source

pub fn encode_to_bytes(&self) -> Bytes

Encode to a fresh Bytes.

Source

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.

Source

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.

Source

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.

Source

pub fn fields(&self) -> impl Iterator<Item = (FieldDescriptor, &Value)> + '_

Iterate every populated known field in field-number order.

Source

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.

Source

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.

Source

pub fn get_field(&self, field: &FieldDescriptor) -> Cow<'_, Value>

Read field — borrows the populated value or synthesises a default.

Source

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.

Source

pub fn has_field_by_name(&self, name: &str) -> bool

has_field keyed by proto name.

Source

pub fn get_field_by_name(&self, name: &str) -> Option<Cow<'_, Value>>

get_field keyed by proto name.

Source

pub fn get_field_by_name_mut(&mut self, name: &str) -> Option<&mut Value>

get_field_mut keyed by proto name.

Source

pub fn has_field_by_number(&self, number: u32) -> bool

has_field keyed by tag number.

Source

pub fn get_field_by_number(&self, number: u32) -> Option<Cow<'_, Value>>

get_field keyed by tag number.

Source

pub fn get_field_by_number_mut(&mut self, number: u32) -> Option<&mut Value>

get_field_mut keyed by tag number.

Source

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.

Source

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.

Source

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

Source

pub fn try_set_field_by_number( &mut self, number: u32, value: Value, ) -> Result<(), SetFieldError>

Validating _by_number form.

§Errors

Returns SetFieldError::NotFound when number is unknown.

Source

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

Source

pub fn try_set_field_by_name( &mut self, name: &str, value: Value, ) -> Result<(), SetFieldError>

Validating _by_name form.

§Errors

Returns SetFieldError::NotFound when name is unknown.

Source

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

Clear field (and any sibling oneof member).

Source

pub fn clear_field_by_name(&mut self, name: &str)

Clear by name; no-op when the name is unknown.

Source

pub fn clear_field_by_number(&mut self, number: u32)

Clear by number; no-op when the number is unknown.

Source

pub fn unknown_fields( &self, ) -> impl Iterator<Item = (u32, &UnknownFieldSet)> + '_

Iterate every recorded unknown-field set in number order.

Source

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

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 From<DynamicMessage> for Value

Source§

fn from(v: DynamicMessage) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for DynamicMessage

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl ReflectMessage 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.