redis_driver/commands/set_commands.rs
1use crate::{
2 prepare_command,
3 resp::{
4 cmd, CommandArg, CommandArgs, FromSingleValueArray, FromValue, IntoArgs,
5 SingleArgOrCollection,
6 },
7 PreparedCommand,
8};
9use std::hash::Hash;
10
11/// A group of Redis commands related to [`Sets`](https://redis.io/docs/data-types/sets/)
12/// # See Also
13/// [Redis Set Commands](https://redis.io/commands/?group=set)
14pub trait SetCommands {
15 /// Add the specified members to the set stored at key.
16 ///
17 /// # See Also
18 /// [<https://redis.io/commands/sadd/>](https://redis.io/commands/sadd/)
19 #[must_use]
20 fn sadd<K, M, C>(&mut self, key: K, members: C) -> PreparedCommand<Self, usize>
21 where
22 Self: Sized,
23 K: Into<CommandArg>,
24 M: Into<CommandArg>,
25 C: SingleArgOrCollection<M>,
26 {
27 prepare_command(self, cmd("SADD").arg(key).arg(members))
28 }
29
30 /// Returns the set cardinality (number of elements) of the set stored at key.
31 ///
32 /// # Return
33 /// The cardinality (number of elements) of the set, or 0 if key does not exist.
34 ///
35 /// # See Also
36 /// [<https://redis.io/commands/scard/>](https://redis.io/commands/scard/)
37 #[must_use]
38 fn scard<K>(&mut self, key: K) -> PreparedCommand<Self, usize>
39 where
40 Self: Sized,
41 K: Into<CommandArg>,
42 {
43 prepare_command(self, cmd("SCARD").arg(key))
44 }
45
46 /// Returns the members of the set resulting from the difference
47 /// between the first set and all the successive sets.
48 ///
49 /// # Return
50 /// A list with members of the resulting set.
51 ///
52 /// # See Also
53 /// [<https://redis.io/commands/sdiff/>](https://redis.io/commands/sdiff/)
54 #[must_use]
55 fn sdiff<K, M, C, A>(&mut self, keys: C) -> PreparedCommand<Self, A>
56 where
57 Self: Sized,
58 K: Into<CommandArg>,
59 M: FromValue + Eq + Hash,
60 C: SingleArgOrCollection<K>,
61 A: FromSingleValueArray<M>,
62 {
63 prepare_command(self, cmd("SDIFF").arg(keys))
64 }
65
66 /// This command is equal to [sdiff](crate::SetCommands::sdiff), but instead of returning the resulting set,
67 /// it is stored in destination.
68 ///
69 /// # Return
70 /// The number of elements in the resulting set.
71 ///
72 /// # See Also
73 /// [<https://redis.io/commands/sdiffstore/>](https://redis.io/commands/sdiffstore/)
74 #[must_use]
75 fn sdiffstore<D, K, C>(&mut self, destination: D, keys: C) -> PreparedCommand<Self, usize>
76 where
77 Self: Sized,
78 D: Into<CommandArg>,
79 K: Into<CommandArg>,
80 C: SingleArgOrCollection<K>,
81 {
82 prepare_command(self, cmd("SDIFFSTORE").arg(destination).arg(keys))
83 }
84
85 /// Returns the members of the set resulting from the intersection of all the given sets.
86 ///
87 /// # Return
88 /// A list with members of the resulting set.
89 ///
90 /// # See Also
91 /// [<https://redis.io/commands/sinter/>](https://redis.io/commands/sinter/)
92 #[must_use]
93 fn sinter<K, M, C, A>(&mut self, keys: C) -> PreparedCommand<Self, A>
94 where
95 Self: Sized,
96 K: Into<CommandArg>,
97 M: FromValue + Eq + Hash,
98 C: SingleArgOrCollection<K>,
99 A: FromSingleValueArray<M>,
100 {
101 prepare_command(self, cmd("SINTER").arg(keys))
102 }
103
104 /// This command is similar to [sinter](crate::SetCommands::sinter), but instead of returning the result set,
105 /// it returns just the cardinality of the result.
106 ///
107 /// limit: if the intersection cardinality reaches limit partway through the computation,
108 /// the algorithm will exit and yield limit as the cardinality. 0 means unlimited
109 ///
110 /// # Return
111 /// A list with members of the resulting set.
112 ///
113 /// # See Also
114 /// [<https://redis.io/commands/sintercard/>](https://redis.io/commands/sintercard/)
115 #[must_use]
116 fn sintercard<K, C>(&mut self, keys: C, limit: usize) -> PreparedCommand<Self, usize>
117 where
118 Self: Sized,
119 K: Into<CommandArg>,
120 C: SingleArgOrCollection<K>,
121 {
122 prepare_command(
123 self,
124 cmd("SINTERCARD")
125 .arg(keys.num_args())
126 .arg(keys)
127 .arg("LIMIT")
128 .arg(limit),
129 )
130 }
131
132 /// This command is equal to [sinter](crate::SetCommands::sinter), but instead of returning the resulting set,
133 /// it is stored in destination.
134 ///
135 /// # Return
136 /// The number of elements in the resulting set.
137 ///
138 /// # See Also
139 /// [<https://redis.io/commands/sinterstore/>](https://redis.io/commands/sinterstore/)
140 #[must_use]
141 fn sinterstore<D, K, C>(&mut self, destination: D, keys: C) -> PreparedCommand<Self, usize>
142 where
143 Self: Sized,
144 D: Into<CommandArg>,
145 K: Into<CommandArg>,
146 C: SingleArgOrCollection<K>,
147 {
148 prepare_command(self, cmd("SINTERSTORE").arg(destination).arg(keys))
149 }
150
151 /// Returns if member is a member of the set stored at key.
152 ///
153 /// # Return
154 /// * `true` - if the element is a member of the set.
155 /// * `false` - if the element is not a member of the set, or if key does not exist.
156 ///
157 /// # See Also
158 /// [<https://redis.io/commands/sismember/>](https://redis.io/commands/sismember/)
159 #[must_use]
160 fn sismember<K, M>(&mut self, key: K, member: M) -> PreparedCommand<Self, bool>
161 where
162 Self: Sized,
163 K: Into<CommandArg>,
164 M: Into<CommandArg>,
165 {
166 prepare_command(self, cmd("SISMEMBER").arg(key).arg(member))
167 }
168
169 /// Returns all the members of the set value stored at key.
170 ///
171 /// # See Also
172 /// [<https://redis.io/commands/smembers/>](https://redis.io/commands/smembers/)
173 #[must_use]
174 fn smembers<K, M, A>(&mut self, key: K) -> PreparedCommand<Self, A>
175 where
176 Self: Sized,
177 K: Into<CommandArg>,
178 M: FromValue + Eq + Hash,
179 A: FromSingleValueArray<M>,
180 {
181 prepare_command(self, cmd("SMEMBERS").arg(key))
182 }
183
184 /// Returns whether each member is a member of the set stored at key.
185 ///
186 /// # Return
187 /// list representing the membership of the given elements, in the same order as they are requested.
188 ///
189 /// # See Also
190 /// [<https://redis.io/commands/smismember/>](https://redis.io/commands/smismember/)
191 #[must_use]
192 fn smismember<K, M, C>(&mut self, key: K, members: C) -> PreparedCommand<Self, Vec<bool>>
193 where
194 Self: Sized,
195 K: Into<CommandArg>,
196 M: Into<CommandArg>,
197 C: SingleArgOrCollection<M>,
198 {
199 prepare_command(self, cmd("SMISMEMBER").arg(key).arg(members))
200 }
201
202 /// Move member from the set at source to the set at destination.
203 ///
204 /// # Return
205 /// * `true` - if the element is moved.
206 /// * `false` - if the element is not a member of source and no operation was performed.
207 ///
208 /// # See Also
209 /// [<https://redis.io/commands/smove/>](https://redis.io/commands/smove/)
210 #[must_use]
211 fn smove<S, D, M>(
212 &mut self,
213 source: S,
214 destination: D,
215 member: M,
216 ) -> PreparedCommand<Self, bool>
217 where
218 Self: Sized,
219 S: Into<CommandArg>,
220 D: Into<CommandArg>,
221 M: Into<CommandArg>,
222 {
223 prepare_command(self, cmd("SMOVE").arg(source).arg(destination).arg(member))
224 }
225
226 /// Removes and returns one or more random members from the set value store at key.
227 ///
228 /// # Return
229 /// the list of popped elements
230 ///
231 /// # See Also
232 /// [<https://redis.io/commands/spop/>](https://redis.io/commands/spop/)
233 #[must_use]
234 fn spop<K, M, A>(&mut self, key: K, count: usize) -> PreparedCommand<Self, A>
235 where
236 Self: Sized,
237 K: Into<CommandArg>,
238 M: FromValue + Eq + Hash,
239 A: FromSingleValueArray<M>,
240 {
241 prepare_command(self, cmd("SPOP").arg(key).arg(count))
242 }
243
244 /// Removes and returns one or more random members from the set value store at key.
245 ///
246 /// # Return
247 /// the list of popped elements
248 ///
249 /// # See Also
250 /// [<https://redis.io/commands/srandmember/>](https://redis.io/commands/srandmember/)
251 #[must_use]
252 fn srandmember<K, M, A>(&mut self, key: K, count: usize) -> PreparedCommand<Self, A>
253 where
254 Self: Sized,
255 K: Into<CommandArg>,
256 M: FromValue + Eq + Hash,
257 A: FromSingleValueArray<M>,
258 {
259 prepare_command(self, cmd("SRANDMEMBER").arg(key).arg(count))
260 }
261
262 /// Remove the specified members from the set stored at key.
263 ///
264 /// # Return
265 /// the number of members that were removed from the set, not including non existing members.
266 ///
267 /// # See Also
268 /// [<https://redis.io/commands/srem/>](https://redis.io/commands/srem/)
269 #[must_use]
270 fn srem<K, M, C>(&mut self, key: K, members: C) -> PreparedCommand<Self, usize>
271 where
272 Self: Sized,
273 K: Into<CommandArg>,
274 M: Into<CommandArg>,
275 C: SingleArgOrCollection<M>,
276 {
277 prepare_command(self, cmd("SREM").arg(key).arg(members))
278 }
279
280 /// Iterates elements of Sets types.
281 ///
282 /// # Return
283 /// a list of Set members.
284 ///
285 /// # See Also
286 /// [<https://redis.io/commands/sscan/>](https://redis.io/commands/sscan/)
287 #[must_use]
288 fn sscan<K, M>(
289 &mut self,
290 key: K,
291 cursor: u64,
292 options: SScanOptions,
293 ) -> PreparedCommand<Self, (u64, Vec<M>)>
294 where
295 Self: Sized,
296 K: Into<CommandArg>,
297 M: FromValue,
298 {
299 prepare_command(self, cmd("SSCAN").arg(key).arg(cursor).arg(options))
300 }
301
302 /// Returns the members of the set resulting from the union of all the given sets.
303 ///
304 /// # Return
305 /// A list with members of the resulting set.
306 ///
307 /// # See Also
308 /// [<https://redis.io/commands/sunion/>](https://redis.io/commands/sunion/)
309 #[must_use]
310 fn sunion<K, M, C, A>(&mut self, keys: C) -> PreparedCommand<Self, A>
311 where
312 Self: Sized,
313 K: Into<CommandArg>,
314 M: FromValue + Eq + Hash,
315 C: SingleArgOrCollection<K>,
316 A: FromSingleValueArray<M>,
317 {
318 prepare_command(self, cmd("SUNION").arg(keys))
319 }
320
321 /// This command is equal to [sunion](crate::SetCommands::sunion), but instead of returning the resulting set,
322 /// it is stored in destination.
323 ///
324 /// # Return
325 /// The number of elements in the resulting set.
326 ///
327 /// # See Also
328 /// [<https://redis.io/commands/sunionstore/>](https://redis.io/commands/sunionstore/)
329 #[must_use]
330 fn sunionstore<D, K, C>(&mut self, destination: D, keys: C) -> PreparedCommand<Self, usize>
331 where
332 Self: Sized,
333 D: Into<CommandArg>,
334 K: Into<CommandArg>,
335 C: SingleArgOrCollection<K>,
336 {
337 prepare_command(self, cmd("SUNIONSTORE").arg(destination).arg(keys))
338 }
339}
340
341/// Options for the [`sscan`](crate::SetCommands::sscan) command
342#[derive(Default)]
343pub struct SScanOptions {
344 command_args: CommandArgs,
345}
346
347impl SScanOptions {
348 #[must_use]
349 pub fn match_pattern<P: Into<CommandArg>>(self, match_pattern: P) -> Self {
350 Self {
351 command_args: self.command_args.arg("MATCH").arg(match_pattern),
352 }
353 }
354
355 #[must_use]
356 pub fn count(self, count: usize) -> Self {
357 Self {
358 command_args: self.command_args.arg("COUNT").arg(count),
359 }
360 }
361}
362
363impl IntoArgs for SScanOptions {
364 fn into_args(self, args: CommandArgs) -> CommandArgs {
365 args.arg(self.command_args)
366 }
367}