redis_driver/commands/
prepared_command.rs

1use crate::{
2    resp::{Command, FromValue, Value},
3    ClientTrait, Future,
4};
5use std::marker::PhantomData;
6
7type PostProcessFunc<'a, R> = dyn Fn(Value, Command, &'a mut dyn ClientTrait) -> Future<'a, R> + Send + Sync;
8
9pub struct PreparedCommand<'a, T, R>
10where
11    R: FromValue,
12{
13    pub phantom: PhantomData<R>,
14    pub executor: &'a mut T,
15    pub command: Command,
16    pub keep_command_for_result: bool,
17    pub post_process: Option<Box<PostProcessFunc<'a, R>>>,
18}
19
20impl<'a, T, R> PreparedCommand<'a, T, R>
21where
22    R: FromValue,
23{
24    #[must_use]
25    pub fn new(executor: &'a mut T, command: Command) -> Self {
26        PreparedCommand {
27            phantom: PhantomData,
28            executor,
29            command,
30            keep_command_for_result: false,
31            post_process: None,
32        }
33    }
34
35    pub fn keep_command_for_result(mut self) -> Self {
36        self.keep_command_for_result = true;
37        self
38    }
39
40    pub fn post_process(mut self, post_process: Box<PostProcessFunc<'a, R>>) -> Self {
41        self.post_process = Some(post_process);
42        self
43    }
44
45    pub fn command(&self) -> &Command {
46        &self.command
47    }
48}
49
50pub(crate) fn prepare_command<T, R: FromValue>(
51    executor: &mut T,
52    command: Command,
53) -> PreparedCommand<T, R> {
54    PreparedCommand::new(executor, command)
55}