forked from launchbadge/sqlx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support
NonZero*
scalar types (launchbadge#3244)
* feat: support `NonZero*` scalar types This commits adds `Type`, `Encode`, and `Decode` impls for all the `NonZero*` types from the standard library. They are implemented as direct proxies to their primitive counterparts, except that when decoding, the values are checked to not be zero. * fixup!: remove `non-zero` cargo feature * fixup!: make `non-zero` module private * fixup!: rebase and fix trait impls
- Loading branch information
Showing
2 changed files
with
80 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
//! [`Type`], [`Encode`], and [`Decode`] implementations for the various [`NonZero*`][non-zero] | ||
//! types from the standard library. | ||
//! | ||
//! [non-zero]: core::num::NonZero | ||
use std::num::{ | ||
NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, | ||
}; | ||
|
||
use crate::database::Database; | ||
use crate::decode::Decode; | ||
use crate::encode::{Encode, IsNull}; | ||
use crate::types::Type; | ||
|
||
macro_rules! impl_non_zero { | ||
($($int:ty => $non_zero:ty),* $(,)?) => { | ||
$(impl<DB> Type<DB> for $non_zero | ||
where | ||
DB: Database, | ||
$int: Type<DB>, | ||
{ | ||
fn type_info() -> <DB as Database>::TypeInfo { | ||
<$int as Type<DB>>::type_info() | ||
} | ||
|
||
fn compatible(ty: &<DB as Database>::TypeInfo) -> bool { | ||
<$int as Type<DB>>::compatible(ty) | ||
} | ||
} | ||
|
||
impl<'q, DB> Encode<'q, DB> for $non_zero | ||
where | ||
DB: Database, | ||
$int: Encode<'q, DB>, | ||
{ | ||
fn encode_by_ref(&self, buf: &mut <DB as Database>::ArgumentBuffer<'q>) -> Result<IsNull, crate::error::BoxDynError> { | ||
<$int as Encode<'q, DB>>::encode_by_ref(&self.get(), buf) | ||
} | ||
|
||
fn encode(self, buf: &mut <DB as Database>::ArgumentBuffer<'q>) -> Result<IsNull, crate::error::BoxDynError> | ||
where | ||
Self: Sized, | ||
{ | ||
<$int as Encode<'q, DB>>::encode(self.get(), buf) | ||
} | ||
|
||
fn produces(&self) -> Option<<DB as Database>::TypeInfo> { | ||
<$int as Encode<'q, DB>>::produces(&self.get()) | ||
} | ||
} | ||
|
||
impl<'r, DB> Decode<'r, DB> for $non_zero | ||
where | ||
DB: Database, | ||
$int: Decode<'r, DB>, | ||
{ | ||
fn decode(value: <DB as Database>::ValueRef<'r>) -> Result<Self, crate::error::BoxDynError> { | ||
let int = <$int as Decode<'r, DB>>::decode(value)?; | ||
let non_zero = Self::try_from(int)?; | ||
|
||
Ok(non_zero) | ||
} | ||
})* | ||
}; | ||
} | ||
|
||
impl_non_zero! { | ||
i8 => NonZeroI8, | ||
u8 => NonZeroU8, | ||
i16 => NonZeroI16, | ||
u16 => NonZeroU16, | ||
i32 => NonZeroI32, | ||
u32 => NonZeroU32, | ||
i64 => NonZeroI64, | ||
u64 => NonZeroU64, | ||
} |