How to reuse the complex output type of a function?

I'm trying to use the sqlx crate to define some connection to postgresql.

Here's the signature of the sqlx function I'm using (from the sqlx documentation: Connection in sqlx - Rust).

 fn connect(
        url: &str
    ) -> Pin<Box<dyn Future<Output = Result<Self, Error>> + Send>>
       where Self: Sized { ... }

I'm using that function to connect to my postgres database and it works.
But I'd like to define my own function, that will return a connection using my connection parameters and possibly some other options.

For example:

fn my_pg_conn(){
    PgConnection::connect(
        "postgresql://myuser:mypassword@127.0.0.1:5432"
    )
}

But this does not compile because I didn't specify a return type.
How can I give a type to my function?
I guess I could copy paste the type

Pin<Box<dyn Future<Output = Result<Self, Error>> + Send>>

but that's horrible.

How can I transparently use the same type as the original function, or use a shorter name for that type? I don't see if the sqlx team have given an alias to that type that we could use. How could I find out if there's a shorter name for that type?

Thanks for your help.

Just make your own.

type MyType = Pin<Box<dyn Future<Output = Result<Self, Error>> + Send>>;

BTW there's a more general, parametric (both in the type and the lifetime) alias provided by the futures crate: BoxFuture<'a, T>.

3 Likes

Well, actually this is not possible to do that, since Self is supposed to refer to a type, and Error is not defined. I guess I can replace Self with the type on which the function was defined. But then I also have to go look for what kind of Error type is used. This seems time consuming.
It's not the first time I stumble upon that problem. Do you really have to reverse engineer the code of every function you want to use the output type of?

Edit: just saw your addition about BoxFuture. I don't understand it but I will look into it.

??? It's literally sqlx::Error.

Anyway, again, you can just make it generic and substitute Self with the type parameter.

Something like this:

BoxFuture<Result<PgConnection, sqlx::Error>>

I knew Error had to be defined somewhere, but it's not clear where at first sight.
sqlx is just an example. I've stumbled upon this problem with other libraries where they define different types of errors and you have to browse through a lot of code and documentation to find out.
I want to check if there's something I'm missing, if there's an easier way to proceed.

1 Like

No, there isn't. It would be better if all crates provided a type alias for their long types, but if they don't then you have to figure out the type. When creating crates, we should keep this in mind.

2 Likes

Click on it. It's a link to its own documentation.

1 Like

Sorry, not being of any help here but I could not resist asking myself what people think, when seeing a declaration like:

fn clear_cached_statements(
    &mut self
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + '_>>
where
    Self::Database: HasStatementCache,

The first thing I think is: it should look like this.

fn clear_cached_statements(&mut self)

(if this post is inappropriate let me please know and remove it).

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.