mirror of
https://github.com/launchbadge/sqlx
synced 2024-11-10 06:24:16 +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
1 changed files with 27 additions and 0 deletions
|
@ -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>;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue