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 <jplatte@users.noreply.github.com>

* chore: use `sqlx::Result` directly

Co-authored-by: Jonas Platte <jplatte@users.noreply.github.com>
This commit is contained in:
Erik 2022-07-19 23:31:10 +02:00 committed by GitHub
parent 8fc4625ec7
commit 78a0a5943a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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<Self> {
/// Ok(Self {
/// bar: MyCustomType {
/// custom: row.try_get("custom")?
/// }
/// })
/// }
/// }
/// ```
pub trait FromRow<'r, R: Row>: Sized {
fn from_row(row: &'r R) -> Result<Self, Error>;
}