Code Monkey home page Code Monkey logo

postgres-from-row's People

Contributors

remkop22 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

postgres-from-row's Issues

Trait bounds not met

Unrelated to #10, while I can now get the derive on a struct with a lifetime parameter to compile, I can't get the usage of it in a function to compile.

Minimal example:

// The derive compiles fine now
#[derive(FromRow)]
struct User<'b> {
    id: i32,
    username: &'b str
}

// However, there's a lifetime issue when trying to use the derived function:
fn get_user_by_id<'c>(db: &mut impl postgres::GenericClient, id: i32) -> Result<User<'c>, postgres::Error> {
    let row = db.query_one("SELECT * FROM User WHERE id = $1", &[&id])?;
    User::try_from_row(&row)
}

Error:

error[E0599]: the function or associated item `try_from_row` exists for struct `User<'_>`, but its trait bounds were not satisfied
  --> tests/integration.rs:14:11
   |
6  | struct User<'b> {
   | ---------------
   | |
   | function or associated item `try_from_row` not found for this struct
   | doesn't satisfy `User<'_>: FromRow`
...
14 |     User::try_from_row(&row)
   |           ^^^^^^^^^^^^ function or associated item cannot be called on `User<'_>` due to unsatisfied trait bounds
   |
   = note: trait bound `&str: FromSql<'__from_row_lifetime>` was not satisfied

(Note that this error at the usage site appears both with and without #11, just with the lifetime name in the error changed)

Fails to compile when 'a lifetime parameter used

Reproduction:

use postgres_from_row::FromRow;

#[derive(FromRow)]
struct User<'a> {
    username: &'a str,
    email: &'a str,
}

Error:

error[E0496]: lifetime name `'a` shadows a lifetime name that is already in scope
 --> example-project\src\main.rs:4:10
  |
4 | #[derive(FromRow)]
  |          ^^^^^^^ lifetime `'a` already in scope
5 | struct User<'a> {
  |             -- first declared here
  |
  = note: this error originates in the derive macro `FromRow` (in Nightly builds, run with -Z macro-backtrace for more info)

Macro expansion when using 'a (does not compile):

#![feature(prelude_import)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
use postgres_from_row::FromRow;
struct User<'a> {
    username: &'a str,
    email: &'a str,
}
impl<'a> postgres_from_row::FromRow for User<'a>
where
    &'a str: for<'a> postgres_from_row::tokio_postgres::types::FromSql<'a>,
    &'a str: for<'a> postgres_from_row::tokio_postgres::types::FromSql<'a>,
{
    fn from_row(row: &postgres_from_row::tokio_postgres::Row) -> Self {
        Self {
            username: postgres_from_row::tokio_postgres::Row::get::<
                &str,
                &'a str,
            >(row, "username"),
            email: postgres_from_row::tokio_postgres::Row::get::<
                &str,
                &'a str,
            >(row, "email"),
        }
    }
    fn try_from_row(
        row: &postgres_from_row::tokio_postgres::Row,
    ) -> std::result::Result<Self, postgres_from_row::tokio_postgres::Error> {        
        Ok(Self {
            username: postgres_from_row::tokio_postgres::Row::try_get::<
                &str,
                &'a str,
            >(row, "username")?,
            email: postgres_from_row::tokio_postgres::Row::try_get::<
                &str,
                &'a str,
            >(row, "email")?,
        })
    }
}

Surprisingly, it compiles when you use 'b instead of 'a in the struct:

use postgres_from_row::FromRow;

#[derive(FromRow)]
struct User<'b> {
    username: &'b str,
    email: &'b str,
}

Macro expansion when using 'b (compiles):

#![feature(prelude_import)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
use postgres_from_row::FromRow;
struct User<'b> {
    username: &'b str,
    email: &'b str,
}
impl<'b> postgres_from_row::FromRow for User<'b>
where
    &'b str: for<'a> postgres_from_row::tokio_postgres::types::FromSql<'a>,
    &'b str: for<'a> postgres_from_row::tokio_postgres::types::FromSql<'a>,
{
    fn from_row(row: &postgres_from_row::tokio_postgres::Row) -> Self {
        Self {
            username: postgres_from_row::tokio_postgres::Row::get::<
                &str,
                &'b str,
            >(row, "username"),
            email: postgres_from_row::tokio_postgres::Row::get::<
                &str,
                &'b str,
            >(row, "email"),
        }
    }
    fn try_from_row(
        row: &postgres_from_row::tokio_postgres::Row,
    ) -> std::result::Result<Self, postgres_from_row::tokio_postgres::Error> {        
        Ok(Self {
            username: postgres_from_row::tokio_postgres::Row::try_get::<
                &str,
                &'b str,
            >(row, "username")?,
            email: postgres_from_row::tokio_postgres::Row::try_get::<
                &str,
                &'b str,
            >(row, "email")?,
        })
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.