Struct Multipart

Source
pub struct Multipart { /* private fields */ }
Expand description

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

extern crate zmq;
extern crate tokio_zmq;

use tokio_zmq::Multipart;

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

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

impl Envelope {
    fn from_multipart(mut multipart: Multipart) -> Result<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"));
    multipart.push_back(zmq::Message::from_slice(b"some.address"));
    multipart.push_back(zmq::Message::from_slice(b"Some content"));
    let envelope = Envelope::from_multipart(multipart).unwrap();

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

Implementations§

Source§

impl Multipart

Source

pub fn new() -> Self

Source

pub fn into_inner(self) -> VecDeque<Message>

Source

pub fn get(&self, index: usize) -> Option<&Message>

Source

pub fn pop_front(&mut self) -> Option<Message>

Source

pub fn pop_back(&mut self) -> Option<Message>

Source

pub fn push_front(&mut self, msg: Message)

Source

pub fn push_back(&mut self, msg: Message)

Source

pub fn is_empty(&self) -> bool

Source

pub fn iter(&self) -> Iter<'_, Message>

Source

pub fn len(&self) -> usize

Source

pub fn iter_mut(&mut self) -> IterMut<'_, Message>

Source

pub fn drain<R>(&mut self, range: R) -> Drain<'_, Message>
where R: RangeBounds<usize>,

Trait Implementations§

Source§

impl Debug for Multipart

Source§

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

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

impl Default for Multipart

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl From<Message> for Multipart

Source§

fn from(msg: Message) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<Message>> for Multipart

Source§

fn from(v: Vec<Message>) -> Self

Converts to this type from the input type.
Source§

impl<'a> IntoIterator for &'a Multipart

Source§

type Item = &'a Message

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, Message>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Iter<'a, Message>

Creates an iterator from a value. Read more
Source§

impl<'a> IntoIterator for &'a mut Multipart

Source§

type Item = &'a mut Message

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, Message>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> IterMut<'a, Message>

Creates an iterator from a value. Read more
Source§

impl IntoIterator for Multipart

Source§

type Item = Message

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<Message>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> IntoIter<Message>

Creates an iterator from a value. Read more

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> 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, 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.