Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce feature flagging for time crate #664

Merged
merged 2 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,19 @@ There's a few things on our immediate TODO list
will be built into the `cargo-pgx` subcommand and make use of https://github.com/zombodb/postgres-parser.
- More examples -- especially around memory management and the various derive macros `#[derive(PostgresType/Enum)]`

## Experimental Features

## Feature Flags
PGX has optional feature flags for Rust code that do not involve configuring the version of Postgres used,
but rather extend additional support for other kinds of Rust code. These are not included by default.

### "time-crate": interop with the `time` crate
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This name works for me. Good job finding a name.

This crate once used direct interop with the excellent [time crate][timecrate].
However, due to complications involving performance and accurate interop with Postgres,
this feature is now considered deprecated in favor of a lower-overhead interop.
You may still request implementations of `TryFrom<time::Type> for pgx::MatchingType`
and `From<time::Type> for pgx::MatchingType` by enabling the `"time-crate"` feature.

### Experimental Features

Adding `pgx = { version = "0.5.0-beta.0", features = ["postgrestd"] }` to your Cargo.toml
will enable a **highly** experimental variant of `pgx` designed for integration with `postgrestd`,
Expand Down Expand Up @@ -269,3 +281,4 @@ Use of this source code is governed by the MIT license that can be found in the
```

[Discord]: https://discord.gg/hPb93Y9
[timecrate]: https://crates.io/crates/time
7 changes: 6 additions & 1 deletion pgx-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ rustdoc-args = ["--cfg", "docsrs"]
owo-colors = "3.4.0"
once_cell = "1.10.0"
libc = "0.2.126"
pgx = { path = "../pgx", default-features = false, version= "=0.5.0-beta.0" }
pgx-macros = { path = "../pgx-macros", version= "=0.5.0-beta.0" }
pgx-pg-config = { path = "../pgx-pg-config", version= "=0.5.0-beta.0" }
pgx-utils = { path = "../pgx-utils", version= "=0.5.0-beta.0" }
Expand All @@ -47,3 +46,9 @@ shutdown_hooks = "0.1.0"
time = "0.3.9"
eyre = "0.6.8"
thiserror = "1.0"

[dependencies.pgx]
path = "../pgx"
default-features = false
features = [ "time-crate" ] # testing purposes
version= "=0.5.0-beta.0"
3 changes: 2 additions & 1 deletion pgx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pg11 = [ "pgx-pg-sys/pg11" ]
pg12 = [ "pgx-pg-sys/pg12" ]
pg13 = [ "pgx-pg-sys/pg13" ]
pg14 = [ "pgx-pg-sys/pg14" ]
time-crate = [ ] # TODO(0.6.0): add "dep:time"

[package.metadata.docs.rs]
features = ["pg14"]
Expand All @@ -43,7 +44,7 @@ pgx-utils = { path = "../pgx-utils/", version = "=0.5.0-beta.0" }
serde = { version = "1.0.137", features = [ "derive" ] }
serde_cbor = "0.11.2"
serde_json = "1.0.81"
time = { version = "0.3.9", features = ["formatting", "parsing", "alloc", "macros"] }
time = { version = "0.3.9", features = ["formatting", "parsing", "alloc", "macros"] } # TODO(0.6.0): add `optional = true`
atomic-traits = "0.3.0"
heapless = "0.7.13"
uuid = { version = "1.0.0", features = [ "v4" ] }
Expand Down
17 changes: 13 additions & 4 deletions pgx/src/datum/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ impl Date {
#[inline]
#[deprecated(
since = "0.5.0",
note = "the repr of pgx::Date is no longer time::Date \
and this fn will be removed in a future version"
note = "the repr of pgx::Date is no longer time::Date and this fn will be broken in a future version\n\
please use pgx's `time-crate` feature to opt-in to `From<time::Date> for pgx::Date`"
)]
pub fn new(date: time::Date) -> Date {
date.into()
// TODO(0.6.0): remove this
timecrate_date_to_pg_date(date)
}

#[inline]
Expand Down Expand Up @@ -96,13 +97,21 @@ impl Date {
}
}

#[cfg(feature = "time-crate")]
impl From<time::Date> for Date {
#[inline]
fn from(date: time::Date) -> Self {
Date::from_pg_epoch_days(date.to_julian_day() - POSTGRES_EPOCH_JDATE)
timecrate_date_to_pg_date(date)
}
}

// This function only exists as a temporary shim while the deprecation cycle for Date::new is running
// TODO(0.6.0): remove this
#[inline(always)]
fn timecrate_date_to_pg_date(date: time::Date) -> Date {
Date::from_pg_epoch_days(date.to_julian_day() - POSTGRES_EPOCH_JDATE)
}

impl TryFrom<Date> for time::Date {
type Error = i32;
fn try_from(date: Date) -> Result<time::Date, Self::Error> {
Expand Down