summaryrefslogtreecommitdiffstats
path: root/third_party/rust/tokio-0.1.22/examples/hello_world.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
commit43a97878ce14b72f0981164f87f2e35e14151312 (patch)
tree620249daf56c0258faa40cbdcf9cfba06de2a846 /third_party/rust/tokio-0.1.22/examples/hello_world.rs
parentInitial commit. (diff)
downloadfirefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz
firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/tokio-0.1.22/examples/hello_world.rs')
-rw-r--r--third_party/rust/tokio-0.1.22/examples/hello_world.rs58
1 files changed, 58 insertions, 0 deletions
diff --git a/third_party/rust/tokio-0.1.22/examples/hello_world.rs b/third_party/rust/tokio-0.1.22/examples/hello_world.rs
new file mode 100644
index 0000000000..c82762691a
--- /dev/null
+++ b/third_party/rust/tokio-0.1.22/examples/hello_world.rs
@@ -0,0 +1,58 @@
+//! Hello world server.
+//!
+//! A simple client that opens a TCP stream, writes "hello world\n", and closes
+//! the connection.
+//!
+//! You can test this out by running:
+//!
+//! ncat -l 6142
+//!
+//! And then in another terminal run:
+//!
+//! cargo run --example hello_world
+
+#![deny(warnings)]
+
+extern crate tokio;
+
+use tokio::io;
+use tokio::net::TcpStream;
+use tokio::prelude::*;
+
+pub fn main() -> Result<(), Box<std::error::Error>> {
+ let addr = "127.0.0.1:6142".parse()?;
+
+ // Open a TCP stream to the socket address.
+ //
+ // Note that this is the Tokio TcpStream, which is fully async.
+ let client = TcpStream::connect(&addr)
+ .and_then(|stream| {
+ println!("created stream");
+ io::write_all(stream, "hello world\n").then(|result| {
+ println!("wrote to stream; success={:?}", result.is_ok());
+ Ok(())
+ })
+ })
+ .map_err(|err| {
+ // All tasks must have an `Error` type of `()`. This forces error
+ // handling and helps avoid silencing failures.
+ //
+ // In our example, we are only going to log the error to STDOUT.
+ println!("connection error = {:?}", err);
+ });
+
+ // Start the Tokio runtime.
+ //
+ // The Tokio is a pre-configured "out of the box" runtime for building
+ // asynchronous applications. It includes both a reactor and a task
+ // scheduler. This means applications are multithreaded by default.
+ //
+ // This function blocks until the runtime reaches an idle state. Idle is
+ // defined as all spawned tasks have completed and all I/O resources (TCP
+ // sockets in our case) have been dropped.
+ println!("About to create the stream and write to it...");
+ tokio::run(client);
+ println!("Stream has been created and written to.");
+
+ Ok(())
+}