Struct nats::subscription::Subscription[][src]

pub struct Subscription(_);
Expand description

A Subscription receives Messages published to specific NATS Subjects.

Implementations

impl Subscription[src]

pub fn receiver(&self) -> &Receiver<Message>[src]

Get a crossbeam Receiver for subscription messages. Useful for crossbeam_channel::select macro

Example

let sub1_ch = sub1.receiver();
let sub2_ch = sub2.receiver();
crossbeam_channel::select! {
    recv(sub1_ch) -> msg => {
        println!("Got message from sub1: {:?}", msg);
        Ok(())
    }
    recv(sub2_ch) -> msg => {
        println!("Got message from sub2: {:?}", msg);
        Ok(())
    }
}

pub fn next(&self) -> Option<Message>[src]

Get the next message, or None if the subscription has been unsubscribed or the connection closed.

Example

if let Some(msg) = sub.next() {}

pub fn try_next(&self) -> Option<Message>[src]

Try to get the next message, or None if no messages are present or if the subscription has been unsubscribed or the connection closed.

Example

if let Some(msg) = sub.try_next() {
  println!("Received {}", msg);
}

pub fn next_timeout(&self, timeout: Duration) -> Result<Message>[src]

Get the next message, or a timeout error if no messages are available for timout.

Example

if let Ok(msg) = sub.next_timeout(std::time::Duration::from_secs(1)) {}

pub fn messages(&self) -> Iter<'_>

Notable traits for Iter<'a>

impl<'a> Iterator for Iter<'a> type Item = Message;
[src]

Returns a blocking message iterator. Same as calling iter().

Example

for msg in sub.messages() {}

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

Notable traits for Iter<'a>

impl<'a> Iterator for Iter<'a> type Item = Message;
[src]

Returns a blocking message iterator.

Example

for msg in sub.iter() {}

pub fn try_iter(&self) -> TryIter<'_>

Notable traits for TryIter<'a>

impl<'a> Iterator for TryIter<'a> type Item = Message;
[src]

Returns a non-blocking message iterator.

Example

for msg in sub.try_iter() {}

pub fn timeout_iter(&self, timeout: Duration) -> TimeoutIter<'_>

Notable traits for TimeoutIter<'a>

impl<'a> Iterator for TimeoutIter<'a> type Item = Message;
[src]

Returns a blocking message iterator with a time deadline for blocking.

Example

for msg in sub.timeout_iter(std::time::Duration::from_secs(1)) {}

pub fn with_handler<F>(self, handler: F) -> Handler where
    F: Fn(Message) -> Result<()> + Send + 'static, 
[src]

Attach a closure to handle messages. This closure will execute in a separate thread. The result of this call is a Handler which can not be iterated and must be unsubscribed or closed directly to unregister interest. A Handler will not unregister interest with the server when drop(&mut self) is called.

Example

nc.subscribe("bar")?.with_handler(move |msg| {
    println!("Received {}", &msg);
    Ok(())
});

pub fn unsubscribe(self) -> Result<()>[src]

Unsubscribe a subscription immediately without draining. Use drain instead if you want any pending messages to be processed by a handler, if one is configured.

Example

let sub = nc.subscribe("foo")?;
sub.unsubscribe()?;

pub fn close(self) -> Result<()>[src]

Close a subscription. Same as unsubscribe

Use drain instead if you want any pending messages to be processed by a handler, if one is configured.

Example

let sub = nc.subscribe("foo")?;
sub.close()?;

pub fn drain(&self) -> Result<()>[src]

Send an unsubscription then flush the connection, allowing any unprocessed messages to be handled by a handler function if one is configured.

After the flush returns, we know that a round-trip to the server has happened after it received our unsubscription, so we shut down the subscriber afterwards.

A similar method exists on the Connection struct which will drain all subscriptions for the NATS client, and transition the entire system into the closed state afterward.

Example


let mut sub = nc.subscribe("test.drain")?;

nc.publish("test.drain", "message")?;
sub.drain()?;

let mut received = false;
for _ in sub {
    received = true;
}

assert!(received);

Trait Implementations

impl Clone for Subscription[src]

fn clone(&self) -> Subscription[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl Debug for Subscription[src]

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

Formats the value using the given formatter. Read more

impl IntoIterator for Subscription[src]

type Item = Message

The type of the elements being iterated over.

type IntoIter = IntoIter

Which kind of iterator are we turning this into?

fn into_iter(self) -> IntoIter

Notable traits for IntoIter

impl Iterator for IntoIter type Item = Message;
[src]

Creates an iterator from a value. Read more

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

type Item = Message

The type of the elements being iterated over.

type IntoIter = Iter<'a>

Which kind of iterator are we turning this into?

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

Notable traits for Iter<'a>

impl<'a> Iterator for Iter<'a> type Item = Message;
[src]

Creates an iterator from a value. Read more

Auto Trait Implementations

Blanket Implementations

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

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

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

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

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

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

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

pub fn from(t: T) -> T[src]

Performs the conversion.

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

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

Creates owned data from borrowed data, usually by cloning. Read more

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

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.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

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.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.

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

pub fn vzip(self) -> V