FromRow

Trait FromRow 

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

Source

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

Convert a row into this type

Provided Methods§

Source

fn from_rows(result: QueryResult) -> Result<Vec<Self>>

Convert multiple rows into a vector of this type

Source

fn from_single_row(result: QueryResult) -> Result<Self>

Convert a single row result, returning an error if zero or multiple rows

Source

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.

Implementations on Foreign Types§

Source§

impl FromRow for bool

Source§

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

Source§

impl FromRow for f64

Source§

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

Source§

impl FromRow for i64

Source§

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

Source§

impl FromRow for String

Source§

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

Source§

impl FromRow for DateTime<Utc>

Source§

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

Source§

impl FromRow for Uuid

Source§

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

Source§

impl<T: FromRow> FromRow for Option<T>

Source§

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

Implementors§