remkop22 / postgres-from-row Goto Github PK
View Code? Open in Web Editor NEWDerive `FromRow` trait to automaticly convert postgres rows to structs.
Home Page: https://docs.rs/postgres-from-row
License: MIT License
Derive `FromRow` trait to automaticly convert postgres rows to structs.
Home Page: https://docs.rs/postgres-from-row
License: MIT License
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)
Support if I don't want to select or return all columns of a table.
Hi there, can you consider adding support to Bigint parsing support? :)
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")?,
})
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.