FromRow

Trait FromRow 

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

Trait for converting a database row into a Rust struct

Implement this trait for your structs to enable automatic mapping from query results using query_as.

§Example

use stoolap::{Database, FromRow, ResultRow, Result};

struct User {
    id: i64,
    name: String,
    email: Option<String>,
}

impl FromRow for User {
    fn from_row(row: &ResultRow) -> Result<Self> {
        Ok(User {
            id: row.get(0)?,
            name: row.get(1)?,
            email: row.get(2)?,  // Option<T> handles NULL
        })
    }
}

// Now you can use query_as
let db = Database::open("memory://")?;
let users: Vec<User> = db.query_as("SELECT id, name, email FROM users", ())?;

§Using column names

You can also use column names for more robust mapping:

impl FromRow for User {
    fn from_row(row: &ResultRow) -> Result<Self> {
        Ok(User {
            id: row.get_by_name("id")?,
            name: row.get_by_name("name")?,
            email: row.get_by_name("email")?,
        })
    }
}

Required Methods§

Source

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

Convert a result row into Self

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.

Implementors§