From 78a0a5943af323864feeeb5401ca715f100aecfb Mon Sep 17 00:00:00 2001 From: Erik Date: Tue, 19 Jul 2022 23:31:10 +0200 Subject: [PATCH] Add example for manual implemenation of the FromRow trait (#1495) * chore: add doc example for manual implemenation of FromRow trait * fix typo Co-authored-by: Jonas Platte * chore: use `sqlx::Result` directly Co-authored-by: Jonas Platte --- sqlx-core/src/from_row.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/sqlx-core/src/from_row.rs b/sqlx-core/src/from_row.rs index a71b40af..050fa106 100644 --- a/sqlx-core/src/from_row.rs +++ b/sqlx-core/src/from_row.rs @@ -122,6 +122,33 @@ use crate::row::Row; /// ``` /// /// This field is compatible with the `default` attribute. +/// +/// ## Manual implementation +/// +/// You can also implement the [`FromRow`] trait by hand. This can be useful if you +/// have a struct with a field that needs manuel decoding: +/// +/// +/// ```rust,ignore +/// use sqlx::{FromRow, sqlite::SqliteRow, sqlx::Row}; +/// struct MyCustomType { +/// custom: String, +/// } +/// +/// struct Foo { +/// bar: MyCustomType, +/// } +/// +/// impl FromRow<'_, SqliteRow> for Foo { +/// fn from_row(row: &SqliteRow) -> sqlx::Result { +/// Ok(Self { +/// bar: MyCustomType { +/// custom: row.try_get("custom")? +/// } +/// }) +/// } +/// } +/// ``` pub trait FromRow<'r, R: Row>: Sized { fn from_row(row: &'r R) -> Result; }