Skip to main content

NetData

Struct NetData 

Source
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: u64

The serialisation id of the data

Implementations§

Source§

impl NetData

Source

pub fn with(ser_id: u64, data: HeapOrSer) -> NetData

Create a new data instance from a serialisation id and some allocated data

Source

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),  
}
Source

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.

Source

pub fn ser_id(&self) -> &u64

Returns a reference to the serialisation id of this data

Trait Implementations§

Source§

impl Debug for NetData

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl TryClone for NetData

Source§

fn try_clone(&self) -> Result<NetData, SerError>

Tries to produce a copy of self or returns an error

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> DispatchEvent for T
where T: Any + Send + Debug,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any + Send>

Convert this event into an erased Any payload for downcasting.
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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> Erased for T

Source§

impl<M> MessageBounds for M
where M: Debug + Send + 'static,