Consumer

Struct Consumer 

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

Thread-safe Consumer type with a group number representing the consumer group.

Use try_clone to make copies of the Consumer in the same group or use subscribe on a producer to create new Consumers.

let mut tx = disk_chan::new("foo", 2_u32.pow(24), 1).await?;
let mut rx = tx.subscribe(0).await?;
let mut rx2 = rx.try_clone().await?;

let msg = loop {
    match rx.recv().await {
        Some(msg) => break msg,
        None => rx.next_page().await?,
    }
};

Implementations§

Source§

impl Consumer

Source

pub async fn try_clone(&self) -> Result<Self, Error>

Attempts to clone the current Consumer with the same group.

This can potentially fail to do interactions with the file system, but should be near impossible with correct usage.

Source

pub async fn recv(&self) -> Option<&[u8]>

Asynchronously receives the next message in the consumer group.

Returns None when the consumer has read all messages from the current page, and the user must call next_page to continue reading.

This is necessary due to the message’s lifetime being tied to the consumer’s current page so it must not be accessed after a call to next_page. Thus, it is up to the user to ensure the compiler is happy with lifetimes.

let mut tx = disk_chan::new("foo", 2_u32.pow(24), 1).await?;
let mut rx = tx.subscribe(0).await?;
let mut rx2 = rx.try_clone().await?;

let msg = loop {
    match rx.recv().await {
        Some(msg) => break msg,
        None => rx.next_page().await?,
    }
};
Source

pub async fn next_page(&mut self) -> Result<(), Error>

Sets the internal page to the next available one.

Should be called after recv returns None.

let mut tx = disk_chan::new("foo", 2_u32.pow(24), 1).await?;
let mut rx = tx.subscribe(0).await?;
let mut rx2 = rx.try_clone().await?;

let msg = loop {
    match rx.recv().await {
        Some(msg) => break msg,
        None => rx.next_page().await?,
    }
};

Trait Implementations§

Source§

impl Debug for Consumer

Source§

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

Formats the value using the given formatter. 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.