pub struct UPayload { /* private fields */ }
Expand description
A wrapper around (raw) message payload data and the corresponding payload format.
Implementations§
Source§impl UPayload
impl UPayload
Sourcepub fn new<T: Into<Bytes>>(payload: T, payload_format: UPayloadFormat) -> Self
pub fn new<T: Into<Bytes>>(payload: T, payload_format: UPayloadFormat) -> Self
Creates a new payload for some data.
§Examples
use up_rust::UPayloadFormat;
use up_rust::communication::UPayload;
let data: Vec<u8> = vec![0x00_u8, 0x01_u8, 0x02_u8];
let payload = UPayload::new(data, UPayloadFormat::UPAYLOAD_FORMAT_RAW);
assert_eq!(payload.payload_format(), UPayloadFormat::UPAYLOAD_FORMAT_RAW);
assert_eq!(payload.payload().len(), 3);
Sourcepub fn try_from_protobuf<M>(message: M) -> Result<Self, UMessageError>where
M: MessageFull,
pub fn try_from_protobuf<M>(message: M) -> Result<Self, UMessageError>where
M: MessageFull,
Creates a new UPayload from a protobuf message.
The resulting payload will have UPayloadType::UPAYLOAD_FORMAT_PROTOBUF_WRAPPED_IN_ANY
.
§Errors
Returns an error if the given message cannot be serialized to bytes.
§Examples
use up_rust::{communication::UPayload, UPayloadFormat};
use protobuf::{well_known_types::wrappers::StringValue};
let mut data = StringValue::new();
data.value = "hello world".to_string();
assert!(UPayload::try_from_protobuf(data).is_ok_and(|pl|
pl.payload_format() == UPayloadFormat::UPAYLOAD_FORMAT_PROTOBUF_WRAPPED_IN_ANY
&& pl.payload().len() > 0));
Sourcepub fn payload_format(&self) -> UPayloadFormat
pub fn payload_format(&self) -> UPayloadFormat
Sourcepub fn extract_protobuf<T: MessageFull + Default>(
&self,
) -> Result<T, UMessageError>
pub fn extract_protobuf<T: MessageFull + Default>( &self, ) -> Result<T, UMessageError>
Extracts the protobuf Message
contained in payload.
This function is used to extract strongly-typed data from a UPayload
object,
taking into account the payload format (will only succeed if payload format is
UPayloadFormat::UPAYLOAD_FORMAT_PROTOBUF
or UPayloadFormat::UPAYLOAD_FORMAT_PROTOBUF_WRAPPED_IN_ANY
)
§Type Parameters
T
: The target type of the data to be unpacked.
§Returns
Ok(T)
: The deserialized protobufMessage
contained in the payload.
§Errors
- Err(
UMessageError
) if the unpacking process fails, for example if the payload could not be deserialized into the target typeT
.
§Examples
use up_rust::{communication::UPayload, UPayloadFormat};
use protobuf::{well_known_types::wrappers::StringValue};
let mut data = StringValue::new();
data.value = "hello world".to_string();
let payload = UPayload::try_from_protobuf(data).expect("should be able to create UPayload from StringValue");
let string_value: StringValue = payload.extract_protobuf().expect("should be able to extract StringValue from UPayload");
assert_eq!(string_value.value, *"hello world");
Trait Implementations§
impl StructuralPartialEq for UPayload
Auto Trait Implementations§
impl !Freeze for UPayload
impl RefUnwindSafe for UPayload
impl Send for UPayload
impl Sync for UPayload
impl Unpin for UPayload
impl UnwindSafe for UPayload
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more