pub struct NetData {
pub ser_id: u64,
/* private fields */
}Expand description
The data part of an incoming message from the networking subsystem
A NetData instance can either represent serialised data
or heap-allocated “reflected” data cast to Box<Any>.
This is because messages are “reflected” instead of serialised whenever possible
for messages sent to an ActorPath that turnes out to be in the same
KompactSystem.
Whether serialised or heap-allocated, network data always comes with a serialisation id of type SerId that can be used to identify the underlying type with a simple lookup.
It is recommend to use try_deserialise or
try_deserialise_unchecked to unpack
the contained data to the appropriate type, instead of accessing the data field directly.
These methods abstract over whether or not the data is actually serialised
or simply heap-allocated.
Fields§
§ser_id: u64The serialisation id of the data
Implementations§
Source§impl NetData
impl NetData
Sourcepub fn with(ser_id: u64, data: HeapOrSer) -> NetData
pub fn with(ser_id: u64, data: HeapOrSer) -> NetData
Create a new data instance from a serialisation id and some allocated data
Sourcepub fn try_deserialise<T, D>(self) -> Result<T, UnpackError<NetData>>where
T: 'static,
D: Deserialiser<T>,
pub fn try_deserialise<T, D>(self) -> Result<T, UnpackError<NetData>>where
T: 'static,
D: Deserialiser<T>,
Try to deserialise the data into a value of type T
This method attempts to deserialise the contents into an
instance of T using the deserialiser D.
It will only do so after verifying that ser_id == D::SER_ID.
If the serialisation id does not match, this message is returned unaltered wrapped in an UnpackError.
§Example
use kompact::prelude::*;
use bytes::BytesMut;
let test_str = "Test me".to_string();
// serialise the string
let mut mbuf = BytesMut::with_capacity(test_str.size_hint().expect("size hint"));
test_str.serialise(&mut mbuf).expect("serialise");
// create a net message
let buf = mbuf.freeze();
let msg = NetMessage::with_bytes(String::SER_ID, some_path, some_path2, buf);
// try to deserialise it again
match msg.try_deserialise::<u64, u64>() {
Ok(_) => unreachable!("It's definitely not a u64..."),
Err(UnpackError::NoIdMatch(msg_again)) => {
match msg_again.try_deserialise::<String, String>() {
Ok(test_res) => assert_eq!(test_str, test_res),
Err(_) => unreachable!("It's definitely a string..."),
}
}
Err(error) => panic!("Not the error we expected: {:?}", error),
}Sourcepub fn try_deserialise_unchecked<T, D>(self) -> Result<T, UnpackError<NetData>>where
T: 'static,
D: Deserialiser<T>,
pub fn try_deserialise_unchecked<T, D>(self) -> Result<T, UnpackError<NetData>>where
T: 'static,
D: Deserialiser<T>,
Try to deserialise the data into a value of type T
This method attempts to deserialise the contents into an
instance of T using the deserialiser D
without checking the ser_id first for a match.
Only use this, if you have already verified that ser_id == D::SER_ID!
Otherwise use try_deserialise.
§Example
use kompact::prelude::*;
use bytes::BytesMut;
let test_str = "Test me".to_string();
// serialise the string
let mut mbuf = BytesMut::with_capacity(test_str.size_hint().expect("size hint"));
test_str.serialise(&mut mbuf).expect("serialise");
// create a net message
let buf = mbuf.freeze();
let msg = NetMessage::with_bytes(String::SER_ID, some_path, some_path2, buf);
// try to deserialise it again
match msg.ser_id() {
&u64::SER_ID => unreachable!("It's definitely not a u64..."),
&String::SER_ID => {
let test_res = msg.data.try_deserialise_unchecked::<String, String>().expect("deserialised");
assert_eq!(test_str, test_res);
}
_ => unreachable!("It's definitely not...whatever this is..."),
}§Note
The match_deser macro generates code that is approximately equivalent to the example above with some nicer syntax.