Skip to main content

FromRow

Trait FromRow 

Source
pub trait FromRow: Sized {
    // Required method
    fn from_row(row: &Row) -> Result<Self>;
}
Expand description

Trait for types that can be constructed from a database row.

Implement this trait for your structs to enable direct mapping from query results using Connection::fetch_one_as, Connection::fetch_all_as, or by calling FromRow::from_row on each Row from a Rowset.

§Example

use hyperdb_api::{Row, FromRow, Result};

struct User {
    id: i32,
    name: String,
    active: bool,
}

impl FromRow for User {
    fn from_row(row: &Row) -> Result<Self> {
        Ok(User {
            id: row.get::<i32>(0).ok_or_else(|| hyperdb_api::Error::new("NULL id"))?,
            name: row.get::<String>(1).unwrap_or_default(),
            active: row.get::<bool>(2).unwrap_or(false),
        })
    }
}

Required Methods§

Source

fn from_row(row: &Row) -> Result<Self>

Constructs an instance from a database row.

§Errors

Returns an Error — typically crate::Error::Other — when a required column is missing, SQL NULL, or cannot be decoded as the expected type. Implementations decide the exact failure shape.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<A: RowValue> FromRow for (Option<A>,)

Source§

fn from_row(row: &Row) -> Result<Self>

Source§

impl<A: RowValue, B: RowValue> FromRow for (Option<A>, Option<B>)

Source§

fn from_row(row: &Row) -> Result<Self>

Source§

impl<A: RowValue, B: RowValue, C: RowValue> FromRow for (Option<A>, Option<B>, Option<C>)

Source§

fn from_row(row: &Row) -> Result<Self>

Source§

impl<A: RowValue, B: RowValue, C: RowValue, D: RowValue> FromRow for (Option<A>, Option<B>, Option<C>, Option<D>)

Source§

fn from_row(row: &Row) -> Result<Self>

Implementors§