pub struct Topic { /* private fields */ }Expand description
A gossip pub/sub topic.
Topics are identified by a BLAKE3 hash of their name string. When subscribed, incoming messages are validated (§1.4) and delivered to an inbox. Messages from blocked senders (§1.5) are dropped silently.
§Examples
use ma_core::Topic;
let topic = Topic::new("/ma/broadcast/0.0.1");
assert_eq!(topic.name(), "/ma/broadcast/0.0.1");
assert!(!topic.is_subscribed());Implementations§
Source§impl Topic
impl Topic
Sourcepub fn new(name: impl Into<String>) -> Self
pub fn new(name: impl Into<String>) -> Self
Create a new topic from a protocol-style name string.
The topic starts unsubscribed. Call subscribe to
begin receiving messages.
Sourcepub fn broadcast() -> Self
pub fn broadcast() -> Self
Create a topic for the well-known broadcast channel.
Equivalent to Topic::new("/ma/broadcast/0.0.1").
Sourcepub fn is_subscribed(&self) -> bool
pub fn is_subscribed(&self) -> bool
Whether this topic is currently subscribed (has an inbox).
Sourcepub fn subscribe(&mut self)
pub fn subscribe(&mut self)
Subscribe with a new internal inbox using the default capacity.
If already subscribed, this is a no-op.
Sourcepub fn subscribe_with(&mut self, inbox: Inbox<Message>)
pub fn subscribe_with(&mut self, inbox: Inbox<Message>)
Subscribe with an existing inbox, so messages from multiple sources converge into a single queue.
Replaces any previous inbox.
Sourcepub fn unsubscribe(&mut self)
pub fn unsubscribe(&mut self)
Unsubscribe — stop receiving messages and drop the inbox.
Sourcepub fn deliver(&mut self, message: Message) -> bool
pub fn deliver(&mut self, message: Message) -> bool
Deliver a message into this topic’s inbox after validation.
Returns true if the message was accepted, false if it was
rejected (wrong content type, has recipient, blocked sender,
expired, or not subscribed).
Sourcepub fn drain(&mut self) -> Vec<Message>
pub fn drain(&mut self) -> Vec<Message>
Drain all non-expired messages from the topic’s inbox.
Returns an empty Vec if not subscribed.
Sourcepub fn block(&mut self, sender_did: impl Into<String>)
pub fn block(&mut self, sender_did: impl Into<String>)
Block a sender DID. Messages from this sender will be dropped before any other validation.
Sourcepub fn is_blocked(&self, sender_did: &str) -> bool
pub fn is_blocked(&self, sender_did: &str) -> bool
Whether a sender DID is blocked.
Auto Trait Implementations§
impl Freeze for Topic
impl RefUnwindSafe for Topic
impl Send for Topic
impl Sync for Topic
impl Unpin for Topic
impl UnsafeUnpin for Topic
impl UnwindSafe for Topic
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more