Skip to content

async_fn_in_trait (AFIT) usage #3016

Open
@tgross35

Description

@tgross35

I haven't seen it discussed anywhere, but could sqlx benefit from AFIT as-is?

This was recently released with 1.75 (2023-12-28). As I understand it, it would mean that some of the traits that look pretty messy:

pub trait Executor<'c>: Send + Debug + Sized {
    type Database: Database;

    fn fetch_optional<'e, 'q, E>(
        self,
        query: E
    ) -> Pin<Box<dyn Future<Output = Result<Option<<Self::Database as Database>::Row>, Error>> + Send + 'e>>
       where 'q: 'e,
             'c: 'e,
             E: 'q + Execute<'q, Self::Database>;
    // ...
}

Can now be written with a more understandable syntax, which also drops the Box<dyn ...> indirection:

pub trait Executor<'c>: Send + Debug + Sized {
    type Database: Database;

    async fn fetch_optional<'e, 'q, E>(
        self,
        query: E
    ) -> Result<Option<<Self::Database as Database>::Row>, Error>
       where 'q: 'e,
             'c: 'e,
             E: 'q + Execute<'q, Self::Database>;
    // ...
}

The blog post here has more information https://blog.rust-lang.org/2023/12/21/async-fn-rpit-in-traits.html. It seems like the limitations could make things less usable, but perhaps still beneficial?

Obviously putting this to use would be a MSRV break so it won't be usable anytime soon, but I am just curious if this has been looked at at all.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions