pub trait FromRow: Sized {
// Required method
fn from_row(row: Row) -> Result<Self>;
// Provided methods
fn from_rows(result: QueryResult) -> Result<Vec<Self>> { ... }
fn from_single_row(result: QueryResult) -> Result<Self> { ... }
fn from_optional_row(result: QueryResult) -> Result<Option<Self>> { ... }
}Expand description
Trait for deserializing a row into a Rust type
This trait is implemented by schema types to enable conversion from database rows to strongly-typed structs.
§Example
ⓘ
struct User {
id: Uuid,
name: String,
age: i64,
}
impl FromRow for User {
fn from_row(row: Row) -> Result<Self> {
Ok(User {
id: row.get_required("id")?.as_uuid()?,
name: row.get_required("name")?.as_str()?.to_string(),
age: row.get_required("age")?.as_i64()?,
})
}
}Required Methods§
Provided Methods§
Sourcefn from_rows(result: QueryResult) -> Result<Vec<Self>>
fn from_rows(result: QueryResult) -> Result<Vec<Self>>
Convert multiple rows into a vector of this type
Sourcefn from_single_row(result: QueryResult) -> Result<Self>
fn from_single_row(result: QueryResult) -> Result<Self>
Convert a single row result, returning an error if zero or multiple rows
Sourcefn from_optional_row(result: QueryResult) -> Result<Option<Self>>
fn from_optional_row(result: QueryResult) -> Result<Option<Self>>
Convert an optional single row result
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.