Specializing trait methods for given cases
Thank you for the nice crate!
I needed support for specializing some method implementations in the generated traits for each case, while having a single implementation for the rest of the trait. What I came up with is a `#[when]` attribute that can be used on methods within a `#[trait_gen]` trait, for example
```rust
#[trait_gen(DB -> sqlx::sqlite::Sqlite, sqlx::mysql::MySql, sqlx::postgres::Postgres)]
#[async_trait]
impl Repo for sqlx::Pool<DB> {
// ...
#[when(sqlx::sqlite::Sqlite -> update)]
async fn update_sqlite(&self, id: u64, param: String) -> Result<(), String> {
// SQLite specific implementation
todo!()
}
#[when(sqlx::mysql::MySql -> update)]
async fn update_mysql(&self, id: u64, param: String) -> Result<(), String> {
// MySQL specific implementation
todo!()
}
// ...
```
As I needed this quickly, I published it as a fork: https://github.com/vigoo/conditional_trait_gen
But if you are interested, I'm happy to open a PR with it upstream.
关闭于 2025-04-27 4 条评论