Skip to main content

Room

Struct Room 

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

A joined room.

Cheap to clone. The actor behind it runs while any clone exists, and leave ends it for all of them.

Implementations§

Source§

impl Room

Source

pub fn for_each_member<F, Fut>( &self, name: &str, task: F, ) -> AbortOnDropHandle<()> ⓘ
where F: FnMut(EndpointId) -> Fut + Send + 'static, Fut: Future<Output = ()> + Send + 'static,

Runs task for every member that publishes name, as the membership changes.

Starts task(member) once a member lists name, and aborts it once the member no longer does. A task that ends on its own is started again only after the member lists name anew, so a task that should outlast a closed broadcast retries itself, for example with subscriptions. Runs until the room is left, its last handle drops, or the returned handle drops. It holds no handle of the room itself, so it does not keep the room joined.

§Panics

Panics when called outside a Tokio runtime.

§Examples
let _readers = room.for_each_member("cam", |member| async move {
    println!("{} publishes a camera", member.fmt_short());
});
Source

pub fn subscriptions(&self, peer: EndpointId, name: &str) -> MemberSubscriptions

Returns the subscriptions to member peer’s broadcast name, a new one after each end while the member lists it.

The first next waits for the member to list name, however long, so bound the wait if it may never do. Each later one waits for the previous subscription to close, such as when the member restarts its broadcast or the session with it fails, and subscribes again. After a subscription that failed, or closed within sixteen seconds, it waits first, from two seconds up to sixteen, since a member that vanished stays listed for minutes.

The subscriptions end once the member stops listing name after it did, the room is left, or its last handle drops. They hold a handle of the room only while they subscribe, so they do not keep the room joined.

§Examples
let mut subscriptions = room.subscriptions(peer, "cam");
while let Some(subscription) = subscriptions.next().await {
    let _broadcast = subscription.broadcast();
    // Read the broadcast until it closes, then take the next one.
}
Source

pub fn read_values<T>(&self, name: &str, track: &str) -> MemberValues<T>
where T: DeserializeOwned + Send + 'static,

Reads track track of broadcast name from every member that publishes it.

The counterpart of publish_value: each member reports its newest value, and None once it no longer lists name. A member is read through subscriptions, so one whose subscription ends while it still lists name, such as one whose session failed, is read again. A value that does not decode is skipped, and a member whose broadcast has no track track is not read until it lists name anew. Dropping the returned MemberValues stops every reader. The readers do not keep the room joined.

§Panics

Panics when called outside a Tokio runtime.

§Examples
let mut presence = room.read_values::<String>("presence", "status");
while let Some(update) = presence.recv().await {
    let member = update.member.fmt_short();
    match update.value {
        Some(status) => println!("{member}: {}", status.value),
        None => println!("{member} left"),
    }
}
Source§

impl Room

Source

pub fn ticket(&self) -> RoomTicket

Returns a ticket for this room with this member as its bootstrap peer.

Source

pub fn state(&self) -> Direct<RoomState>

Returns the members, their broadcasts and connections, as they change.

Source

pub fn publish<B>(&self, name: &str) -> Result<B, Error>
where B: TryFrom<Producer, Error: Error + Send + Sync + 'static>,

Publishes a broadcast into the room as name, to its members only, and returns it.

The path is rooms/<this member>/<topic>/<name>. B is built from the route table’s broadcast::Producer: a LocalBroadcast, or the producer itself. The broadcast stays announced until it closes, unpublish takes it out, or the room is left.

§Errors

Fails with Error::InvalidName for an empty name or one holding a slash, Error::Node if the name is already published or B cannot be built, and Error::Left once the room was left.

§Panics

Panics when called outside a Tokio runtime.

Source

pub fn publish_value<T: Serialize>( &self, name: &str, track: &str, ) -> Result<ValueWriter<T>, Error>

Publishes a broadcast into the room as name whose one track track holds a latest-value JSON value.

For state the members share, such as presence, a cursor or a game: each update replaces the value, and a member that subscribes late gets the current one at once. Members read it from everyone with read_values, or from one member with subscribe and [Subscription::value_track]. The track exists before the broadcast is announced, so no member misses it, and the broadcast carries no media catalog. The last clone of the writer to drop finishes the track and closes the broadcast, which takes name out of the room. Otherwise it stays as publish describes.

§Errors

As publish.

§Panics

Panics when called outside a Tokio runtime.

§Examples
let presence = room.publish_value::<String>("presence", "status")?;
presence.update(&"typing".to_owned())?;
Source

pub fn unpublish(&self, name: &str) -> bool

Takes name out of the room, if this member publishes it.

The broadcast stays open for whoever holds it, unannounced. Returns whether name was published.

Source

pub async fn subscribe( &self, peer: EndpointId, name: &str, ) -> Result<Subscription, Error>

Resolves peer’s broadcast name over the session with that member.

Reads what the member itself announces on that session, so no other peer can stand in for it. Waits until the member announces name, so bound the wait if the member may never publish it.

§Errors

Fails with Error::Left once the room was left, and with Error::Node if the member cannot be dialed, the node’s policy does not grant the member its RoomsLayout scope, or the session ends before the member announces name.

Source

pub fn set_display_name(&self, name: Option<String>)

Sets the name other members see, or clears it.

Source

pub async fn leave(&self)

Leaves the room for every clone of this handle.

Tells the other members and withdraws this member’s publications. Subscriptions made with subscribe stay open, so drop them too. Returns within seconds even if every other member is gone. Idempotent. Not cancellation safe: call it again to finish.

Trait Implementations§

Source§

impl Clone for Room

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Room

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Room

§

impl !UnwindSafe for Room

§

impl Freeze for Room

§

impl Send for Room

§

impl Sync for Room

§

impl Unpin for Room

§

impl UnsafeUnpin for Room

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<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

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

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

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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

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