Struct Writer

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

Source

pub fn new(writer: W) -> Self

Create a new message writer.

Examples found in repository?
examples/tcp.rs (line 113)
110fn create_from_stream(tcp_stream: TcpStream) -> (Reader<CloneableStream>, Writer<CloneableStream>) {
111    let stream = CloneableStream(Arc::new(tcp_stream));
112    let reader = Reader::new(stream.clone());
113    let writer = Writer::new(stream.clone());
114    (reader, writer)
115}
More examples
Hide additional examples
examples/send.rs (line 10)
8async fn send() -> io::Result<()> {
9    let stdout = io::stdout().lock().await;
10    let mut writer = Writer::new(stdout);
11    for i in 0..3 {
12        let message = Message::new(i, 1, "hi".as_bytes().to_vec());
13        print_msg(&message);
14        writer.send(message).await?;
15    }
16    Ok(())
17}
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}
Source

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?
examples/send.rs (line 14)
8async fn send() -> io::Result<()> {
9    let stdout = io::stdout().lock().await;
10    let mut writer = Writer::new(stdout);
11    for i in 0..3 {
12        let message = Message::new(i, 1, "hi".as_bytes().to_vec());
13        print_msg(&message);
14        writer.send(message).await?;
15    }
16    Ok(())
17}
More examples
Hide additional 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}
Source

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> 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
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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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

Source§

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.