Trait general_pub_sub::Client [−][src]
pub trait Client<TIdentifier: UniqueIdentifier, TMessage> { fn get_id(&self) -> TIdentifier; fn send(&mut self, message: &Message<'_, TMessage>); }
A PubSub Client
Trait describing a generic PubSub Client.
The identifier can be any data type so long as it conforms to
the UniqueIdentifier
trait.
Message can also be of any type.
Examples
Basic Usage:
struct BasicClient { id: u32 } impl Client<u32, &str> for BasicClient { fn get_id(&self) -> u32 { return self.id; } fn send(&self, message: &str) { println!("Client ({}) Received: {}", self.id, message); } }
Multi-client Example:
struct ConsoleClient { id: u32 } impl Client<u32, &str> for ConsoleClient { fn get_id(&self) -> u32 { return self.id; } fn send(&self, message: &str) { println!("Client ({}) Received: {}", self.id, message); } } struct TcpClient { id: &str, stream: std::net::TcpStream } impl Client<&str, &str> for TcpClient { fn get_id(&self) -> &str { return self.id; } fn send(&self, message: &str) { self.stream.write(format!("Client ({}) Received: {}", self.id, message).as_bytes()) } } enum Clients { Console(ConsoleClient), Tcp(TcpClient) } impl Client<&str, &str> for Clients { fn get_id(&self) -> &str { match self { Self::Console(client) => client.get_id().to_string(), Self::Tcp(client) => client.get_id() } } fn send(&self, message: &str) { match self { Self::Console(client) => client.send(message), Self::Console(client) => client.send(message) } } }
Required methods
fn get_id(&self) -> TIdentifier
[src]
Gets the ID
of the Client
. Must be unique.
fn send(&mut self, message: &Message<'_, TMessage>)
[src]
Sends a Message
to a Client
.