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§
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.