1+ pub mod primitives;
2+
3+ pub use primitives:: * ;
4+
5+ use std:: process:: Stdio ;
6+
7+ use tokio;
8+ use tokio:: process:: Command ;
9+ use tokio:: io:: { BufReader , AsyncBufReadExt } ;
10+
11+ pub fn process ( ) {
12+ let rt = tokio:: runtime:: Runtime :: new ( ) . unwrap ( ) ;
13+
14+ let _: Result < ( ) , Box < dyn std:: error:: Error > > = rt. block_on ( async {
15+ let mut child = Command :: new ( "echo" )
16+ . arg ( "hello" )
17+ . arg ( "world" )
18+ . spawn ( )
19+ . expect ( "failed to spawn" ) ;
20+
21+ // Await until the command completes
22+ let status = child. wait ( ) . await ?;
23+ println ! ( "the command exited with: {}" , status) ;
24+
25+ Ok ( ( ) )
26+ } ) ;
27+ }
28+
29+ pub fn process2 ( ) {
30+ let rt = tokio:: runtime:: Runtime :: new ( ) . unwrap ( ) ;
31+
32+ let _: Result < ( ) , Box < dyn std:: error:: Error > > = rt. block_on ( async {
33+ let mut cmd = Command :: new ( "cat" ) ;
34+ cmd. arg ( "Cargo.toml" ) ;
35+
36+ // Specify that we want the command's standard output piped back to us.
37+ // By default, standard input/output/error will be inherited from the
38+ // current process (for example, this means that standard input will
39+ // come from the keyboard and standard output/error will go directly to
40+ // the terminal if this process is invoked from the command line).
41+ cmd. stdout ( Stdio :: piped ( ) ) ;
42+
43+ let mut child = cmd. spawn ( ) . expect ( "failed to spawn command" ) ;
44+
45+ let stdout = child
46+ . stdout
47+ . take ( )
48+ . expect ( "child did not have a handle to stdout" ) ;
49+
50+ let mut reader = BufReader :: new ( stdout) . lines ( ) ;
51+
52+ // Ensure the child process is spawned in the runtime so it can
53+ // make progress on its own while we await for any output.
54+ tokio:: spawn ( async move {
55+ let status = child
56+ . wait ( )
57+ . await
58+ . expect ( "child process encountered an error" ) ;
59+
60+ println ! ( "child status was: {}" , status) ;
61+ } ) ;
62+
63+ while let Some ( line) = reader. next_line ( ) . await ? {
64+ println ! ( "Line: {}" , line) ;
65+ }
66+
67+ Ok ( ( ) )
68+ } ) ;
69+
70+
71+ }
72+
73+
74+ pub fn oneshot ( ) {
75+ let rt = tokio:: runtime:: Runtime :: new ( ) . unwrap ( ) ;
76+
77+ let _: Result < ( ) , Box < dyn std:: error:: Error > > = rt. block_on ( async {
78+ let ( tx, rx) = tokio:: sync:: oneshot:: channel :: < String > ( ) ;
79+
80+ tokio:: spawn ( async move {
81+ tx. send ( "hello" . to_string ( ) ) . unwrap ( ) ;
82+ } ) ;
83+
84+ let msg = rx. await ?;
85+ println ! ( "got = {}" , msg) ;
86+
87+ Ok ( ( ) )
88+ } ) ;
89+ }
90+
91+ pub fn async_with_oneshot ( ) {
92+ let rt = tokio:: runtime:: Runtime :: new ( ) . unwrap ( ) ;
93+
94+ async fn some_computation ( ) -> String {
95+ "the result of the computation" . to_string ( )
96+ }
97+
98+ let _: Result < ( ) , Box < dyn std:: error:: Error > > = rt. block_on ( async {
99+ let join_handle = tokio:: spawn ( async move {
100+ some_computation ( ) . await
101+ } ) ;
102+
103+ // Do other work while the computation is happening in the background
104+
105+ // Wait for the computation result
106+ let res = join_handle. await ?;
107+ println ! ( "result = {}" , res) ;
108+
109+ Ok ( ( ) )
110+ } ) ;
111+ }
112+
113+ pub fn mpsc_example ( ) {
114+ async fn some_computation ( input : u32 ) -> String {
115+ format ! ( "the result of computation {}" , input)
116+ }
117+
118+
119+ let rt = tokio:: runtime:: Runtime :: new ( ) . unwrap ( ) ;
120+
121+ let _: Result < ( ) , Box < dyn std:: error:: Error > > = rt. block_on ( async {
122+ let ( tx, mut rx) = tokio:: sync:: mpsc:: channel :: < String > ( 10 ) ;
123+
124+ tokio:: spawn ( async move {
125+ for i in 0 ..10 {
126+ let res = some_computation ( i) . await ;
127+ tx. send ( res) . await . unwrap ( ) ;
128+ }
129+ } ) ;
130+
131+ while let Some ( res) = rx. recv ( ) . await {
132+ println ! ( "got = {}" , res) ;
133+ }
134+
135+ Ok ( ( ) )
136+ } ) ;
137+ }
138+
139+ pub fn broadcast_example ( ) {
140+ let rt = tokio:: runtime:: Runtime :: new ( ) . unwrap ( ) ;
141+
142+ let _: Result < ( ) , Box < dyn std:: error:: Error > > = rt. block_on ( async {
143+ let ( tx, mut rx1) = tokio:: sync:: broadcast:: channel :: < String > ( 10 ) ;
144+ let mut rx2 = tx. subscribe ( ) ;
145+
146+ tokio:: spawn ( async move {
147+ tx. send ( "hello" . to_string ( ) ) . unwrap ( ) ;
148+ tx. send ( "world" . to_string ( ) ) . unwrap ( ) ;
149+ } ) ;
150+
151+ println ! ( "rx1 = {:?}" , rx1. recv( ) . await ) ;
152+ println ! ( "rx2 = {:?}" , rx2. recv( ) . await ) ;
153+ println ! ( "rx2 = {:?}" , rx2. recv( ) . await ) ;
154+
155+ Ok ( ( ) )
156+ } ) ;
157+
158+ }
159+
160+ pub fn watch_example ( ) {
161+ let rt = tokio:: runtime:: Runtime :: new ( ) . unwrap ( ) ;
162+
163+ let _: Result < ( ) , Box < dyn std:: error:: Error > > = rt. block_on ( async {
164+ let ( tx, rx1) = tokio:: sync:: watch:: channel :: < String > ( "hello" . to_string ( ) ) ;
165+ let mut rx2 = tx. subscribe ( ) ;
166+
167+ tokio:: spawn ( async move {
168+ tx. send ( "world" . to_string ( ) ) . unwrap ( ) ;
169+ } ) ;
170+
171+ println ! ( "rx1 = {:?}" , * rx1. borrow( ) ) ;
172+ println ! ( "rx2 = {:?}" , * rx2. borrow( ) ) ;
173+ println ! ( "rx2 = {:?}" , rx2. changed( ) . await ) ;
174+
175+ Ok ( ( ) )
176+ } ) ;
177+
178+ }
0 commit comments