pub struct DynamicMessage { /* private fields */ }
Expand description

DynamicMessage provides encoding, decoding and reflection of a protobuf message.

It wraps a MessageDescriptor and the Value for each field of the message, and implements Message.

Implementations§

Available on crate feature text-format only.

Parse a DynamicMessage from the given message encoded using the text format.

Examples
let dynamic_message = DynamicMessage::parse_text_format(message_descriptor, "foo: 150").unwrap();
assert_eq!(dynamic_message.get_field_by_name("foo").unwrap().as_ref(), &Value::I32(150));
Available on crate feature text-format only.

Merges the given message encoded using the text format into this message.

Examples
let mut dynamic_message = DynamicMessage::new(message_descriptor);
dynamic_message.merge_text_format("foo: 150").unwrap();
assert_eq!(dynamic_message.get_field_by_name("foo").unwrap().as_ref(), &Value::I32(150));
Available on crate feature text-format only.

Formats this dynamic message using the protobuf text format, with default options.

Examples
let dynamic_message = DynamicMessage::decode(message_descriptor, b"\x08\x96\x01\x1a\x02\x10\x42".as_ref()).unwrap();
assert_eq!(dynamic_message.to_text_format(), "foo:150,nested{bar:66}");
Available on crate feature text-format only.

Formats this dynamic message using the protobuf text format, with custom options.

Examples
let dynamic_message = DynamicMessage::decode(message_descriptor, b"\x08\x96\x01\x1a\x02\x10\x42".as_ref()).unwrap();
let options = FormatOptions::new().pretty(true);
assert_eq!(dynamic_message.to_text_format_with_options(&options), "foo: 150\nnested {\n  bar: 66\n}");
Available on crate feature serde only.

Serialize this message into serializer using the encoding specified by options.

Examples
let dynamic_message = DynamicMessage::new(message_descriptor);
let mut serializer = serde_json::Serializer::new(vec![]);
let mut options = SerializeOptions::new().skip_default_fields(false);
dynamic_message.serialize_with_options(&mut serializer, &options).unwrap();
assert_eq!(serializer.into_inner(), b"{\"foo\":0}");
Available on crate feature serde only.

Deserialize an instance of the message type described by desc from deserializer.

Examples
let json = r#"{ "foo": 150 }"#;
let mut deserializer = serde_json::de::Deserializer::from_str(json);
let dynamic_message = DynamicMessage::deserialize(message_descriptor, &mut deserializer).unwrap();
deserializer.end().unwrap();

assert_eq!(dynamic_message.get_field_by_name("foo").unwrap().as_ref(), &Value::I32(150));
Available on crate feature serde only.

Deserialize an instance of the message type described by desc from deserializer, using the encoding specified by options.

Examples
let json = r#"{ "foo": 150, "unknown": true }"#;
let mut deserializer = serde_json::de::Deserializer::from_str(json);
let options = DeserializeOptions::new().deny_unknown_fields(false);
let dynamic_message = DynamicMessage::deserialize_with_options(message_descriptor, &mut deserializer, &options).unwrap();
deserializer.end().unwrap();

assert_eq!(dynamic_message.get_field_by_name("foo").unwrap().as_ref(), &Value::I32(150));

Creates a new, empty instance of DynamicMessage for the message type specified by the MessageDescriptor.

Decodes an instance of the message type specified by the MessageDescriptor from the buffer and merges it into a new instance of DynamicMessage.

Examples
let dynamic_message = DynamicMessage::decode(message_descriptor, b"\x08\x96\x01".as_ref()).unwrap();
assert_eq!(dynamic_message.get_field_by_name("foo").unwrap().as_ref(), &Value::I32(150));

Returns true if this message has the given field set.

If the field type supports distinguishing whether a value has been set (see supports_presence), such as for messages, then this method returns true only if a value has been set. For other types, such as integers, it returns true if the value is set to a non-default value.

If this method returns false, then the field will not be included in the encoded bytes of this message.

Examples

This example uses the following message definition:

message MyMessage {
    int32 foo = 1;

    oneof optional {
        int32 bar = 2;
    }
}
let foo = message_descriptor.get_field_by_name("foo").unwrap();
let bar = message_descriptor.get_field_by_name("bar").unwrap();

assert!(!foo.supports_presence());
assert!(bar.supports_presence());

let mut dynamic_message = DynamicMessage::new(message_descriptor);
assert!(!dynamic_message.has_field(&foo));
assert!(!dynamic_message.has_field(&bar));

dynamic_message.set_field(&foo, Value::I32(0));
dynamic_message.set_field(&bar, Value::I32(0));
assert!(!dynamic_message.has_field(&foo));
assert!(dynamic_message.has_field(&bar));

dynamic_message.set_field(&foo, Value::I32(5));
dynamic_message.set_field(&bar, Value::I32(6));
assert!(dynamic_message.has_field(&foo));
assert!(dynamic_message.has_field(&bar));

Gets the value of the given field, or the default value if it is unset.

Gets a mutable reference to the value ofthe given field. If the field is not set, it is inserted with its default value.

Sets the value of the given field.

Panics

This method may panic if the value type is not compatible with the field type, as defined by Value::is_valid_for_field. Consider using try_set_field() for a non-panicking version.

