Skip to content

Commit

Permalink
Propagate TryFromIntError in TryFrom<Datum> for Date (#676)
Browse files Browse the repository at this point in the history
* Propagate TryFromIntError in `TryFrom<Datum> for Date`
* cfg dropping top bytes in pg10
  • Loading branch information
workingjubilee authored Sep 9, 2022
1 parent 414eb50 commit 9fad7d2
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions pgx/src/datum/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Use of this source code is governed by the MIT license that can be found in the
*/

use crate::{pg_sys, FromDatum, IntoDatum};
use core::num::TryFromIntError;
use std::ffi::CStr;

pub const POSTGRES_EPOCH_JDATE: i32 = pg_sys::POSTGRES_EPOCH_JDATE as i32;
Expand All @@ -18,9 +19,9 @@ pub const UNIX_EPOCH_JDATE: i32 = pg_sys::UNIX_EPOCH_JDATE as i32;
pub struct Date(i32);

impl TryFrom<pg_sys::Datum> for Date {
type Error = i32;
type Error = TryFromIntError;
fn try_from(d: pg_sys::Datum) -> Result<Self, Self::Error> {
Ok(Date(d.value() as i32))
i32::try_from(d.value() as isize).map(|d| Date(d))
}
}

Expand All @@ -41,7 +42,11 @@ impl FromDatum for Date {
if is_null {
None
} else {
Some(datum.try_into().expect("Error converting date datum"))
if cfg!(feature = "pg10") {
Some(Date(datum.value() as i32))
} else {
Some(datum.try_into().expect("Error converting date datum"))
}
}
}
}
Expand Down

0 comments on commit 9fad7d2

Please sign in to comment.