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

source

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.

source

pub fn topic(&self) -> &str

Returns this ChannelEvent’s topic

source

pub fn event(&self) -> &str

Returns this ChannelEvent’s event

source

pub fn payload(&self) -> &Value

Returns this ChannelEvent’s payload as a Value

source

pub fn reference(&self) -> Option<&str>

Returns the reference field (“ref” in json) for this ChannelEvent, if one was provided by the client

source

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

source§

fn clone(&self) -> ChannelEvent

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for ChannelEvent

source§

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

Formats the value using the given formatter. Read more
source§

impl<'de> Deserialize<'de> for ChannelEvent

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<T, E> From<(T, E)> for ChannelEvent
where T: Into<Cow<'static, str>>, E: Into<Cow<'static, str>>,

source§

fn from(te: (T, E)) -> Self

Converts to this type from the input type.
source§

impl<T, E, P> From<(T, E, P)> for ChannelEvent
where T: Into<Cow<'static, str>>, E: Into<Cow<'static, str>>, P: Serialize,

source§

fn from(tep: (T, E, P)) -> Self

Converts to this type from the input type.
source§

impl PartialEq for ChannelEvent

source§

fn eq(&self, other: &ChannelEvent) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Serialize for ChannelEvent

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl Eq for ChannelEvent

source§

impl StructuralEq for ChannelEvent

source§

impl StructuralPartialEq for ChannelEvent

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
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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

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

§

type Output = T

Should always be Self
source§

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

§

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>,

§

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>,

§

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

source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,