Tries to set the value of the given field, returning an error if the value is an invalid type.

Examples
let mut dynamic_message = DynamicMessage::new(message_descriptor.clone());
let foo = message_descriptor.get_field_by_name("foo").unwrap();
assert_eq!(dynamic_message.try_set_field(&foo, Value::I32(5)), Ok(()));
assert_eq!(dynamic_message.try_set_field(&foo, Value::String("bar".to_owned())), Err(SetFieldError::InvalidType {
    field: foo,
    value: Value::String("bar".to_owned()),
}));

Clears the given field.

After calling this method, has_field will return false for the field, and it will not be included in the encoded bytes of this message.

Returns true if this message has a field set with the given number.

See has_field for more details.

Gets the value of the field with the given number, or the default value if it is unset.

If the message has no field with the given number, None is returned.

See get_field for more details.

Gets a mutable reference to the value of the field with the given number. If the field is not set, it is inserted with its default value.

If the message has no field with the given number, None is returned.

See get_field_mut for more details.

Sets the value of the field with number number.

If no field with the given number exists, this method does nothing.

See set_field for more details.

Tries to set the value of the field with number number, returning an error if the value is an invalid type or does not exist.

Examples
let mut dynamic_message = DynamicMessage::new(message_descriptor.clone());
assert_eq!(dynamic_message.try_set_field_by_number(1, Value::I32(5)), Ok(()));
assert_eq!(dynamic_message.try_set_field_by_number(1, Value::String("bar".to_owned())), Err(SetFieldError::InvalidType {
    field: message_descriptor.get_field(1).unwrap(),
    value: Value::String("bar".to_owned()),
}));
assert_eq!(dynamic_message.try_set_field_by_number(42, Value::I32(5)), Err(SetFieldError::NotFound));

Clears the field with the given number.

If no field with the given number exists, this method does nothing.

See clear_field for more details.

Returns true if this message has a field set with the given name.

See has_field for more details.

Gets the value of the field with the given name, or the default value if it is unset.

If the message has no field with the given name, None is returned.

See get_field for more details.

Gets a mutable reference to the value of the field with the given name. If the field is not set, it is inserted with its default value.

If the message has no field with the given name, None is returned.

See get_field_mut for more details.

Sets the value of the field with name name.

If no field with the given name exists, this method does nothing.

See set_field for more details.

Tries to set the value of the field with name name, returning an error if the value is an invalid type or does not exist.

Examples
let mut dynamic_message = DynamicMessage::new(message_descriptor.clone());
assert_eq!(dynamic_message.try_set_field_by_name("foo", Value::I32(5)), Ok(()));
assert_eq!(dynamic_message.try_set_field_by_name("foo", Value::String("bar".to_owned())), Err(SetFieldError::InvalidType {
    field: message_descriptor.get_field_by_name("foo").unwrap(),
    value: Value::String("bar".to_owned()),
}));
assert_eq!(dynamic_message.try_set_field_by_name("notfound", Value::I32(5)), Err(SetFieldError::NotFound));

Clears the field with the given name.

If no field with the given name exists, this method does nothing.

See clear_field for more details.

Returns true if this message has the given extension field set.

See has_field for more details.

Gets the value of the given extension field, or the default value if it is unset.

See get_field for more details.

Gets a mutable reference to the value of the given extension field. If the field is not set, it is inserted with its default value.

See get_field_mut for more details.

Sets the value of the given extension field.

See set_field for more details.

Clears the given extension field.

See clear_field for more details.

Merge a strongly-typed message into this one.

The message should be compatible with the type specified by descriptor, or the merge will likely fail with a DecodeError.

Convert this dynamic message into a strongly typed value.

The message should be compatible with the type specified by descriptor, or the conversion will likely fail with a DecodeError.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

Formats this message using the protobuf text format.

Examples
let dynamic_message = DynamicMessage::decode(message_descriptor, b"\x08\x96\x01\x1a\x02\x10\x42".as_ref()).unwrap();
assert_eq!(format!("{}", dynamic_message), "foo:150,nested{bar:66}");
// The alternate format specifier may be used to pretty-print the output
assert_eq!(format!("{:#}", dynamic_message), "foo: 150\nnested {\n  bar: 66\n}");
Returns the encoded length of the message without a length delimiter.
Clears the message, resetting all fields to their default.
Encodes the message to a buffer. Read more
Encodes the message to a newly allocated buffer.
Encodes the message with a length-delimiter to a buffer. Read more
Encodes the message with a length-delimiter to a newly allocated buffer.
Decodes an instance of the message from a buffer, and merges it into self. Read more
Decodes a length-delimited instance of the message from buffer, and merges it into self.
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Gets a MessageDescriptor describing the type of this message.
Converts this message into an instance of DynamicMessage by going through the byte representation.

Serialize this message into serializer using the canonical JSON encoding.

Examples
let dynamic_message = DynamicMessage::decode(message_descriptor, b"\x08\x96\x01".as_ref()).unwrap();
let mut serializer = serde_json::Serializer::new(vec![]);
dynamic_message.serialize(&mut serializer).unwrap();
assert_eq!(serializer.into_inner(), b"{\"foo\":150}");

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.