1use byteorder::{NetworkEndian, ReadBytesExt, WriteBytesExt};
2use std::collections::Bound;
3use std::error::Error;
4use std::io::Write;
5
6use crate::deserialize::{self, Defaultable, FromSql, Queryable};
7use crate::expression::bound::Bound as SqlBound;
8use crate::expression::AsExpression;
9use crate::pg::{Pg, PgTypeMetadata, PgValue};
10use crate::query_builder::bind_collector::ByteWrapper;
11use crate::serialize::{self, IsNull, Output, ToSql};
12use crate::sql_types::*;
13
14struct RangeFlags(<RangeFlags as
::bitflags::__private::PublicFlags>::Internal);
impl RangeFlags {
#[allow(deprecated, non_upper_case_globals,)]
pub const EMPTY: Self = Self::from_bits_retain(0x01);
#[allow(deprecated, non_upper_case_globals,)]
pub const LB_INC: Self = Self::from_bits_retain(0x02);
#[allow(deprecated, non_upper_case_globals,)]
pub const UB_INC: Self = Self::from_bits_retain(0x04);
#[allow(deprecated, non_upper_case_globals,)]
pub const LB_INF: Self = Self::from_bits_retain(0x08);
#[allow(deprecated, non_upper_case_globals,)]
pub const UB_INF: Self = Self::from_bits_retain(0x10);
#[allow(deprecated, non_upper_case_globals,)]
pub const LB_NULL: Self = Self::from_bits_retain(0x20);
#[allow(deprecated, non_upper_case_globals,)]
pub const UB_NULL: Self = Self::from_bits_retain(0x40);
#[allow(deprecated, non_upper_case_globals,)]
pub const CONTAIN_EMPTY: Self = Self::from_bits_retain(0x80);
}
impl ::bitflags::Flags for RangeFlags {
const FLAGS: &'static [::bitflags::Flag<RangeFlags>] =
&[{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("EMPTY", RangeFlags::EMPTY)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("LB_INC", RangeFlags::LB_INC)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("UB_INC", RangeFlags::UB_INC)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("LB_INF", RangeFlags::LB_INF)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("UB_INF", RangeFlags::UB_INF)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("LB_NULL", RangeFlags::LB_NULL)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("UB_NULL", RangeFlags::UB_NULL)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("CONTAIN_EMPTY",
RangeFlags::CONTAIN_EMPTY)
}];
type Bits = u8;
fn bits(&self) -> u8 { RangeFlags::bits(self) }
fn from_bits_retain(bits: u8) -> RangeFlags {
RangeFlags::from_bits_retain(bits)
}
}
#[allow(dead_code, deprecated, unused_doc_comments, unused_attributes,
unused_mut, unused_imports, non_upper_case_globals, clippy ::
assign_op_pattern, clippy :: indexing_slicing, clippy :: same_name_method,
clippy :: iter_without_into_iter,)]
const _: () =
{
#[repr(transparent)]
struct InternalBitFlags(u8);
#[automatically_derived]
impl ::core::clone::Clone for InternalBitFlags {
#[inline]
fn clone(&self) -> InternalBitFlags {
let _: ::core::clone::AssertParamIsClone<u8>;
*self
}
}
#[automatically_derived]
impl ::core::marker::Copy for InternalBitFlags { }
#[automatically_derived]
impl ::core::marker::StructuralPartialEq for InternalBitFlags { }
#[automatically_derived]
impl ::core::cmp::PartialEq for InternalBitFlags {
#[inline]
fn eq(&self, other: &InternalBitFlags) -> bool {
self.0 == other.0
}
}
#[automatically_derived]
impl ::core::cmp::Eq for InternalBitFlags {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_receiver_is_total_eq(&self) -> () {
let _: ::core::cmp::AssertParamIsEq<u8>;
}
}
#[automatically_derived]
impl ::core::cmp::PartialOrd for InternalBitFlags {
#[inline]
fn partial_cmp(&self, other: &InternalBitFlags)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::cmp::PartialOrd::partial_cmp(&self.0, &other.0)
}
}
#[automatically_derived]
impl ::core::cmp::Ord for InternalBitFlags {
#[inline]
fn cmp(&self, other: &InternalBitFlags) -> ::core::cmp::Ordering {
::core::cmp::Ord::cmp(&self.0, &other.0)
}
}
#[automatically_derived]
impl ::core::hash::Hash for InternalBitFlags {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
::core::hash::Hash::hash(&self.0, state)
}
}
impl ::bitflags::__private::PublicFlags for RangeFlags {
type Primitive = u8;
type Internal = InternalBitFlags;
}
impl ::bitflags::__private::core::default::Default for
InternalBitFlags {
#[inline]
fn default() -> Self { InternalBitFlags::empty() }
}
impl ::bitflags::__private::core::fmt::Debug for InternalBitFlags {
fn fmt(&self,
f: &mut ::bitflags::__private::core::fmt::Formatter<'_>)
-> ::bitflags::__private::core::fmt::Result {
if self.is_empty() {
f.write_fmt(format_args!("{0:#x}",
<u8 as ::bitflags::Bits>::EMPTY))
} else {
::bitflags::__private::core::fmt::Display::fmt(self, f)
}
}
}
impl ::bitflags::__private::core::fmt::Display for InternalBitFlags {
fn fmt(&self,
f: &mut ::bitflags::__private::core::fmt::Formatter<'_>)
-> ::bitflags::__private::core::fmt::Result {
::bitflags::parser::to_writer(&RangeFlags(*self), f)
}
}
impl ::bitflags::__private::core::str::FromStr for InternalBitFlags {
type Err = ::bitflags::parser::ParseError;
fn from_str(s: &str)
->
::bitflags::__private::core::result::Result<Self,
Self::Err> {
::bitflags::parser::from_str::<RangeFlags>(s).map(|flags|
flags.0)
}
}
impl ::bitflags::__private::core::convert::AsRef<u8> for
InternalBitFlags {
fn as_ref(&self) -> &u8 { &self.0 }
}
impl ::bitflags::__private::core::convert::From<u8> for
InternalBitFlags {
fn from(bits: u8) -> Self { Self::from_bits_retain(bits) }
}
#[allow(dead_code, deprecated, unused_attributes)]
impl InternalBitFlags {
#[inline]
pub const fn empty() -> Self {
Self(<u8 as ::bitflags::Bits>::EMPTY)
}
#[inline]
pub const fn all() -> Self {
let mut truncated = <u8 as ::bitflags::Bits>::EMPTY;
let mut i = 0;
{
{
let flag =
<RangeFlags as ::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<RangeFlags as ::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<RangeFlags as ::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<RangeFlags as ::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<RangeFlags as ::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<RangeFlags as ::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<RangeFlags as ::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<RangeFlags as ::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
let _ = i;
Self(truncated)
}
#[inline]
pub const fn bits(&self) -> u8 { self.0 }
#[inline]
pub const fn from_bits(bits: u8)
-> ::bitflags::__private::core::option::Option<Self> {
let truncated = Self::from_bits_truncate(bits).0;
if truncated == bits {
::bitflags::__private::core::option::Option::Some(Self(bits))
} else { ::bitflags::__private::core::option::Option::None }
}
#[inline]
pub const fn from_bits_truncate(bits: u8) -> Self {
Self(bits & Self::all().0)
}
#[inline]
pub const fn from_bits_retain(bits: u8) -> Self { Self(bits) }
#[inline]
pub fn from_name(name: &str)
-> ::bitflags::__private::core::option::Option<Self> {
{
if name == "EMPTY" {
return ::bitflags::__private::core::option::Option::Some(Self(RangeFlags::EMPTY.bits()));
}
};
;
{
if name == "LB_INC" {
return ::bitflags::__private::core::option::Option::Some(Self(RangeFlags::LB_INC.bits()));
}
};
;
{
if name == "UB_INC" {
return ::bitflags::__private::core::option::Option::Some(Self(RangeFlags::UB_INC.bits()));
}
};
;
{
if name == "LB_INF" {
return ::bitflags::__private::core::option::Option::Some(Self(RangeFlags::LB_INF.bits()));
}
};
;
{
if name == "UB_INF" {
return ::bitflags::__private::core::option::Option::Some(Self(RangeFlags::UB_INF.bits()));
}
};
;
{
if name == "LB_NULL" {
return ::bitflags::__private::core::option::Option::Some(Self(RangeFlags::LB_NULL.bits()));
}
};
;
{
if name == "UB_NULL" {
return ::bitflags::__private::core::option::Option::Some(Self(RangeFlags::UB_NULL.bits()));
}
};
;
{
if name == "CONTAIN_EMPTY" {
return ::bitflags::__private::core::option::Option::Some(Self(RangeFlags::CONTAIN_EMPTY.bits()));
}
};
;
let _ = name;
::bitflags::__private::core::option::Option::None
}
#[inline]
pub const fn is_empty(&self) -> bool {
self.0 == <u8 as ::bitflags::Bits>::EMPTY
}
#[inline]
pub const fn is_all(&self) -> bool {
Self::all().0 | self.0 == self.0
}
#[inline]
pub const fn intersects(&self, other: Self) -> bool {
self.0 & other.0 != <u8 as ::bitflags::Bits>::EMPTY
}
#[inline]
pub const fn contains(&self, other: Self) -> bool {
self.0 & other.0 == other.0
}
#[inline]
pub fn insert(&mut self, other: Self) {
*self = Self(self.0).union(other);
}
#[inline]
pub fn remove(&mut self, other: Self) {
*self = Self(self.0).difference(other);
}
#[inline]
pub fn toggle(&mut self, other: Self) {
*self = Self(self.0).symmetric_difference(other);
}
#[inline]
pub fn set(&mut self, other: Self, value: bool) {
if value { self.insert(other); } else { self.remove(other); }
}
#[inline]
#[must_use]
pub const fn intersection(self, other: Self) -> Self {
Self(self.0 & other.0)
}
#[inline]
#[must_use]
pub const fn union(self, other: Self) -> Self {
Self(self.0 | other.0)
}
#[inline]
#[must_use]
pub const fn difference(self, other: Self) -> Self {
Self(self.0 & !other.0)
}
#[inline]
#[must_use]
pub const fn symmetric_difference(self, other: Self) -> Self {
Self(self.0 ^ other.0)
}
#[inline]
#[must_use]
pub const fn complement(self) -> Self {
Self::from_bits_truncate(!self.0)
}
}
impl ::bitflags::__private::core::fmt::Binary for InternalBitFlags {
fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
-> ::bitflags::__private::core::fmt::Result {
let inner = self.0;
::bitflags::__private::core::fmt::Binary::fmt(&inner, f)
}
}
impl ::bitflags::__private::core::fmt::Octal for InternalBitFlags {
fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
-> ::bitflags::__private::core::fmt::Result {
let inner = self.0;
::bitflags::__private::core::fmt::Octal::fmt(&inner, f)
}
}
impl ::bitflags::__private::core::fmt::LowerHex for InternalBitFlags {
fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
-> ::bitflags::__private::core::fmt::Result {
let inner = self.0;
::bitflags::__private::core::fmt::LowerHex::fmt(&inner, f)
}
}
impl ::bitflags::__private::core::fmt::UpperHex for InternalBitFlags {
fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
-> ::bitflags::__private::core::fmt::Result {
let inner = self.0;
::bitflags::__private::core::fmt::UpperHex::fmt(&inner, f)
}
}
impl ::bitflags::__private::core::ops::BitOr for InternalBitFlags {
type Output = Self;
#[inline]
fn bitor(self, other: InternalBitFlags) -> Self {
self.union(other)
}
}
impl ::bitflags::__private::core::ops::BitOrAssign for
InternalBitFlags {
#[inline]
fn bitor_assign(&mut self, other: Self) { self.insert(other); }
}
impl ::bitflags::__private::core::ops::BitXor for InternalBitFlags {
type Output = Self;
#[inline]
fn bitxor(self, other: Self) -> Self {
self.symmetric_difference(other)
}
}
impl ::bitflags::__private::core::ops::BitXorAssign for
InternalBitFlags {
#[inline]
fn bitxor_assign(&mut self, other: Self) { self.toggle(other); }
}
impl ::bitflags::__private::core::ops::BitAnd for InternalBitFlags {
type Output = Self;
#[inline]
fn bitand(self, other: Self) -> Self { self.intersection(other) }
}
impl ::bitflags::__private::core::ops::BitAndAssign for
InternalBitFlags {
#[inline]
fn bitand_assign(&mut self, other: Self) {
*self =
Self::from_bits_retain(self.bits()).intersection(other);
}
}
impl ::bitflags::__private::core::ops::Sub for InternalBitFlags {
type Output = Self;
#[inline]
fn sub(self, other: Self) -> Self { self.difference(other) }
}
impl ::bitflags::__private::core::ops::SubAssign for InternalBitFlags
{
#[inline]
fn sub_assign(&mut self, other: Self) { self.remove(other); }
}
impl ::bitflags::__private::core::ops::Not for InternalBitFlags {
type Output = Self;
#[inline]
fn not(self) -> Self { self.complement() }
}
impl ::bitflags::__private::core::iter::Extend<InternalBitFlags> for
InternalBitFlags {
fn extend<T: ::bitflags::__private::core::iter::IntoIterator<Item
= Self>>(&mut self, iterator: T) {
for item in iterator { self.insert(item) }
}
}
impl ::bitflags::__private::core::iter::FromIterator<InternalBitFlags>
for InternalBitFlags {
fn from_iter<T: ::bitflags::__private::core::iter::IntoIterator<Item
= Self>>(iterator: T) -> Self {
use ::bitflags::__private::core::iter::Extend;
let mut result = Self::empty();
result.extend(iterator);
result
}
}
impl InternalBitFlags {
#[inline]
pub const fn iter(&self) -> ::bitflags::iter::Iter<RangeFlags> {
::bitflags::iter::Iter::__private_const_new(<RangeFlags as
::bitflags::Flags>::FLAGS,
RangeFlags::from_bits_retain(self.bits()),
RangeFlags::from_bits_retain(self.bits()))
}
#[inline]
pub const fn iter_names(&self)
-> ::bitflags::iter::IterNames<RangeFlags> {
::bitflags::iter::IterNames::__private_const_new(<RangeFlags
as ::bitflags::Flags>::FLAGS,
RangeFlags::from_bits_retain(self.bits()),
RangeFlags::from_bits_retain(self.bits()))
}
}
impl ::bitflags::__private::core::iter::IntoIterator for
InternalBitFlags {
type Item = RangeFlags;
type IntoIter = ::bitflags::iter::Iter<RangeFlags>;
fn into_iter(self) -> Self::IntoIter { self.iter() }
}
impl InternalBitFlags {
#[inline]
pub fn bits_mut(&mut self) -> &mut u8 { &mut self.0 }
}
#[allow(dead_code, deprecated, unused_attributes)]
impl RangeFlags {
#[inline]
pub const fn empty() -> Self { Self(InternalBitFlags::empty()) }
#[inline]
pub const fn all() -> Self { Self(InternalBitFlags::all()) }
#[inline]
pub const fn bits(&self) -> u8 { self.0.bits() }
#[inline]
pub const fn from_bits(bits: u8)
-> ::bitflags::__private::core::option::Option<Self> {
match InternalBitFlags::from_bits(bits) {
::bitflags::__private::core::option::Option::Some(bits) =>
::bitflags::__private::core::option::Option::Some(Self(bits)),
::bitflags::__private::core::option::Option::None =>
::bitflags::__private::core::option::Option::None,
}
}
#[inline]
pub const fn from_bits_truncate(bits: u8) -> Self {
Self(InternalBitFlags::from_bits_truncate(bits))
}
#[inline]
pub const fn from_bits_retain(bits: u8) -> Self {
Self(InternalBitFlags::from_bits_retain(bits))
}
#[inline]
pub fn from_name(name: &str)
-> ::bitflags::__private::core::option::Option<Self> {
match InternalBitFlags::from_name(name) {
::bitflags::__private::core::option::Option::Some(bits) =>
::bitflags::__private::core::option::Option::Some(Self(bits)),
::bitflags::__private::core::option::Option::None =>
::bitflags::__private::core::option::Option::None,
}
}
#[inline]
pub const fn is_empty(&self) -> bool { self.0.is_empty() }
#[inline]
pub const fn is_all(&self) -> bool { self.0.is_all() }
#[inline]
pub const fn intersects(&self, other: Self) -> bool {
self.0.intersects(other.0)
}
#[inline]
pub const fn contains(&self, other: Self) -> bool {
self.0.contains(other.0)
}
#[inline]
pub fn insert(&mut self, other: Self) { self.0.insert(other.0) }
#[inline]
pub fn remove(&mut self, other: Self) { self.0.remove(other.0) }
#[inline]
pub fn toggle(&mut self, other: Self) { self.0.toggle(other.0) }
#[inline]
pub fn set(&mut self, other: Self, value: bool) {
self.0.set(other.0, value)
}
#[inline]
#[must_use]
pub const fn intersection(self, other: Self) -> Self {
Self(self.0.intersection(other.0))
}
#[inline]
#[must_use]
pub const fn union(self, other: Self) -> Self {
Self(self.0.union(other.0))
}
#[inline]
#[must_use]
pub const fn difference(self, other: Self) -> Self {
Self(self.0.difference(other.0))
}
#[inline]
#[must_use]
pub const fn symmetric_difference(self, other: Self) -> Self {
Self(self.0.symmetric_difference(other.0))
}
#[inline]
#[must_use]
pub const fn complement(self) -> Self {
Self(self.0.complement())
}
}
impl ::bitflags::__private::core::fmt::Binary for RangeFlags {
fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
-> ::bitflags::__private::core::fmt::Result {
let inner = self.0;
::bitflags::__private::core::fmt::Binary::fmt(&inner, f)
}
}
impl ::bitflags::__private::core::fmt::Octal for RangeFlags {
fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
-> ::bitflags::__private::core::fmt::Result {
let inner = self.0;
::bitflags::__private::core::fmt::Octal::fmt(&inner, f)
}
}
impl ::bitflags::__private::core::fmt::LowerHex for RangeFlags {
fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
-> ::bitflags::__private::core::fmt::Result {
let inner = self.0;
::bitflags::__private::core::fmt::LowerHex::fmt(&inner, f)
}
}
impl ::bitflags::__private::core::fmt::UpperHex for RangeFlags {
fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
-> ::bitflags::__private::core::fmt::Result {
let inner = self.0;
::bitflags::__private::core::fmt::UpperHex::fmt(&inner, f)
}
}
impl ::bitflags::__private::core::ops::BitOr for RangeFlags {
type Output = Self;
#[inline]
fn bitor(self, other: RangeFlags) -> Self { self.union(other) }
}
impl ::bitflags::__private::core::ops::BitOrAssign for RangeFlags {
#[inline]
fn bitor_assign(&mut self, other: Self) { self.insert(other); }
}
impl ::bitflags::__private::core::ops::BitXor for RangeFlags {
type Output = Self;
#[inline]
fn bitxor(self, other: Self) -> Self {
self.symmetric_difference(other)
}
}
impl ::bitflags::__private::core::ops::BitXorAssign for RangeFlags {
#[inline]
fn bitxor_assign(&mut self, other: Self) { self.toggle(other); }
}
impl ::bitflags::__private::core::ops::BitAnd for RangeFlags {
type Output = Self;
#[inline]
fn bitand(self, other: Self) -> Self { self.intersection(other) }
}
impl ::bitflags::__private::core::ops::BitAndAssign for RangeFlags {
#[inline]
fn bitand_assign(&mut self, other: Self) {
*self =
Self::from_bits_retain(self.bits()).intersection(other);
}
}
impl ::bitflags::__private::core::ops::Sub for RangeFlags {
type Output = Self;
#[inline]
fn sub(self, other: Self) -> Self { self.difference(other) }
}
impl ::bitflags::__private::core::ops::SubAssign for RangeFlags {
#[inline]
fn sub_assign(&mut self, other: Self) { self.remove(other); }
}
impl ::bitflags::__private::core::ops::Not for RangeFlags {
type Output = Self;
#[inline]
fn not(self) -> Self { self.complement() }
}
impl ::bitflags::__private::core::iter::Extend<RangeFlags> for
RangeFlags {
fn extend<T: ::bitflags::__private::core::iter::IntoIterator<Item
= Self>>(&mut self, iterator: T) {
for item in iterator { self.insert(item) }
}
}
impl ::bitflags::__private::core::iter::FromIterator<RangeFlags> for
RangeFlags {
fn from_iter<T: ::bitflags::__private::core::iter::IntoIterator<Item
= Self>>(iterator: T) -> Self {
use ::bitflags::__private::core::iter::Extend;
let mut result = Self::empty();
result.extend(iterator);
result
}
}
impl RangeFlags {
#[inline]
pub const fn iter(&self) -> ::bitflags::iter::Iter<RangeFlags> {
::bitflags::iter::Iter::__private_const_new(<RangeFlags as
::bitflags::Flags>::FLAGS,
RangeFlags::from_bits_retain(self.bits()),
RangeFlags::from_bits_retain(self.bits()))
}
#[inline]
pub const fn iter_names(&self)
-> ::bitflags::iter::IterNames<RangeFlags> {
::bitflags::iter::IterNames::__private_const_new(<RangeFlags
as ::bitflags::Flags>::FLAGS,
RangeFlags::from_bits_retain(self.bits()),
RangeFlags::from_bits_retain(self.bits()))
}
}
impl ::bitflags::__private::core::iter::IntoIterator for RangeFlags {
type Item = RangeFlags;
type IntoIter = ::bitflags::iter::Iter<RangeFlags>;
fn into_iter(self) -> Self::IntoIter { self.iter() }
}
};bitflags::bitflags! {
16 struct RangeFlags: u8 {
17 const EMPTY = 0x01;
18 const LB_INC = 0x02;
19 const UB_INC = 0x04;
20 const LB_INF = 0x08;
21 const UB_INF = 0x10;
22 const LB_NULL = 0x20;
23 const UB_NULL = 0x40;
24 const CONTAIN_EMPTY = 0x80;
25 }
26}
27
28macro_rules! range_as_expression {
29 ($ty:ty; $sql_type:ty) => {
30 #[cfg(feature = "postgres_backend")]
31 #[allow(clippy::extra_unused_lifetimes)]
34 impl<'a, ST: 'static, T> AsExpression<$sql_type> for $ty {
35 type Expression = SqlBound<$sql_type, Self>;
36
37 fn as_expression(self) -> Self::Expression {
38 SqlBound::new(self)
39 }
40 }
41 };
42}
43
44#[allow(clippy :: extra_unused_lifetimes)]
impl<'a, ST: 'static, T> AsExpression<Range<ST>> for (Bound<T>, Bound<T>) {
type Expression = SqlBound<Range<ST>, Self>;
fn as_expression(self) -> Self::Expression { SqlBound::new(self) }
}range_as_expression!((Bound<T>, Bound<T>); Range<ST>);
45#[allow(clippy :: extra_unused_lifetimes)]
impl<'a, ST: 'static, T> AsExpression<Range<ST>> for &'a (Bound<T>, Bound<T>)
{
type Expression = SqlBound<Range<ST>, Self>;
fn as_expression(self) -> Self::Expression { SqlBound::new(self) }
}range_as_expression!(&'a (Bound<T>, Bound<T>); Range<ST>);
46#[allow(clippy :: extra_unused_lifetimes)]
impl<'a, ST: 'static, T> AsExpression<Nullable<Range<ST>>> for
(Bound<T>, Bound<T>) {
type Expression = SqlBound<Nullable<Range<ST>>, Self>;
fn as_expression(self) -> Self::Expression { SqlBound::new(self) }
}range_as_expression!((Bound<T>, Bound<T>); Nullable<Range<ST>>);
47#[allow(clippy :: extra_unused_lifetimes)]
impl<'a, ST: 'static, T> AsExpression<Nullable<Range<ST>>> for
&'a (Bound<T>, Bound<T>) {
type Expression = SqlBound<Nullable<Range<ST>>, Self>;
fn as_expression(self) -> Self::Expression { SqlBound::new(self) }
}range_as_expression!(&'a (Bound<T>, Bound<T>); Nullable<Range<ST>>);
48
49#[allow(clippy :: extra_unused_lifetimes)]
impl<'a, ST: 'static, T> AsExpression<Range<ST>> for std::ops::Range<T> {
type Expression = SqlBound<Range<ST>, Self>;
fn as_expression(self) -> Self::Expression { SqlBound::new(self) }
}range_as_expression!(std::ops::Range<T>; Range<ST>);
50#[allow(clippy :: extra_unused_lifetimes)]
impl<'a, ST: 'static, T> AsExpression<Range<ST>> for &'a std::ops::Range<T> {
type Expression = SqlBound<Range<ST>, Self>;
fn as_expression(self) -> Self::Expression { SqlBound::new(self) }
}range_as_expression!(&'a std::ops::Range<T>; Range<ST>);
51#[allow(clippy :: extra_unused_lifetimes)]
impl<'a, ST: 'static, T> AsExpression<Nullable<Range<ST>>> for
std::ops::Range<T> {
type Expression = SqlBound<Nullable<Range<ST>>, Self>;
fn as_expression(self) -> Self::Expression { SqlBound::new(self) }
}range_as_expression!(std::ops::Range<T>; Nullable<Range<ST>>);
52#[allow(clippy :: extra_unused_lifetimes)]
impl<'a, ST: 'static, T> AsExpression<Nullable<Range<ST>>> for
&'a std::ops::Range<T> {
type Expression = SqlBound<Nullable<Range<ST>>, Self>;
fn as_expression(self) -> Self::Expression { SqlBound::new(self) }
}range_as_expression!(&'a std::ops::Range<T>; Nullable<Range<ST>>);
53
54#[allow(clippy :: extra_unused_lifetimes)]
impl<'a, ST: 'static, T> AsExpression<Range<ST>> for
std::ops::RangeInclusive<T> {
type Expression = SqlBound<Range<ST>, Self>;
fn as_expression(self) -> Self::Expression { SqlBound::new(self) }
}range_as_expression!(std::ops::RangeInclusive<T>; Range<ST>);
55#[allow(clippy :: extra_unused_lifetimes)]
impl<'a, ST: 'static, T> AsExpression<Range<ST>> for
&'a std::ops::RangeInclusive<T> {
type Expression = SqlBound<Range<ST>, Self>;
fn as_expression(self) -> Self::Expression { SqlBound::new(self) }
}range_as_expression!(&'a std::ops::RangeInclusive<T>; Range<ST>);
56#[allow(clippy :: extra_unused_lifetimes)]
impl<'a, ST: 'static, T> AsExpression<Nullable<Range<ST>>> for
std::ops::RangeInclusive<T> {
type Expression = SqlBound<Nullable<Range<ST>>, Self>;
fn as_expression(self) -> Self::Expression { SqlBound::new(self) }
}range_as_expression!(std::ops::RangeInclusive<T>; Nullable<Range<ST>>);
57#[allow(clippy :: extra_unused_lifetimes)]
impl<'a, ST: 'static, T> AsExpression<Nullable<Range<ST>>> for
&'a std::ops::RangeInclusive<T> {
type Expression = SqlBound<Nullable<Range<ST>>, Self>;
fn as_expression(self) -> Self::Expression { SqlBound::new(self) }
}range_as_expression!(&'a std::ops::RangeInclusive<T>; Nullable<Range<ST>>);
58
59#[allow(clippy :: extra_unused_lifetimes)]
impl<'a, ST: 'static, T> AsExpression<Range<ST>> for
std::ops::RangeToInclusive<T> {
type Expression = SqlBound<Range<ST>, Self>;
fn as_expression(self) -> Self::Expression { SqlBound::new(self) }
}range_as_expression!(std::ops::RangeToInclusive<T>; Range<ST>);
60#[allow(clippy :: extra_unused_lifetimes)]
impl<'a, ST: 'static, T> AsExpression<Range<ST>> for
&'a std::ops::RangeToInclusive<T> {
type Expression = SqlBound<Range<ST>, Self>;
fn as_expression(self) -> Self::Expression { SqlBound::new(self) }
}range_as_expression!(&'a std::ops::RangeToInclusive<T>; Range<ST>);
61#[allow(clippy :: extra_unused_lifetimes)]
impl<'a, ST: 'static, T> AsExpression<Nullable<Range<ST>>> for
std::ops::RangeToInclusive<T> {
type Expression = SqlBound<Nullable<Range<ST>>, Self>;
fn as_expression(self) -> Self::Expression { SqlBound::new(self) }
}range_as_expression!(std::ops::RangeToInclusive<T>; Nullable<Range<ST>>);
62#[allow(clippy :: extra_unused_lifetimes)]
impl<'a, ST: 'static, T> AsExpression<Nullable<Range<ST>>> for
&'a std::ops::RangeToInclusive<T> {
type Expression = SqlBound<Nullable<Range<ST>>, Self>;
fn as_expression(self) -> Self::Expression { SqlBound::new(self) }
}range_as_expression!(&'a std::ops::RangeToInclusive<T>; Nullable<Range<ST>>);
63
64#[allow(clippy :: extra_unused_lifetimes)]
impl<'a, ST: 'static, T> AsExpression<Range<ST>> for std::ops::RangeFrom<T> {
type Expression = SqlBound<Range<ST>, Self>;
fn as_expression(self) -> Self::Expression { SqlBound::new(self) }
}range_as_expression!(std::ops::RangeFrom<T>; Range<ST>);
65#[allow(clippy :: extra_unused_lifetimes)]
impl<'a, ST: 'static, T> AsExpression<Range<ST>> for
&'a std::ops::RangeFrom<T> {
type Expression = SqlBound<Range<ST>, Self>;
fn as_expression(self) -> Self::Expression { SqlBound::new(self) }
}range_as_expression!(&'a std::ops::RangeFrom<T>; Range<ST>);
66#[allow(clippy :: extra_unused_lifetimes)]
impl<'a, ST: 'static, T> AsExpression<Nullable<Range<ST>>> for
std::ops::RangeFrom<T> {
type Expression = SqlBound<Nullable<Range<ST>>, Self>;
fn as_expression(self) -> Self::Expression { SqlBound::new(self) }
}range_as_expression!(std::ops::RangeFrom<T>; Nullable<Range<ST>>);
67#[allow(clippy :: extra_unused_lifetimes)]
impl<'a, ST: 'static, T> AsExpression<Nullable<Range<ST>>> for
&'a std::ops::RangeFrom<T> {
type Expression = SqlBound<Nullable<Range<ST>>, Self>;
fn as_expression(self) -> Self::Expression { SqlBound::new(self) }
}range_as_expression!(&'a std::ops::RangeFrom<T>; Nullable<Range<ST>>);
68
69#[allow(clippy :: extra_unused_lifetimes)]
impl<'a, ST: 'static, T> AsExpression<Range<ST>> for std::ops::RangeTo<T> {
type Expression = SqlBound<Range<ST>, Self>;
fn as_expression(self) -> Self::Expression { SqlBound::new(self) }
}range_as_expression!(std::ops::RangeTo<T>; Range<ST>);
70#[allow(clippy :: extra_unused_lifetimes)]
impl<'a, ST: 'static, T> AsExpression<Range<ST>> for &'a std::ops::RangeTo<T>
{
type Expression = SqlBound<Range<ST>, Self>;
fn as_expression(self) -> Self::Expression { SqlBound::new(self) }
}range_as_expression!(&'a std::ops::RangeTo<T>; Range<ST>);
71#[allow(clippy :: extra_unused_lifetimes)]
impl<'a, ST: 'static, T> AsExpression<Nullable<Range<ST>>> for
std::ops::RangeTo<T> {
type Expression = SqlBound<Nullable<Range<ST>>, Self>;
fn as_expression(self) -> Self::Expression { SqlBound::new(self) }
}range_as_expression!(std::ops::RangeTo<T>; Nullable<Range<ST>>);
72#[allow(clippy :: extra_unused_lifetimes)]
impl<'a, ST: 'static, T> AsExpression<Nullable<Range<ST>>> for
&'a std::ops::RangeTo<T> {
type Expression = SqlBound<Nullable<Range<ST>>, Self>;
fn as_expression(self) -> Self::Expression { SqlBound::new(self) }
}range_as_expression!(&'a std::ops::RangeTo<T>; Nullable<Range<ST>>);
73
74#[cfg(feature = "postgres_backend")]
75impl<T, ST> FromSql<Range<ST>, Pg> for (Bound<T>, Bound<T>)
76where
77 T: FromSql<ST, Pg> + Defaultable,
78{
79 fn from_sql(value: PgValue<'_>) -> deserialize::Result<Self> {
80 let mut bytes = value.as_bytes();
81 let flags: RangeFlags = RangeFlags::from_bits_truncate(bytes.read_u8()?);
82 let mut lower_bound = Bound::Unbounded;
83 let mut upper_bound = Bound::Unbounded;
84
85 if flags.contains(RangeFlags::EMPTY) {
86 lower_bound = Bound::Excluded(T::default_value());
87 } else if !flags.contains(RangeFlags::LB_INF) {
88 let elem_size = bytes.read_i32::<NetworkEndian>()?;
89 let (elem_bytes, new_bytes) = bytes.split_at(elem_size.try_into()?);
90 bytes = new_bytes;
91 let value = T::from_sql(PgValue::new_internal(elem_bytes, &value))?;
92
93 lower_bound = if flags.contains(RangeFlags::LB_INC) {
94 Bound::Included(value)
95 } else {
96 Bound::Excluded(value)
97 };
98 }
99
100 if flags.contains(RangeFlags::EMPTY) {
101 upper_bound = Bound::Excluded(T::default_value());
102 } else if !flags.contains(RangeFlags::UB_INF) {
103 let _size = bytes.read_i32::<NetworkEndian>()?;
104 let value = T::from_sql(PgValue::new_internal(bytes, &value))?;
105
106 upper_bound = if flags.contains(RangeFlags::UB_INC) {
107 Bound::Included(value)
108 } else {
109 Bound::Excluded(value)
110 };
111 }
112
113 Ok((lower_bound, upper_bound))
114 }
115}
116
117#[cfg(feature = "postgres_backend")]
118impl<T, ST> Queryable<Range<ST>, Pg> for (Bound<T>, Bound<T>)
119where
120 T: FromSql<ST, Pg> + Defaultable,
121{
122 type Row = Self;
123
124 fn build(row: Self) -> deserialize::Result<Self> {
125 Ok(row)
126 }
127}
128
129#[cfg(feature = "postgres_backend")]
130fn to_sql<ST, T>(
131 start: Bound<&T>,
132 end: Bound<&T>,
133 out: &mut Output<'_, '_, Pg>,
134) -> serialize::Result
135where
136 T: ToSql<ST, Pg>,
137{
138 let mut flags = match start {
139 Bound::Included(_) => RangeFlags::LB_INC,
140 Bound::Excluded(_) => RangeFlags::empty(),
141 Bound::Unbounded => RangeFlags::LB_INF,
142 };
143
144 flags |= match end {
145 Bound::Included(_) => RangeFlags::UB_INC,
146 Bound::Excluded(_) => RangeFlags::empty(),
147 Bound::Unbounded => RangeFlags::UB_INF,
148 };
149
150 out.write_u8(flags.bits())?;
151
152 let mut buffer = Vec::new();
153
154 match start {
155 Bound::Included(ref value) | Bound::Excluded(ref value) => {
156 {
157 let mut inner_buffer = Output::new(ByteWrapper(&mut buffer), out.metadata_lookup());
158 value.to_sql(&mut inner_buffer)?;
159 }
160 out.write_u32::<NetworkEndian>(buffer.len().try_into()?)?;
161 out.write_all(&buffer)?;
162 buffer.clear();
163 }
164 Bound::Unbounded => {}
165 }
166
167 match end {
168 Bound::Included(ref value) | Bound::Excluded(ref value) => {
169 {
170 let mut inner_buffer = Output::new(ByteWrapper(&mut buffer), out.metadata_lookup());
171 value.to_sql(&mut inner_buffer)?;
172 }
173 out.write_u32::<NetworkEndian>(buffer.len().try_into()?)?;
174 out.write_all(&buffer)?;
175 }
176 Bound::Unbounded => {}
177 }
178
179 Ok(IsNull::No)
180}
181
182#[cfg(feature = "postgres_backend")]
183impl<ST, T> ToSql<Range<ST>, Pg> for (Bound<T>, Bound<T>)
184where
185 T: ToSql<ST, Pg>,
186{
187 fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
188 to_sql(self.0.as_ref(), self.1.as_ref(), out)
189 }
190}
191
192use std::ops::RangeBounds;
193macro_rules! range_std_to_sql {
194 ($ty:ty) => {
195 #[cfg(feature = "postgres_backend")]
196 impl<ST, T> ToSql<Range<ST>, Pg> for $ty
197 where
198 ST: 'static,
199 T: ToSql<ST, Pg>,
200 {
201 fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
202 to_sql(self.start_bound(), self.end_bound(), out)
203 }
204 }
205 };
206}
207
208impl<ST, T> ToSql<Range<ST>, Pg> for std::ops::Range<T> where ST: 'static,
T: ToSql<ST, Pg> {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>)
-> serialize::Result {
to_sql(self.start_bound(), self.end_bound(), out)
}
}range_std_to_sql!(std::ops::Range<T>);
209impl<ST, T> ToSql<Range<ST>, Pg> for std::ops::RangeInclusive<T> where
ST: 'static, T: ToSql<ST, Pg> {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>)
-> serialize::Result {
to_sql(self.start_bound(), self.end_bound(), out)
}
}range_std_to_sql!(std::ops::RangeInclusive<T>);
210impl<ST, T> ToSql<Range<ST>, Pg> for std::ops::RangeFrom<T> where ST: 'static,
T: ToSql<ST, Pg> {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>)
-> serialize::Result {
to_sql(self.start_bound(), self.end_bound(), out)
}
}range_std_to_sql!(std::ops::RangeFrom<T>);
211impl<ST, T> ToSql<Range<ST>, Pg> for std::ops::RangeTo<T> where ST: 'static,
T: ToSql<ST, Pg> {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>)
-> serialize::Result {
to_sql(self.start_bound(), self.end_bound(), out)
}
}range_std_to_sql!(std::ops::RangeTo<T>);
212impl<ST, T> ToSql<Range<ST>, Pg> for std::ops::RangeToInclusive<T> where
ST: 'static, T: ToSql<ST, Pg> {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>)
-> serialize::Result {
to_sql(self.start_bound(), self.end_bound(), out)
}
}range_std_to_sql!(std::ops::RangeToInclusive<T>);
213
214macro_rules! range_to_sql_nullable {
215 ($ty:ty) => {
216 #[cfg(feature = "postgres_backend")]
217 impl<ST, T> ToSql<Nullable<Range<ST>>, Pg> for $ty
218 where
219 ST: 'static,
220 $ty: ToSql<Range<ST>, Pg>,
221 {
222 fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
223 ToSql::<Range<ST>, Pg>::to_sql(self, out)
224 }
225 }
226 };
227}
228impl<ST, T> ToSql<Nullable<Range<ST>>, Pg> for (Bound<T>, Bound<T>) where
ST: 'static, (Bound<T>, Bound<T>): ToSql<Range<ST>, Pg> {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>)
-> serialize::Result {
ToSql::<Range<ST>, Pg>::to_sql(self, out)
}
}range_to_sql_nullable!((Bound<T>, Bound<T>));
229impl<ST, T> ToSql<Nullable<Range<ST>>, Pg> for std::ops::Range<T> where
ST: 'static, std::ops::Range<T>: ToSql<Range<ST>, Pg> {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>)
-> serialize::Result {
ToSql::<Range<ST>, Pg>::to_sql(self, out)
}
}range_to_sql_nullable!(std::ops::Range<T>);
230impl<ST, T> ToSql<Nullable<Range<ST>>, Pg> for std::ops::RangeInclusive<T>
where ST: 'static, std::ops::RangeInclusive<T>: ToSql<Range<ST>, Pg> {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>)
-> serialize::Result {
ToSql::<Range<ST>, Pg>::to_sql(self, out)
}
}range_to_sql_nullable!(std::ops::RangeInclusive<T>);
231impl<ST, T> ToSql<Nullable<Range<ST>>, Pg> for std::ops::RangeFrom<T> where
ST: 'static, std::ops::RangeFrom<T>: ToSql<Range<ST>, Pg> {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>)
-> serialize::Result {
ToSql::<Range<ST>, Pg>::to_sql(self, out)
}
}range_to_sql_nullable!(std::ops::RangeFrom<T>);
232impl<ST, T> ToSql<Nullable<Range<ST>>, Pg> for std::ops::RangeTo<T> where
ST: 'static, std::ops::RangeTo<T>: ToSql<Range<ST>, Pg> {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>)
-> serialize::Result {
ToSql::<Range<ST>, Pg>::to_sql(self, out)
}
}range_to_sql_nullable!(std::ops::RangeTo<T>);
233impl<ST, T> ToSql<Nullable<Range<ST>>, Pg> for std::ops::RangeToInclusive<T>
where ST: 'static, std::ops::RangeToInclusive<T>: ToSql<Range<ST>, Pg> {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>)
-> serialize::Result {
ToSql::<Range<ST>, Pg>::to_sql(self, out)
}
}range_to_sql_nullable!(std::ops::RangeToInclusive<T>);
234
235#[cfg(feature = "postgres_backend")]
236impl HasSqlType<Int4range> for Pg {
237 fn metadata(_: &mut Self::MetadataLookup) -> PgTypeMetadata {
238 PgTypeMetadata::new(3904, 3905)
239 }
240}
241
242#[cfg(feature = "postgres_backend")]
243impl HasSqlType<Numrange> for Pg {
244 fn metadata(_: &mut Self::MetadataLookup) -> PgTypeMetadata {
245 PgTypeMetadata::new(3906, 3907)
246 }
247}
248
249impl HasSqlType<Tsrange> for Pg {
250 fn metadata(_: &mut Self::MetadataLookup) -> PgTypeMetadata {
251 PgTypeMetadata::new(3908, 3909)
252 }
253}
254
255#[cfg(feature = "postgres_backend")]
256impl HasSqlType<Tstzrange> for Pg {
257 fn metadata(_: &mut Self::MetadataLookup) -> PgTypeMetadata {
258 PgTypeMetadata::new(3910, 3911)
259 }
260}
261
262#[cfg(feature = "postgres_backend")]
263impl HasSqlType<Daterange> for Pg {
264 fn metadata(_: &mut Self::MetadataLookup) -> PgTypeMetadata {
265 PgTypeMetadata::new(3912, 3913)
266 }
267}
268
269#[cfg(feature = "postgres_backend")]
270impl HasSqlType<Int8range> for Pg {
271 fn metadata(_: &mut Self::MetadataLookup) -> PgTypeMetadata {
272 PgTypeMetadata::new(3926, 3927)
273 }
274}
275
276#[cfg(feature = "postgres_backend")]
277impl ToSql<RangeBoundEnum, Pg> for RangeBound {
278 fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
279 let literal = match self {
280 Self::LowerBoundInclusiveUpperBoundInclusive => "[]",
281 Self::LowerBoundInclusiveUpperBoundExclusive => "[)",
282 Self::LowerBoundExclusiveUpperBoundInclusive => "(]",
283 Self::LowerBoundExclusiveUpperBoundExclusive => "()",
284 };
285 out.write_all(literal.as_bytes())
286 .map(|_| IsNull::No)
287 .map_err(|e| Box::new(e) as Box<dyn Error + Send + Sync>)
288 }
289}