[][src]Struct kompact::messaging::NetData

pub struct NetData {
    pub ser_id: SerId,
    // some fields omitted
}

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: SerId

The serialisation id of the data

Methods

impl NetData[src]

pub fn with(ser_id: SerId, data: HeapOrSer) -> Self[src]

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

pub fn try_deserialise<T: 'static, D>(self) -> Result<T, UnpackError<Self>> where
    D: Deserialiser<T>, 
[src]

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

pub fn try_deserialise_unchecked<T: 'static, D>(
    self
) -> Result<T, UnpackError<Self>> where
    D: Deserialiser<T>, 
[src]

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.

pub fn ser_id(&self) -> &SerId[src]

Returns a reference to the serialisation id of this data

Trait Implementations

impl Debug for NetData[src]

Auto Trait Implementations

impl !RefUnwindSafe for NetData

impl Send for NetData

impl !Sync for NetData

impl Unpin for NetData

impl !UnwindSafe for NetData

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

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