azalea

Struct TickBroadcast

Source
pub struct TickBroadcast(/* private fields */);
Expand description

A resource that contains a [broadcast::Sender] that will be sent every Minecraft tick.

This is useful for running code every schedule from async user code.

use azalea_client::TickBroadcast;
let mut receiver = {
    let ecs = client.ecs.lock();
    let tick_broadcast = ecs.resource::<TickBroadcast>();
    tick_broadcast.subscribe()
};
while receiver.recv().await.is_ok() {
    // do something
}

Methods from Deref<Target = Sender<()>>§

pub fn send(&self, value: T) -> Result<usize, SendError<T>>

Attempts to send a value to all active Receiver handles, returning it back if it could not be sent.

A successful send occurs when there is at least one active Receiver handle. An unsuccessful send would be one where all associated Receiver handles have already been dropped.

§Return

On success, the number of subscribed Receiver handles is returned. This does not mean that this number of receivers will see the message as a receiver may drop or lag (see lagging) before receiving the message.

§Note

A return value of Ok does not mean that the sent value will be observed by all or any of the active Receiver handles. Receiver handles may be dropped before receiving the sent message.

A return value of Err does not mean that future calls to send will fail. New Receiver handles may be created by calling subscribe.

§Examples
use tokio::sync::broadcast;

#[tokio::main]
async fn main() {
    let (tx, mut rx1) = broadcast::channel(16);
    let mut rx2 = tx.subscribe();

    tokio::spawn(async move {
        assert_eq!(rx1.recv().await.unwrap(), 10);
        assert_eq!(rx1.recv().await.unwrap(), 20);
    });

    tokio::spawn(async move {
        assert_eq!(rx2.recv().await.unwrap(), 10);
        assert_eq!(rx2.recv().await.unwrap(), 20);
    });

    tx.send(10).unwrap();
    tx.send(20).unwrap();
}

pub fn subscribe(&self) -> Receiver<T>

Creates a new [Receiver] handle that will receive values sent after this call to subscribe.

§Examples
use tokio::sync::broadcast;

#[tokio::main]
async fn main() {
    let (tx, _rx) = broadcast::channel(16);

    // Will not be seen
    tx.send(10).unwrap();

    let mut rx = tx.subscribe();

    tx.send(20).unwrap();

    let value = rx.recv().await.unwrap();
    assert_eq!(20, value);
}

pub fn len(&self) -> usize

Returns the number of queued values.

A value is queued until it has either been seen by all receivers that were alive at the time it was sent, or has been evicted from the queue by subsequent sends that exceeded the queue’s capacity.

§Note

In contrast to [Receiver::len], this method only reports queued values and not values that have been evicted from the queue before being seen by all receivers.

§Examples
use tokio::sync::broadcast;

#[tokio::main]
async fn main() {
    let (tx, mut rx1) = broadcast::channel(16);
    let mut rx2 = tx.subscribe();

    tx.send(10).unwrap();
    tx.send(20).unwrap();
    tx.send(30).unwrap();

    assert_eq!(tx.len(), 3);

    rx1.recv().await.unwrap();

    // The len is still 3 since rx2 hasn't seen the first value yet.
    assert_eq!(tx.len(), 3);

    rx2.recv().await.unwrap();

    assert_eq!(tx.len(), 2);
}

pub fn is_empty(&self) -> bool

Returns true if there are no queued values.

§Examples
use tokio::sync::broadcast;

#[tokio::main]
async fn main() {
    let (tx, mut rx1) = broadcast::channel(16);
    let mut rx2 = tx.subscribe();

    assert!(tx.is_empty());

    tx.send(10).unwrap();

    assert!(!tx.is_empty());

    rx1.recv().await.unwrap();

    // The queue is still not empty since rx2 hasn't seen the value.
    assert!(!tx.is_empty());

    rx2.recv().await.unwrap();

    assert!(tx.is_empty());
}

pub fn receiver_count(&self) -> usize

Returns the number of active receivers.

An active receiver is a Receiver handle returned from channel or subscribe. These are the handles that will receive values sent on this Sender.

§Note

It is not guaranteed that a sent message will reach this number of receivers. Active receivers may never call recv again before dropping.

§Examples
use tokio::sync::broadcast;

#[tokio::main]
async fn main() {
    let (tx, _rx1) = broadcast::channel(16);

    assert_eq!(1, tx.receiver_count());

    let mut _rx2 = tx.subscribe();

    assert_eq!(2, tx.receiver_count());

    tx.send(10).unwrap();
}

pub fn same_channel(&self, other: &Sender<T>) -> bool

Returns true if senders belong to the same channel.

§Examples
use tokio::sync::broadcast;

#[tokio::main]
async fn main() {
    let (tx, _rx) = broadcast::channel::<()>(16);
    let tx2 = tx.clone();

    assert!(tx.same_channel(&tx2));

    let (tx3, _rx3) = broadcast::channel::<()>(16);

    assert!(!tx3.same_channel(&tx2));
}

Trait Implementations§

Source§

impl Deref for TickBroadcast

Source§

type Target = Sender<()>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &<TickBroadcast as Deref>::Target

Dereferences the value.
Source§

impl Resource for TickBroadcast
where TickBroadcast: Send + Sync + 'static,

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
§

impl<T> AsAny for T
where T: Any,

§

fn as_any(&self) -> &(dyn Any + 'static)

§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

§

fn type_name(&self) -> &'static str

Gets the type name of self
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
§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
§

impl<T> Downcast for T
where T: AsAny + ?Sized,

§

fn is<T>(&self) -> bool
where T: AsAny,

Returns true if the boxed type is the same as T. Read more
§

fn downcast_ref<T>(&self) -> Option<&T>
where T: AsAny,

Forward to the method defined on the type Any.
§

fn downcast_mut<T>(&mut self) -> Option<&mut T>
where T: AsAny,

Forward to the method defined on the type Any.
§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Same for T

Source§

type Output = T

Should always be Self
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.
§

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

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more