mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-10-03 07:45:30 +00:00
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:
parent
8fc4625ec7
commit
78a0a5943a
@ -122,6 +122,33 @@ use crate::row::Row;
|
|||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// This field is compatible with the `default` attribute.
|
/// 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 {
|
pub trait FromRow<'r, R: Row>: Sized {
|
||||||
fn from_row(row: &'r R) -> Result<Self, Error>;
|
fn from_row(row: &'r R) -> Result<Self, Error>;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user