Skip to content

Commit

Permalink
launchbadge#2934 solution using autoref trick instead of blanket impl…
Browse files Browse the repository at this point in the history
…ementation for Option<T>
  • Loading branch information
jonnyso committed Jan 8, 2025
1 parent 5ad8a4e commit 4d9f292
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
12 changes: 8 additions & 4 deletions sqlx-core/src/from_row.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::marker::PhantomData;

use crate::{
error::{Error, UnexpectedNullError},
row::Row,
Expand Down Expand Up @@ -491,13 +493,13 @@ impl_from_row_for_tuple!(
(15) -> T16;
);

pub struct Wrapper;
pub struct Wrapper<T>(pub PhantomData<T>);

pub trait FromOptRow<'r, R, T> {
fn __from_row(&self, row: &'r R) -> Result<T, Error>;
}

impl<'r, R, T> FromOptRow<'r, R, Option<T>> for &Wrapper
impl<'r, R, T> FromOptRow<'r, R, Option<T>> for Wrapper<Option<T>>
where
R: Row,
T: FromRow<'r, R>,
Expand All @@ -513,7 +515,7 @@ where
}
}

impl<'r, R, T> FromOptRow<'r, R, T> for Wrapper
impl<'r, R, T> FromOptRow<'r, R, T> for &Wrapper<T>
where
R: Row,
T: FromRow<'r, R>,
Expand All @@ -527,8 +529,10 @@ where
#[macro_export]
macro_rules! __from_opt_row {
($t:ty, $row:expr) => {{
use std::marker::PhantomData;
use $crate::from_row::{FromOptRow, Wrapper};
let value: Result<$t, sqlx::Error> = Wrapper.__from_row($row);
let wrapper = Wrapper(PhantomData::<$t>);
let value = (&wrapper).__from_row($row);
value
}};
}
2 changes: 0 additions & 2 deletions sqlx-macros-core/src/derives/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ fn expand_derive_from_row_struct(
}
// Flatten
(true, None, false) => {
predicates.push(parse_quote!(#ty: ::sqlx::FromRow<#lifetime, R>));
//parse_quote!(<#ty as ::sqlx::FromRow<#lifetime, R>>::from_row(__row))
parse_quote!(::sqlx::__from_opt_row!(#ty, __row))
}
// Flatten + Try from
Expand Down

0 comments on commit 4d9f292

Please sign in to comment.