mirror of
https://github.com/launchbadge/sqlx.git
synced 2026-04-28 21:24:35 +00:00
feat: support NonZero* scalar types (#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
This commit is contained in:
@@ -16,9 +16,11 @@
|
||||
//!
|
||||
//! To represent nullable SQL types, `Option<T>` is supported where `T` implements `Type`.
|
||||
//! An `Option<T>` represents a potentially `NULL` value from SQL.
|
||||
//!
|
||||
|
||||
use crate::{database::Database, type_info::TypeInfo};
|
||||
use crate::database::Database;
|
||||
use crate::type_info::TypeInfo;
|
||||
|
||||
mod non_zero;
|
||||
|
||||
#[cfg(feature = "bstr")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "bstr")))]
|
||||
@@ -60,7 +62,6 @@ pub mod time {
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "bigdecimal")))]
|
||||
#[doc(no_inline)]
|
||||
pub use bigdecimal::BigDecimal;
|
||||
|
||||
#[cfg(feature = "rust_decimal")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "rust_decimal")))]
|
||||
#[doc(no_inline)]
|
||||
@@ -82,7 +83,6 @@ pub mod mac_address {
|
||||
|
||||
#[cfg(feature = "json")]
|
||||
pub use json::{Json, JsonRawValue, JsonValue};
|
||||
|
||||
pub use text::Text;
|
||||
|
||||
/// Indicates that a SQL type is supported for a database.
|
||||
@@ -193,7 +193,6 @@ pub use text::Text;
|
||||
/// price: f64
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
pub trait Type<DB: Database> {
|
||||
/// Returns the canonical SQL type for this Rust type.
|
||||
///
|
||||
|
||||
76
sqlx-core/src/types/non_zero.rs
Normal file
76
sqlx-core/src/types/non_zero.rs
Normal file
@@ -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,
|
||||
}
|
||||
Reference in New Issue
Block a user