tcp_simple/
tcp-simple.rs

1#[cfg(feature = "with-futures")]
2use futures::executor::block_on;
3use futures::io::AllowStdIo;
4use memcache_async::ascii::Protocol;
5use std::env;
6use std::net::TcpStream;
7
8fn main() {
9    let args: Vec<String> = env::args().collect();
10    if args.len() < 2 {
11        eprintln!("{} <addr>", args[0]);
12        return;
13    }
14
15    let addr = &args[1];
16
17    block_on(async move {
18        let (key, val) = ("foo", "bar");
19        let stream = TcpStream::connect(addr).expect("Failed to create stream");
20
21        // "futures::io::AllowStdIo" is used here to make the stream
22        // work with AsyncIO. This shouldn't be used in production
23        // since it will block current thread. Use something like
24        // romio or tokio instead.
25        let mut cache = Protocol::new(AllowStdIo::new(stream));
26        cache
27            .set(&key, val.as_bytes(), 0)
28            .await
29            .expect("Failed to set key");
30
31        let v = cache.get(&key).await.expect("Failed to get key");
32        assert_eq!(v, val.as_bytes());
33    });
34}