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::Raw);
assert_eq!(payload.payload_format(), UPayloadFormat::Raw);
assert_eq!(payload.payload().len(), 3);Sourcepub fn try_from_protobuf<T>(obj: T) -> Result<Self, UMessageError>where
T: ProtobufMappable,
pub fn try_from_protobuf<T>(obj: T) -> Result<Self, UMessageError>where
T: ProtobufMappable,
Creates a new UPayload from an object that can be mapped to/from a protobuf.
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::ProtobufWrappedInAny
&& pl.payload().len() > 0));Sourcepub fn payload_format(&self) -> UPayloadFormat
pub fn payload_format(&self) -> UPayloadFormat
Sourcepub fn extract_protobuf<T>(&self) -> Result<T, UMessageError>where
T: ProtobufMappable + Default,
pub fn extract_protobuf<T>(&self) -> Result<T, UMessageError>where
T: ProtobufMappable + Default,
Extracts the object that is contained in this message’s payload.
§Type Parameters
T: The target type of the data to be unpacked.
§Returns
- The deserialized object contained in the payload.
§Errors
- Returns an error if the unpacking process fails, for example if the payload format
is neither
UPayloadFormat::ProtobufnorUPayloadFormat::ProtobufWrappedInAny, or 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§
§impl<T> AnySync for T
impl<T> AnySync for T
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<TCore> UWithNativePrefixWire for TCore
impl<TCore> UWithNativePrefixWire for TCore
Source§fn into_native_prefix_wire_transport<W>(
self,
wire: W,
) -> UWireTransport<TCore, W, NativePrefixFrameMetadataCodec>where
W: UWire,
fn into_native_prefix_wire_transport<W>(
self,
wire: W,
) -> UWireTransport<TCore, W, NativePrefixFrameMetadataCodec>where
W: UWire,
Wraps this core with an external or custom selected wire using canonical metadata.
Source§fn into_protobuf_transport(
self,
) -> UWireTransport<TCore, ProtobufWire, NativePrefixFrameMetadataCodec>
fn into_protobuf_transport( self, ) -> UWireTransport<TCore, ProtobufWire, NativePrefixFrameMetadataCodec>
Wraps this core with the Protocol Buffers selected-wire profile.
Source§fn into_stable_container_transport(
self,
) -> UWireTransport<TCore, StableContainerWireFormat, NativePrefixFrameMetadataCodec>
fn into_stable_container_transport( self, ) -> UWireTransport<TCore, StableContainerWireFormat, NativePrefixFrameMetadataCodec>
Wraps this core with the stable-container selected-wire profile.