From 00b88e37fefece6e60fc627df3673708ab958dd5 Mon Sep 17 00:00:00 2001 From: xaos Date: Wed, 5 Apr 2023 04:36:05 +0200 Subject: [PATCH] Implement ToSql for std::num::NonZero types --- src/types/to_sql.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/src/types/to_sql.rs b/src/types/to_sql.rs index ffd1bcf..0bb865e 100644 --- a/src/types/to_sql.rs +++ b/src/types/to_sql.rs @@ -51,6 +51,12 @@ macro_rules! from_value( #[inline] fn from(t: $t) -> Self { ToSqlOutput::Owned(t.into())} } + ); + (non_zero $t:ty) => ( + impl From<$t> for ToSqlOutput<'_> { + #[inline] + fn from(t: $t) -> Self { ToSqlOutput::Owned(t.get().into())} + } ) ); from_value!(String); @@ -68,6 +74,15 @@ from_value!(f32); from_value!(f64); from_value!(Vec); +from_value!(non_zero std::num::NonZeroI8); +from_value!(non_zero std::num::NonZeroI16); +from_value!(non_zero std::num::NonZeroI32); +from_value!(non_zero std::num::NonZeroI64); +from_value!(non_zero std::num::NonZeroIsize); +from_value!(non_zero std::num::NonZeroU8); +from_value!(non_zero std::num::NonZeroU16); +from_value!(non_zero std::num::NonZeroU32); + // It would be nice if we could avoid the heap allocation (of the `Vec`) that // `i128` needs in `Into`, but it's probably fine for the moment, and not // worth adding another case to Value. @@ -75,6 +90,10 @@ from_value!(Vec); #[cfg_attr(docsrs, doc(cfg(feature = "i128_blob")))] from_value!(i128); +#[cfg(feature = "i128_blob")] +#[cfg_attr(docsrs, doc(cfg(feature = "i128_blob")))] +from_value!(non_zero std::num::NonZeroI128); + #[cfg(feature = "uuid")] #[cfg_attr(docsrs, doc(cfg(feature = "uuid")))] from_value!(uuid::Uuid); @@ -165,10 +184,23 @@ to_sql_self!(u32); to_sql_self!(f32); to_sql_self!(f64); +to_sql_self!(std::num::NonZeroI8); +to_sql_self!(std::num::NonZeroI16); +to_sql_self!(std::num::NonZeroI32); +to_sql_self!(std::num::NonZeroI64); +to_sql_self!(std::num::NonZeroIsize); +to_sql_self!(std::num::NonZeroU8); +to_sql_self!(std::num::NonZeroU16); +to_sql_self!(std::num::NonZeroU32); + #[cfg(feature = "i128_blob")] #[cfg_attr(docsrs, doc(cfg(feature = "i128_blob")))] to_sql_self!(i128); +#[cfg(feature = "i128_blob")] +#[cfg_attr(docsrs, doc(cfg(feature = "i128_blob")))] +to_sql_self!(std::num::NonZeroI128); + #[cfg(feature = "uuid")] #[cfg_attr(docsrs, doc(cfg(feature = "uuid")))] to_sql_self!(uuid::Uuid); @@ -186,12 +218,27 @@ macro_rules! to_sql_self_fallible( ))) } } + ); + (non_zero $t:ty) => ( + impl ToSql for $t { + #[inline] + fn to_sql(&self) -> Result> { + Ok(ToSqlOutput::Owned(Value::Integer( + i64::try_from(self.get()).map_err( + // TODO: Include the values in the error message. + |err| Error::ToSqlConversionFailure(err.into()) + )? + ))) + } + } ) ); // Special implementations for usize and u64 because these conversions can fail. to_sql_self_fallible!(u64); to_sql_self_fallible!(usize); +to_sql_self_fallible!(non_zero std::num::NonZeroU64); +to_sql_self_fallible!(non_zero std::num::NonZeroUsize); impl ToSql for &'_ T where @@ -275,6 +322,20 @@ mod test { is_to_sql::(); } + #[test] + fn test_nonzero_types() { + is_to_sql::(); + is_to_sql::(); + is_to_sql::(); + is_to_sql::(); + is_to_sql::(); + is_to_sql::(); + is_to_sql::(); + is_to_sql::(); + is_to_sql::(); + is_to_sql::(); + } + #[test] fn test_u8_array() { let a: [u8; 99] = [0u8; 99];