redis_driver/commands/
blocking_commands.rs

1use crate::{
2    prepare_command,
3    resp::{cmd, CommandArg, FromValue, SingleArgOrCollection},
4    Future, LMoveWhere, MonitorStream, PreparedCommand, ZMPopResult, ZWhere,
5};
6
7pub type BZpopMinMaxResult<K, E> = Option<Vec<(K, E, f64)>>;
8
9/// A group of blocking commands
10pub trait BlockingCommands {
11    /// This command is the blocking variant of [`lmove`](crate::ListCommands::lmove).
12    ///
13    /// # Return
14    /// the element being popped from `source` and pushed to `destination`.
15    /// If timeout is reached, a None reply is returned.
16    ///
17    /// # See Also
18    /// [<https://redis.io/commands/blmove/>](https://redis.io/commands/blmove/)
19    #[must_use]
20    fn blmove<S, D, E>(
21        &mut self,
22        source: S,
23        destination: D,
24        where_from: LMoveWhere,
25        where_to: LMoveWhere,
26        timeout: f64,
27    ) -> PreparedCommand<Self, E>
28    where
29        Self: Sized,
30        S: Into<CommandArg>,
31        D: Into<CommandArg>,
32        E: FromValue,
33    {
34        prepare_command(
35            self,
36            cmd("BLMOVE")
37                .arg(source)
38                .arg(destination)
39                .arg(where_from)
40                .arg(where_to)
41                .arg(timeout),
42        )
43    }
44
45    /// This command is the blocking variant of [`lmpop`](crate::ListCommands::lmpop).
46    ///
47    /// # Return
48    /// - None when no element could be popped, and timeout is reached.
49    /// - Tuple composed by the name of the key from which elements were popped and the list of popped element
50    ///
51    /// # See Also
52    /// [<https://redis.io/commands/blmpop/>](https://redis.io/commands/blmpop/)
53    #[must_use]
54    fn blmpop<K, E, C>(
55        &mut self,
56        timeout: f64,
57        keys: C,
58        where_: LMoveWhere,
59        count: usize,
60    ) -> PreparedCommand<Self, Option<(String, Vec<E>)>>
61    where
62        Self: Sized,
63        K: Into<CommandArg>,
64        E: FromValue,
65        C: SingleArgOrCollection<K>,
66    {
67        prepare_command(
68            self,
69            cmd("BLMPOP")
70                .arg(timeout)
71                .arg(keys.num_args())
72                .arg(keys)
73                .arg(where_)
74                .arg("COUNT")
75                .arg(count),
76        )
77    }
78
79    /// This command is a blocking list pop primitive.
80    ///
81    /// It is the blocking version of [`lpop`](crate::ListCommands::lpop) because it
82    /// blocks the connection when there are no elements to pop from any of the given lists.
83    ///
84    /// An element is popped from the head of the first list that is non-empty,
85    /// with the given keys being checked in the order that they are given.
86    ///
87    /// # Return
88    /// - `None` when no element could be popped and the timeout expired
89    /// - a tuple with the first element being the name of the key where an element was popped
90    /// and the second element being the value of the popped element.
91    ///
92    /// # See Also
93    /// [<https://redis.io/commands/blpop/>](https://redis.io/commands/blpop/)
94    #[must_use]
95    fn blpop<K, KK, K1, V>(
96        &mut self,
97        keys: KK,
98        timeout: f64,
99    ) -> PreparedCommand<Self, Option<(K1, V)>>
100    where
101        Self: Sized,
102        K: Into<CommandArg>,
103        KK: SingleArgOrCollection<K>,
104        K1: FromValue,
105        V: FromValue,
106    {
107        prepare_command(self, cmd("BLPOP").arg(keys).arg(timeout))
108    }
109
110    /// This command is a blocking list pop primitive.
111    ///
112    /// It is the blocking version of [`rpop`](crate::ListCommands::rpop) because it
113    /// blocks the connection when there are no elements to pop from any of the given lists.
114    ///
115    /// An element is popped from the tail of the first list that is non-empty,
116    /// with the given keys being checked in the order that they are given.
117    ///
118    /// # Return
119    /// - `None` when no element could be popped and the timeout expired
120    /// - a tuple with the first element being the name of the key where an element was popped
121    /// and the second element being the value of the popped element.
122    ///
123    /// # See Also
124    /// [<https://redis.io/commands/brpop/>](https://redis.io/commands/brpop/)
125    #[must_use]
126    fn brpop<K, KK, K1, V>(
127        &mut self,
128        keys: KK,
129        timeout: f64,
130    ) -> PreparedCommand<Self, Option<(K1, V)>>
131    where
132        Self: Sized,
133        K: Into<CommandArg>,
134        KK: SingleArgOrCollection<K>,
135        K1: FromValue,
136        V: FromValue,
137    {
138        prepare_command(self, cmd("BRPOP").arg(keys).arg(timeout))
139    }
140
141    /// This command is the blocking variant of [`zmpop`](crate::SortedSetCommands::zmpop).
142    ///
143    /// # Return
144    /// * `None` if no element could be popped
145    /// * A tuple made up of
146    ///     * The name of the key from which elements were popped
147    ///     * An array of tuples with all the popped members and their scores
148    ///
149    /// # See Also
150    /// [<https://redis.io/commands/bzmpop/>](https://redis.io/commands/bzmpop/)
151    #[must_use]
152    fn bzmpop<K, C, E>(
153        &mut self,
154        timeout: f64,
155        keys: C,
156        where_: ZWhere,
157        count: usize,
158    ) -> PreparedCommand<Self, Option<ZMPopResult<E>>>
159    where
160        Self: Sized,
161        K: Into<CommandArg>,
162        C: SingleArgOrCollection<K>,
163        E: FromValue,
164    {
165        prepare_command(
166            self,
167            cmd("BZMPOP")
168                .arg(timeout)
169                .arg(keys.num_args())
170                .arg(keys)
171                .arg(where_)
172                .arg("COUNT")
173                .arg(count),
174        )
175    }
176
177    /// This command is the blocking variant of [`zpopmax`](crate::SortedSetCommands::zpopmax).
178    ///
179    /// # Return
180    /// * `None` when no element could be popped and the timeout expired.
181    /// * The list of tuple with
182    ///     * the first element being the name of the key where a member was popped,
183    ///     * the second element is the popped member itself,
184    ///     * and the third element is the score of the popped element.
185    ///
186    /// # See Also
187    /// [<https://redis.io/commands/bzpopmax/>](https://redis.io/commands/bzpopmax/)
188    #[must_use]
189    fn bzpopmax<K, KK, E, K1>(
190        &mut self,
191        keys: KK,
192        timeout: f64,
193    ) -> PreparedCommand<Self, BZpopMinMaxResult<K1, E>>
194    where
195        Self: Sized,
196        K: Into<CommandArg>,
197        KK: SingleArgOrCollection<K>,
198        K1: FromValue,
199        E: FromValue,
200    {
201        prepare_command(self, cmd("BZPOPMAX").arg(keys).arg(timeout))
202    }
203
204    /// This command is the blocking variant of [`zpopmin`](crate::SortedSetCommands::zpopmin).
205    ///
206    /// # Return
207    /// * `None` when no element could be popped and the timeout expired.
208    /// * The list of tuple with
209    ///     * the first element being the name of the key where a member was popped,
210    ///     * the second element is the popped member itself,
211    ///     * and the third element is the score of the popped element.
212    ///
213    /// # See Also
214    /// [<https://redis.io/commands/bzpopmin/>](https://redis.io/commands/bzpopmin/)
215    #[must_use]
216    fn bzpopmin<K, KK, E, K1>(
217        &mut self,
218        keys: KK,
219        timeout: f64,
220    ) -> PreparedCommand<Self, BZpopMinMaxResult<K1, E>>
221    where
222        Self: Sized,
223        K: Into<CommandArg>,
224        KK: SingleArgOrCollection<K>,
225        K1: FromValue,
226        E: FromValue,
227    {
228        prepare_command(self, cmd("BZPOPMIN").arg(keys).arg(timeout))
229    }
230
231    /// Debugging command that streams back every command processed by the Redis server.
232    ///
233    /// # See Also
234    /// [<https://redis.io/commands/monitor/>](https://redis.io/commands/monitor/)
235    #[must_use]
236    fn monitor(&mut self) -> Future<MonitorStream>;
237}