Skip to content

Derive ToSql and FromSql implementations for generic data types #570

@alexwl

Description

@alexwl

At the moment, it is impossible to derive FromSql or ToSql for a data type with generic parameters.

#[derive(FromSql)]
struct Data<'a> {
    id: i32,
    description: &'a str,
}

Error: missing lifetime specifier

#[derive(ToSql)]
struct Data<T: ToSql> {
    id: i32,
    description: String,
    links: Vec<T>,
}

Error: wrong number of type arguments

The generated code for FromSql and ToSql doesn't take into account the fact that a structure may have lifetime or type parameters:

impl<'a> postgres_types::FromSql<'a> for #ident {
fn from_sql(_type: &postgres_types::Type, buf: &'a [u8])
-> std::result::Result<#ident,

impl postgres_types::ToSql for #ident {

Adding support for lifetimes should be relatively straightforward, not sure about type parameters though.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions