pub struct ChannelEvent { /* private fields */ }Expand description
§The messages passed between server and connected clients.
ChannelEvents contain a topic, event, payload, and if sent from a client, a unique reference identifier that can be used to respond to this event.
Most interfaces in this crate take an Into<ChannelEvent> instead of a ChannelEvent directly,
so that you can either implement Into<ChannelEvent> for relevant types, or use these tuple
From implementations:
use trillium_channels::ChannelEvent;
use serde_json::{json, Value, to_string};
let event: ChannelEvent = ("topic", "event").into();
assert_eq!(event.topic(), "topic");
assert_eq!(event.event(), "event");
assert_eq!(event.payload(), &json!({}));
let event: ChannelEvent = ("topic", "event", &json!({"some": "payload"})).into();
assert_eq!(event.topic(), "topic");
assert_eq!(event.event(), "event");
assert_eq!(to_string(event.payload()).unwrap(), r#"{"some":"payload"}"#);
#[derive(serde::Serialize)]
struct SomePayload { payload: &'static str };
let event: ChannelEvent = ("topic", "event", &SomePayload { payload: "anything" }).into();
assert_eq!(event.topic(), "topic");
assert_eq!(event.event(), "event");
assert_eq!(to_string(event.payload()).unwrap(), r#"{"payload":"anything"}"#);Implementations§
Source§impl ChannelEvent
impl ChannelEvent
Sourcepub fn build_reply(
&self,
event: impl Into<Cow<'static, str>>,
payload: &impl Serialize,
) -> ChannelEvent
pub fn build_reply( &self, event: impl Into<Cow<'static, str>>, payload: &impl Serialize, ) -> ChannelEvent
Construct a new ChannelEvent with the same reference as this ChannelEvent. Note that this is the only way of setting the reference on an event.
The event argument can be either a String or, more commonly, a &'static str.
The topic will always be the same as the source ChannelEvent’s topic.
Sourcepub fn reference(&self) -> Option<&str>
pub fn reference(&self) -> Option<&str>
Returns the reference field (“ref” in json) for this ChannelEvent, if one was provided by the client
Sourcepub fn new(
topic: impl Into<Cow<'static, str>>,
event: impl Into<Cow<'static, str>>,
payload: &impl Serialize,
) -> Self
pub fn new( topic: impl Into<Cow<'static, str>>, event: impl Into<Cow<'static, str>>, payload: &impl Serialize, ) -> Self
Constructs a new ChannelEvent from topic, event, and a serializable payload. Use &() if no payload is needed.
Note that the reference cannot be set this way. To set a
reference, use ChannelEvent::build_reply
Trait Implementations§
Source§impl Clone for ChannelEvent
impl Clone for ChannelEvent
Source§fn clone(&self) -> ChannelEvent
fn clone(&self) -> ChannelEvent
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ChannelEvent
impl Debug for ChannelEvent
Source§impl<'de> Deserialize<'de> for ChannelEvent
impl<'de> Deserialize<'de> for ChannelEvent
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<T, E> From<(T, E)> for ChannelEvent
impl<T, E> From<(T, E)> for ChannelEvent
Source§impl<T, E, P> From<(T, E, P)> for ChannelEvent
impl<T, E, P> From<(T, E, P)> for ChannelEvent
Source§impl PartialEq for ChannelEvent
impl PartialEq for ChannelEvent
Source§impl Serialize for ChannelEvent
impl Serialize for ChannelEvent
impl Eq for ChannelEvent
impl StructuralPartialEq for ChannelEvent
Auto Trait Implementations§
impl Freeze for ChannelEvent
impl RefUnwindSafe for ChannelEvent
impl Send for ChannelEvent
impl Sync for ChannelEvent
impl Unpin for ChannelEvent
impl UnsafeUnpin for ChannelEvent
impl UnwindSafe for ChannelEvent
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.