pub struct Writer<W> { /* private fields */ }
Expand description
A writer for SMC messages.
Consumes an futures::io::AsyncWrite
to which messages will be written.
Implementations§
Source§impl<W> Writer<W>where
W: AsyncWrite + Unpin,
impl<W> Writer<W>where
W: AsyncWrite + Unpin,
Sourcepub fn new(writer: W) -> Self
pub fn new(writer: W) -> Self
Create a new message writer.
Examples found in repository?
More examples
examples/echo_upper.rs (line 18)
14async fn echo() -> Result<(), io::Error> {
15 let stdin = io::stdin().lock().await;
16 let stdout = io::stdout().lock().await;
17 let mut reader = Reader::new(stdin);
18 let mut writer = Writer::new(stdout);
19 while let Some(msg) = reader.next().await {
20 let msg = msg?;
21 let resp = Message {
22 channel: msg.channel,
23 typ: msg.typ + 1,
24 message: to_upper(&msg.message),
25 };
26 writer.send(resp).await?;
27 }
28 Ok(())
29}
Sourcepub async fn send(&mut self, message: Message) -> Result<(), Error>
pub async fn send(&mut self, message: Message) -> Result<(), Error>
Send a message.
This encodes the message, writes it and flushes the writer.
Examples found in repository?
More examples
examples/tcp.rs (line 92)
83async fn handle_incoming(stream: TcpStream) -> Result<()> {
84 let (mut reader, mut writer) = create_from_stream(stream);
85 while let Some(msg) = reader.try_next().await? {
86 eprintln!("received: {}", format_msg(&msg));
87 let resp = Message {
88 channel: msg.channel,
89 typ: 2,
90 message: to_upper(&msg.message),
91 };
92 writer.send(resp).await?;
93 }
94 Ok(())
95}
96
97async fn handle_outgoing(stream: TcpStream) -> Result<()> {
98 let (mut reader, mut writer) = create_from_stream(stream);
99
100 let hello_msg = Message::new(1, 1, "hi".as_bytes().to_vec());
101 writer.send(hello_msg).await?;
102
103 while let Some(msg) = reader.try_next().await? {
104 eprintln!("received: {}", format_msg(&msg));
105 }
106
107 Ok(())
108}
examples/echo_upper.rs (line 26)
14async fn echo() -> Result<(), io::Error> {
15 let stdin = io::stdin().lock().await;
16 let stdout = io::stdout().lock().await;
17 let mut reader = Reader::new(stdin);
18 let mut writer = Writer::new(stdout);
19 while let Some(msg) = reader.next().await {
20 let msg = msg?;
21 let resp = Message {
22 channel: msg.channel,
23 typ: msg.typ + 1,
24 message: to_upper(&msg.message),
25 };
26 writer.send(resp).await?;
27 }
28 Ok(())
29}
Sourcepub async fn send_batch(&mut self, messages: Vec<Message>) -> Result<(), Error>
pub async fn send_batch(&mut self, messages: Vec<Message>) -> Result<(), Error>
Send a batch of messages.
This works like Writer::send
but flushes after all messages are written.
Auto Trait Implementations§
impl<W> Freeze for Writer<W>where
W: Freeze,
impl<W> RefUnwindSafe for Writer<W>where
W: RefUnwindSafe,
impl<W> Send for Writer<W>where
W: Send,
impl<W> Sync for Writer<W>where
W: Sync,
impl<W> Unpin for Writer<W>where
W: Unpin,
impl<W> UnwindSafe for Writer<W>where
W: UnwindSafe,
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
Mutably borrows from an owned value. Read more