redis_ac/
lib.rs

1//!
2//! `redis-ac` is a helper crate for [redis][], which provides asynchronous version of [`redis::Commands`][].
3//!
4//! [`Commands`][] trait of this crate is automatically implemented for any types
5//! which implement [`redis::aio::ConnectionLike`][].
6//!
7//! Non-scan command methods of [`Commands`][] return a future.
8//!
9//! ```rust,no_run
10//! use futures::prelude::*;
11//! use redis_ac::Commands;
12//!
13//! # fn main() {
14//! let client = redis::Client::open("redis://127.0.0.1").unwrap();
15//! let connect = client.get_async_connection();
16//!
17//! let f = connect.and_then(|con|{
18//!     con.set("key", "value")
19//!         .and_then(|(con, res): (_, String)| {
20//!             assert_eq!(res, "OK");
21//!             con.get("key")
22//!         })
23//!         .and_then(|(_, res): (_, String)| {
24//!             assert_eq!(res, "value");
25//!             Ok(())
26//!         })
27//! }).map_err(|e| eprintln!("{}", e));
28//!
29//! tokio::run(f);
30//! # }
31//! ```
32//!
33//! Scan command methods of [`Commands`][] return a stream over scanned items.
34//!
35//! The stream returns tuples of an optional connection object and an optional item.
36//! When the last item is returned, the optional value becomes `Some`
37//! which holds the connection object passed to the method. Until then, it is `None`.
38//! The item is normally `Some`. It is set to `None` only if no items are found by scan.
39//! In such case, the stream just returns `(Some, None)` and finishes.
40//!
41//! ```rust,no_run
42//! use futures::prelude::*;
43//! use redis_ac::Commands;
44//!
45//! # fn main() {
46//! let client = redis::Client::open("redis://127.0.0.1").unwrap();
47//! let connect = client.get_async_connection();
48//!
49//! let f = connect.and_then(|con|{
50//!     con.scan_match("key*")
51//!         .for_each(|(con, item): (_, Option<String>)| {
52//!             println!("{:?}", item);
53//!             if con.is_some() {
54//!                 // The last item comes with the connection object.
55//!                 if item.is_none() {
56//!                     // If no items found by scan, `(Some(con), None)` is given.
57//!                     println!("No item");
58//!                 }
59//!             }
60//!             Ok(())
61//!         })
62//! }).map_err(|e| eprintln!("{}", e));
63//!
64//! tokio::run(f);
65//! # }
66//! ```
67//!
68//! If you are interested in only items, `filter_map` can be used to simplify.
69//!
70//! ```rust,no_run
71//! use futures::prelude::*;
72//! use redis_ac::Commands;
73//!
74//! # fn main() {
75//! let client = redis::Client::open("redis://127.0.0.1").unwrap();
76//! let connect = client.get_async_connection();
77//!
78//! let f = connect.and_then(|con|{
79//!     con.scan_match("key*")
80//!         .filter_map(|(_, item)| item)
81//!         .for_each(|item: String| {
82//!             // Here we get only items.
83//!             Ok(())
84//!         })
85//! }).map_err(|e| eprintln!("{}", e));
86//!
87//! tokio::run(f);
88//! # }
89//! ```
90
91#![cfg_attr(feature = "readme", feature(external_doc))]
92#![warn(missing_docs)]
93
94mod commands;
95mod stream;
96
97#[cfg_attr(feature = "readme", doc(include = "../README.md"))]
98type _Doctest = ();
99
100pub use crate::commands::{Commands, RedisScanAll, RedisScanStream};