Skip to content

Commit

Permalink
enhancement: (launchbadge#2934) implement FromRow for Option<T> where…
Browse files Browse the repository at this point in the history
… T: FromRow
  • Loading branch information
jonnyso committed Jan 5, 2025
1 parent 9d74aea commit 2003e26
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion sqlx-core/src/from_row.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{error::Error, row::Row};
use crate::{
error::{Error, UnexpectedNullError},
row::Row,
};

/// A record that can be built from a row returned by the database.
///
Expand Down Expand Up @@ -285,6 +288,22 @@ where
}
}

impl<'r, R, T> FromRow<'r, R> for Option<T>
where
R: Row,
T: FromRow<'r, R>,
{
fn from_row(row: &'r R) -> Result<Self, Error> {
let value = T::from_row(row).map(Some);
if let Err(Error::ColumnDecode { source, .. }) = value.as_ref() {
if let Some(UnexpectedNullError) = source.downcast_ref() {
return Ok(None);
}
}
value
}
}

// implement FromRow for tuples of types that implement Decode
// up to tuples of 9 values

Expand Down

0 comments on commit 2003e26

Please sign in to comment.