Struct tokio_zmq::Multipart [] [src]

pub struct Multipart { /* fields omitted */ }

This type is used for receiving and sending messages in Multipart groups. An application could make using this easier by implementing traits as follows:

#![feature(try_from)]

extern crate zmq;
extern crate tokio_zmq;

use std::convert::{TryFrom, TryInto};

use tokio_zmq::Multipart;

#[derive(Debug)]
enum Error {
    NotEnoughMessages,
    TooManyMessages,
}

struct Envelope {
    filter: zmq::Message,
    address: zmq::Message,
    body: zmq::Message,
}

impl TryFrom<Multipart> for Envelope {
    type Error = Error;

    fn try_from(mut multipart: Multipart) -> Result<Self, Self::Error> {
        let filter = multipart.pop_front().ok_or(Error::NotEnoughMessages)?;
        let address = multipart.pop_front().ok_or(Error::NotEnoughMessages)?;
        let body = multipart.pop_front().ok_or(Error::NotEnoughMessages)?;

        if !multipart.is_empty() {
            return Err(Error::TooManyMessages);
        }

        Ok(Envelope {
            filter,
            address,
            body,
        })
    }
}

impl From<Envelope> for Multipart {
    fn from(envelope: Envelope) -> Self {
        let mut multipart = Multipart::new();

        multipart.push_back(envelope.filter);
        multipart.push_back(envelope.address);
        multipart.push_back(envelope.body);

        multipart
    }
}

fn main() {
    let mut multipart: Multipart = Multipart::new();
    multipart.push_back(zmq::Message::from_slice(b"FILTER: asdf").unwrap());
    multipart.push_back(zmq::Message::from_slice(b"some.address").unwrap());
    multipart.push_back(zmq::Message::from_slice(b"Some content").unwrap());
    let envelope: Envelope = multipart.try_into().unwrap();

    let multipart2: Multipart = envelope.into();
}

Methods

impl Multipart
[src]

[src]

[src]

[src]

[src]

[src]

[src]

[src]

[src]

[src]

[src]

Trait Implementations

impl Debug for Multipart
[src]

[src]

Formats the value using the given formatter.

impl Default for Multipart
[src]

[src]

Returns the "default value" for a type. Read more

impl From<Message> for Multipart
[src]

[src]

Performs the conversion.

impl From<Vec<Message>> for Multipart
[src]

[src]

Performs the conversion.

impl<'a> IntoIterator for &'a Multipart
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

[src]

Creates an iterator from a value. Read more

impl IntoIterator for Multipart
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

[src]

Creates an iterator from a value. Read more

impl<'a> IntoIterator for &'a mut Multipart
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

[src]

Creates an iterator from a value. Read more