File: basic.rs

package info (click to toggle)
rust-futures-ringbuf 0.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 272 kB
  • sloc: makefile: 2
file content (53 lines) | stat: -rw-r--r-- 1,232 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//! Frame a RingBuf with futures_codec. This example shows how the sending task will block when the buffer is full.
//! When a reader consumes the buffer, the sender is woken up.
//!
//! Run with `cargo run --example basic`.
//
use
{
	futures_ringbuf    :: { *                        } ,
	futures            :: { SinkExt, StreamExt, join } ,
	asynchronous_codec :: { Framed, LinesCodec       } ,
};


#[async_std::main]
//
async fn main()
{
	let mock = RingBuffer::new( 13 );
	let (mut writer, mut reader) = Framed::new( mock, LinesCodec{} ).split();


	let send_task = async move
	{
		writer.send( "Hello World\n".to_string() ).await.expect( "send" );
		println!( "sent first line" );

		writer.send( "Second line\n".to_string() ).await.expect( "send" );
		println!( "sent second line" );

		writer.close().await.expect( "close sender" );
		println!( "sink closed" );
	};


	let receive_task = async move
	{
		// If we would return here, the second line will never get sent because the buffer is full.
		//
		// return;

		while let Some(msg) = reader.next().await.transpose().expect( "receive message" )
		{
			println!( "Received: {:#?}", msg );
		}
	};


	// Poll them in concurrently
	//
	join!( send_task, receive_task );
}