dtolnay / async-trait

Type erasure for async trait methods
Apache License 2.0
1.81k stars 84 forks source link

how to set the return value #99

Closed somewheve closed 4 years ago

somewheve commented 4 years ago
#[async_trait]
impl Operation for Ck<'_> {
    async fn insert_into_ck(&self, data: Vec<String>) -> Result<(), Box<dyn Error>> {
        if self.ck_pool.is_none() {
            panic!("请先调用init_ck初始化数据库连接池或者检查地址参数是否出错")
        }
        if let Some(pool) = &self.ck_pool {
            let mut client = pool.get_handle().await;
        }
        Ok(())
    }
} 

will return a bad error

91 | #[async_trait]
   | ^^^^^^^^^^^^^^ expected `()`, found enum `std::result::Result`

I don't get any message to specify the return value in the readme, It makes me feel sad. but got the Any number of arguments, any return value;

dtolnay commented 4 years ago

I was not able to reproduce this so I think the error is somewhere else. Here is what I tried, which compiles correctly. In the future you should provide a minimal repro when opening an issue.

use async_trait::async_trait;
use std::error::Error;

#[async_trait]
trait Operation {
    async fn insert_into_ck(&self, data: Vec<String>) -> Result<(), Box<dyn Error>>;
}

struct Ck<'a> {
    ck_pool: Option<&'a Pool>,
}

struct Pool;

impl Pool {
    async fn get_handle(&self) {}
}

#[async_trait]
impl Operation for Ck<'_> {
    async fn insert_into_ck(&self, data: Vec<String>) -> Result<(), Box<dyn Error>> {
        if self.ck_pool.is_none() {
            panic!("请先调用init_ck初始化数据库连接池或者检查地址参数是否出错")
        }
        if let Some(pool) = &self.ck_pool {
            let mut client = pool.get_handle().await;
        }
        Ok(())
    }
} 
somewheve commented 4 years ago

Sorry I will give a reply tomorrow. I will check my code first, thanks for your code.

somewheve commented 4 years ago

caused by I write a function in a bad way

trait Operation {
      async fn insert_into_ck(&self, data: Vec<String>) 
}

My mistake。 thanks