标签: tokio-postgres

如何将 Future 作为函数参数传递?

我习惯了 Scala 的类型,在这种类型Future中,您可以包装要返回的任何对象来指定它。Future[..]

我的 Rust 函数hello返回Query,但我似乎无法将该结果作为 type 的参数传递Future<Output = Query>。为什么不呢?我应该如何更好地输入它?

当我尝试将未来作为参数传递时,就会发生失败:

use std::future::Future;

struct Person;
struct DatabaseError;

type Query = Result<Vec<Person>, DatabaseError>;

async fn hello_future(future: &dyn Future<Output = Query>) -> bool {
    future.await.is_ok()
}

async fn hello() -> Query {
    unimplemented!()
}

async fn example() {
    let f = hello();
    hello_future(&f);
}

fn main() {}
Run Code Online (Sandbox Code Playgroud)

编译失败并出现以下错误:

error[E0277]: `&dyn Future<Output = Result<Vec<Person>, DatabaseError>>` is not a future
 --> src/main.rs:9:5
  |
9 | …
Run Code Online (Sandbox Code Playgroud)

future rust rust-tokio tokio-postgres

6
推荐指数
1
解决办法
3202
查看次数

在 Rust 中使用 Tokio-postgres 将多个值插入到 Postgres

我正在使用下面的代码使用 tokio-postgres 插入到 Postgres 数据库,是否有更好的选择:

let members = &[obj] //obj is a struct
let mut params = Vec::<&(dyn ToSql + Sync)>::new();
let mut i = 1;
let mut qry:String = "insert into tablename(id,userid,usertype) values".to_string();
for column in members{
    if(i ==1){
        qry = format!("{} (${},${},${})",qry,i,i+1,i+2);
    }else{
        qry = format!("{}, (${},${},${})",qry,i,i+1,i+2);

    }
    params.push(&column.id);
    params.push(&column.userid);
    params.push(&column.usertype);
    i = i+3;
               
}
println!("qry : {}",qry);
let result = p.execute(&qry, &params[..]).await; //p is the pool manager
Run Code Online (Sandbox Code Playgroud)

postgresql multiple-insert rust tokio-postgres

5
推荐指数
1
解决办法
2863
查看次数

如何为枚举类型(rust、postgres)实现 tokio_postgres::types::ToSql

我需要为枚举类型实现 tokio_postgres::types::ToSql (rust 和 db 作为枚举实现),但我不知道如何...

例子

enum Flag {MyFlag1, MyFlag2, MyFlag3};
// on postgres db :
//    CREATE TYPE flag AS ENUM ('my_flag_1', 'my_flag_2', 'my_flag_3');

impl ToSql for Flag {
  fn to_sql(&self, ty: &Type, out: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
    // ???
  }
  fn accepts(ty: &Type) -> bool {
    // ???
  }
}
Run Code Online (Sandbox Code Playgroud)

有人能帮我吗?

rust rust-tokio tokio-postgres

5
推荐指数
1
解决办法
407
查看次数