Skip to content

Commit

Permalink
refactor: Avoid using async fn so &self does not get captured
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Westerlind committed Aug 22, 2019
1 parent b4cba6d commit 390ff9d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 33 deletions.
11 changes: 8 additions & 3 deletions src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,19 @@ impl Cmd {
}

#[inline]
pub async fn query_async<C, T: FromRedisValue>(&self, con: &mut C) -> RedisResult<T>
pub fn query_async<'c, C, T: FromRedisValue>(
&self,
con: &'c mut C,
) -> impl Future<Output = RedisResult<T>> + 'c
where
C: crate::aio::ConnectionLike + Send + 'static,
T: Send + 'static,
{
let pcmd = self.get_packed_command();
let val = con.req_packed_command(pcmd).await?;
from_redis_value(&val)
async move {
let val = con.req_packed_command(pcmd).await?;
from_redis_value(&val)
}
}

/// Similar to `query()` but returns an iterator over the items of the
Expand Down
21 changes: 10 additions & 11 deletions src/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,20 +155,19 @@ impl<'a> ScriptInvocation<'a> {
/// Asynchronously invokes the script and returns the result.
#[inline]
pub fn invoke_async<'c, T: FromRedisValue + Send + 'static>(
&'c self,
&self,
con: &'c mut SharedConnection,
) -> impl Future<Output = RedisResult<T>> + 'c {
let mut eval_cmd = cmd("EVALSHA");
eval_cmd
.arg(self.script.hash.as_bytes())
.arg(self.keys.len())
.arg(&*self.keys)
.arg(&*self.args);

let mut load_cmd = cmd("SCRIPT");
load_cmd.arg("LOAD").arg(self.script.code.as_bytes());
async move {
let mut eval_cmd = cmd("EVALSHA");
eval_cmd
.arg(self.script.hash.as_bytes())
.arg(self.keys.len())
.arg(&*self.keys)
.arg(&*self.args);

let mut load_cmd = cmd("SCRIPT");
load_cmd.arg("LOAD").arg(self.script.code.as_bytes());

let future = {
let mut con = con.clone();
let eval_cmd = eval_cmd.clone();
Expand Down
43 changes: 24 additions & 19 deletions tests/test_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ fn dont_panic_on_closed_shared_connection() {

block_on_all(async move {
connect
.and_then(|mut con| {
.and_then(|con| {
async move {
let cmd = move || {
let mut con = con.clone();
async move {
redis::cmd("SET")
.arg("key1")
Expand Down Expand Up @@ -84,23 +85,26 @@ fn dont_panic_on_closed_shared_connection() {
fn test_pipeline_transaction() {
let ctx = TestContext::new();
block_on_all(ctx.async_connection().and_then(|mut con| {
let mut pipe = redis::pipe();
pipe.atomic()
.cmd("SET")
.arg("key_1")
.arg(42)
.ignore()
.cmd("SET")
.arg("key_2")
.arg(43)
.ignore()
.cmd("MGET")
.arg(&["key_1", "key_2"]);
pipe.query_async(&mut con)
.map_ok(|((k1, k2),): ((i32, i32),)| {
assert_eq!(k1, 42);
assert_eq!(k2, 43);
})
async move {
let mut pipe = redis::pipe();
pipe.atomic()
.cmd("SET")
.arg("key_1")
.arg(42)
.ignore()
.cmd("SET")
.arg("key_2")
.arg(43)
.ignore()
.cmd("MGET")
.arg(&["key_1", "key_2"]);
pipe.query_async(&mut con)
.map_ok(|((k1, k2),): ((i32, i32),)| {
assert_eq!(k1, 42);
assert_eq!(k2, 43);
})
.await
}
}))
.unwrap();
}
Expand Down Expand Up @@ -196,8 +200,9 @@ fn test_transaction_shared_connection() {
let ctx = TestContext::new();
block_on_all(async move {
ctx.shared_async_connection()
.and_then(|mut con| {
.and_then(|con| {
let cmds = (0..100).map(move |i| {
let mut con = con.clone();
async move {
let foo = i;
let bar = format!("bar{}", i);
Expand Down

0 comments on commit 390ff9d

Please sign in to comment.