前提・実現したいこと
こちらでも同じ質問をしました。
私はredisにstruct型をvalueとしてinsertしたいです。
aio::MultiplexedConnection.
を使ってasyncでpipeを使用してinsertすることがゴールです。
発生している問題・エラーメッセージ
error
1error[E0698]: type inside `async fn` body must be known in this context 2 --> redis-lib/src/redis_helper.rs:28:14 3 | 428 | pipe.query_async(&mut con) 5 | ^^^^^^^^^^^ cannot infer type for type parameter `T` declared on the associated function `query_async` 6 | 7note: the type is part of the `async fn` body because of this `await` 8 --> redis-lib/src/redis_helper.rs:28:9 9 | 1028 | / pipe.query_async(&mut con) 1129 | | .await; 12 | |__________________^ 13 14For more information about this error, try `rustc --explain E0698`.
該当のソースコード
こちらは実際にstoreしたいstructです。
rust
1#[derive(Debug, Default, Clone)] 2pub struct MyData { 3 pub id: String, 4 pub value: BigDecimal, 5 pub retio: i64, 6}
以下はredisの部分のコードです。
こちらのpipe.query_async(&mut con) .await;
でエラーが出ます。
rust
1use anyhow::Result; 2use redis::{aio::MultiplexedConnection, Client}; 3use super::data::{Data, MyData}; 4 5#[derive()] 6pub struct RedisHelper { 7 conn: MultiplexedConnection, 8} 9 10impl RedisHelper { 11 pub async fn setup_redis(&mut self, url: String) -> Result<()> { 12 let cli = Client::open(url)?; 13 14 self.conn = cli.get_multiplexed_async_connection().await?; 15 16 Ok(()) 17 } 18 19 pub async fn insert_hash(&self, data: &MyData) -> Result<()> { 20 let mut con = self.conn.clone(); 21 let mut pipe = redis::pipe(); 22 23 pipe.cmd("HSET") 24 .arg(data.id.clone()) 25 .arg(data) 26 .ignore(); 27 28 pipe.query_async(&mut con) 29 .await; 30 31 Ok(()) 32 } 33}
試したこと、わからないこと
-
おそらくこちらの
T: FromRedisValue
の実装が必要だと思ったのですがあってますでしょうか? -
また、こちらはtrait境界?を定義すればいいのでしょうか?
-
try_intoのようなものでできるのかなと思って探してみたのですが、見つかりませんでした。
-
Struct
型をredisにstoreする様な記事などをみなかったのですが、一般的にこの様な方法はしないのでしょうか?何かわかることが有ればぜひ、ご教授ください。
rust
1pub async fn query_async<C, T: FromRedisValue>(&self, con: &mut C) -> RedisResult<T> 2 where 3 C: crate::aio::ConnectionLike, 4 { 5 let v = if self.commands.is_empty() { 6 return from_redis_value(&Value::Bulk(vec![])); 7 } else if self.transaction_mode { 8 self.execute_transaction_async(con).await? 9 } else { 10 self.execute_pipelined_async(con).await? 11 }; 12 from_redis_value(&v) 13 }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/01/24 10:51