Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow lifetime parameters on generic types #8973

Closed
sfackler opened this issue Sep 4, 2013 · 2 comments
Closed

Allow lifetime parameters on generic types #8973

sfackler opened this issue Sep 4, 2013 · 2 comments
Labels
A-lifetimes Area: Lifetimes / regions

Comments

@sfackler
Copy link
Member

sfackler commented Sep 4, 2013

TL;DR

It'd be nice if something like this were possible:

trait Foo {}

trait Bar<F: Foo> {
    fn bar<'a>(&'a self) -> F<'a>;
}

This'd probably require some new syntax to limit F to Foos with lifetime parameters. Potentially related to #5922.

Use Case

In https://github.com/sfackler/rust-postgres, the PostgresConnection and PostgresTransaction types can both prepare statements. However, PostgresConnection.prepare and PostgresTransaction.prepare return different implementations of the PostgresStatement trait. Both implementations of PostgresStatement contain borrowed pointers to their parent PostgresConnection and the lifetime of a statement is semantically tied to a connection in any case. I'd like to have a trait to unify this functionality. It'd look something like

pub trait PostgresPreparer<S: PostgresStatement> {
    fn try_prepare<'a>(&'a self, query: &str) -> Result<S<'a>, PostgresDbError>;

    fn prepare<'a>(&'a self, query: &str) -> S<'a> {
        match try_prepare(query) {
            Ok(stmt) => stmt,
            Err(err) => fail2!("Error preparing statement: {}", err.to_str())
        }
    }

    fn try_update(&self, query: &str, params: &[&ToSql]) -> Result<uint, PostgresDbError> {
        do self.try_prepare(query).chain |stmt| {
            stmt.try_update(params)
        }
    }

    fn update(&self, query: &str, params: &[&ToSql]) -> uint {
        match self.try_update(query, params) {
            Ok(updated) => updated,
            Err(err) => fail2!("Error executing query: {}", err.to_str())
        }
    }

}
@sfackler
Copy link
Member Author

Talking with @nikomatsakis in IRC, this requires higher kinded types.

@nikomatsakis
Copy link
Contributor

Dup #8922

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: Lifetimes / regions
Projects
None yet
Development

No branches or pull requests

2 participants