Skip to content

Custom impl TryFrom returning something other than tokio_postgres::Error #16

Description

@sjoqvist

I want to read aggregated information from my database in a way that is hard to represent as a table but easy to return as JSONB and, subsequently, easy to parse from JSON to a Rust struct using Serde. The naïve approach is to duplicate the entire struct with the exception of one serde_json::Value field, and then implement conversion of the entire type.

To avoid code duplication, I wanted to use #[from_row(try_from = "JsonVersionOfMyType")] and a custom impl TryFrom<JsonVersionOfMyType> for MyType. However, this requires type Error = serde_json::Error; but then I'm getting

error[E0277]: the trait bound `tokio_postgres::Error: std::convert::From<serde_json::Error>` is not satisfied
  --> crates/x/src/x/x.rs:26:35
   |
26 | #[derive(Clone, Debug, Serialize, FromRow)]
   |                                   ^^^^^^^ the trait `std::convert::From<serde_json::Error>` is not implemented for `tokio_postgres::Error`
   |
   = note: this error originates in the derive macro `FromRow` (in Nightly builds, run with -Z macro-backtrace for more info)

The problem is that there seems to be no external way to construct tokio_postgres::Error, which means that it can only be generated during communication with the database.

Am I doing something wrong? In that case, maybe you could provide sample code for how to use try_from? I've found no useful information on the Internet.

Or can this feature only be used by code that potentially returns tokio_postgres::Error when converting between two "internal" Rust types? Because that seems like a non-existent use case.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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