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

remove the dependency on cstr_core #1025

Merged
merged 2 commits into from
Feb 1, 2023
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
18 changes: 0 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ Postgres Type | Rust Type (as `Option<T>`)
`box` | `pgx::pg_sys::BOX`
`point` | `pgx::pgx_sys::Point`
`tid` | `pgx::pg_sys::ItemPointerData`
`cstring` | `&std::ffi::CStr`
`cstring` | `&core::ffi::CStr`
`inet` | `pgx::Inet(String)` -- TODO: needs better support
`numeric` | `pgx::Numeric<P, S> or pgx::AnyNumeric`
`void` | `()`
Expand Down
4 changes: 2 additions & 2 deletions articles/postgresql-aggregates-with-rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ CREATE TYPE DemoSum;
-- src/lib.rs:6
-- exploring_aggregates::demosum_in
CREATE OR REPLACE FUNCTION "demosum_in"(
"input" cstring /* &cstr_core::CStr */
"input" cstring /* &std::ffi::CStr */
) RETURNS DemoSum /* exploring_aggregates::DemoSum */
IMMUTABLE PARALLEL SAFE STRICT
LANGUAGE c /* Rust */
Expand All @@ -436,7 +436,7 @@ AS 'MODULE_PATHNAME', 'demosum_in_wrapper';
-- exploring_aggregates::demosum_out
CREATE OR REPLACE FUNCTION "demosum_out"(
"input" DemoSum /* exploring_aggregates::DemoSum */
) RETURNS cstring /* &cstr_core::CStr */
) RETURNS cstring /* &std::ffi::CStr */
IMMUTABLE PARALLEL SAFE STRICT
LANGUAGE c /* Rust */
AS 'MODULE_PATHNAME', 'demosum_out_wrapper';
Expand Down
2 changes: 1 addition & 1 deletion pgx-examples/aggregate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ All rights reserved.

Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/
use core::ffi::CStr;
use pgx::aggregate::*;
use pgx::cstr_core::CStr;
use pgx::prelude::*;
use pgx::{pgx, PgVarlena, PgVarlenaInOutFuncs, StringInfo};
use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion pgx-examples/custom_types/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ struct MyType {
impl PgVarlenaInOutFuncs for MyType {

// parse the provided CStr into a `PgVarlena<MyType>`
fn input(input: &std::ffi::CStr) -> PgVarlena<Self> {
fn input(input: &core::ffi::CStr) -> PgVarlena<Self> {
let mut iter = input.to_str().unwrap().split(',');
let (a, b, c) = (iter.next(), iter.next(), iter.next());

Expand Down
2 changes: 1 addition & 1 deletion pgx-examples/custom_types/src/fixed_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ All rights reserved.

Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/
use pgx::cstr_core::CStr;
use core::ffi::CStr;
use pgx::prelude::*;
use pgx::{opname, pg_operator, PgVarlena, PgVarlenaInOutFuncs, StringInfo};
use std::str::FromStr;
Expand Down
16 changes: 8 additions & 8 deletions pgx-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,12 +348,12 @@ extension_sql!(r#"\
);

#[pg_extern(immutable)]
fn complex_in(input: &pgx::cstr_core::CStr) -> PgBox<Complex> {
fn complex_in(input: &core::ffi::CStr) -> PgBox<Complex> {
todo!()
}

#[pg_extern(immutable)]
fn complex_out(complex: PgBox<Complex>) -> &'static pgx::cstr_core::CStr {
fn complex_out(complex: PgBox<Complex>) -> &'static core::ffi::CStr {
todo!()
}

Expand Down Expand Up @@ -769,7 +769,7 @@ fn impl_postgres_type(ast: DeriveInput) -> syn::Result<proc_macro2::TokenStream>

#[doc(hidden)]
#[::pgx::pgx_macros::pg_extern(immutable,parallel_safe)]
pub fn #funcname_in #generics(input: Option<&#lifetime ::pgx::cstr_core::CStr>) -> Option<#name #generics> {
pub fn #funcname_in #generics(input: Option<&#lifetime ::core::ffi::CStr>) -> Option<#name #generics> {
input.map_or_else(|| {
for m in <#name as ::pgx::inoutfuncs::JsonInOutFuncs>::NULL_ERROR_MESSAGE {
::pgx::pg_sys::error!("{}", m);
Expand All @@ -780,7 +780,7 @@ fn impl_postgres_type(ast: DeriveInput) -> syn::Result<proc_macro2::TokenStream>

#[doc(hidden)]
#[::pgx::pgx_macros::pg_extern(immutable,parallel_safe)]
pub fn #funcname_out #generics(input: #name #generics) -> &#lifetime ::pgx::cstr_core::CStr {
pub fn #funcname_out #generics(input: #name #generics) -> &#lifetime ::core::ffi::CStr {
let mut buffer = ::pgx::stringinfo::StringInfo::new();
::pgx::inoutfuncs::JsonInOutFuncs::output(&input, &mut buffer);
buffer.into()
Expand All @@ -792,7 +792,7 @@ fn impl_postgres_type(ast: DeriveInput) -> syn::Result<proc_macro2::TokenStream>
stream.extend(quote! {
#[doc(hidden)]
#[::pgx::pgx_macros::pg_extern(immutable,parallel_safe)]
pub fn #funcname_in #generics(input: Option<&#lifetime ::pgx::cstr_core::CStr>) -> Option<#name #generics> {
pub fn #funcname_in #generics(input: Option<&#lifetime ::core::ffi::CStr>) -> Option<#name #generics> {
input.map_or_else(|| {
for m in <#name as ::pgx::inoutfuncs::InOutFuncs>::NULL_ERROR_MESSAGE {
::pgx::pg_sys::error!("{}", m);
Expand All @@ -803,7 +803,7 @@ fn impl_postgres_type(ast: DeriveInput) -> syn::Result<proc_macro2::TokenStream>

#[doc(hidden)]
#[::pgx::pgx_macros::pg_extern(immutable,parallel_safe)]
pub fn #funcname_out #generics(input: #name #generics) -> &#lifetime ::pgx::cstr_core::CStr {
pub fn #funcname_out #generics(input: #name #generics) -> &#lifetime ::core::ffi::CStr {
let mut buffer = ::pgx::stringinfo::StringInfo::new();
::pgx::inoutfuncs::InOutFuncs::output(&input, &mut buffer);
buffer.into()
Expand All @@ -814,7 +814,7 @@ fn impl_postgres_type(ast: DeriveInput) -> syn::Result<proc_macro2::TokenStream>
stream.extend(quote! {
#[doc(hidden)]
#[::pgx::pgx_macros::pg_extern(immutable,parallel_safe)]
pub fn #funcname_in #generics(input: Option<&#lifetime ::pgx::cstr_core::CStr>) -> Option<::pgx::datum::PgVarlena<#name #generics>> {
pub fn #funcname_in #generics(input: Option<&#lifetime ::core::ffi::CStr>) -> Option<::pgx::datum::PgVarlena<#name #generics>> {
input.map_or_else(|| {
for m in <#name as ::pgx::inoutfuncs::PgVarlenaInOutFuncs>::NULL_ERROR_MESSAGE {
::pgx::pg_sys::error!("{}", m);
Expand All @@ -825,7 +825,7 @@ fn impl_postgres_type(ast: DeriveInput) -> syn::Result<proc_macro2::TokenStream>

#[doc(hidden)]
#[::pgx::pgx_macros::pg_extern(immutable,parallel_safe)]
pub fn #funcname_out #generics(input: ::pgx::datum::PgVarlena<#name #generics>) -> &#lifetime ::pgx::cstr_core::CStr {
pub fn #funcname_out #generics(input: ::pgx::datum::PgVarlena<#name #generics>) -> &#lifetime ::core::ffi::CStr {
let mut buffer = ::pgx::stringinfo::StringInfo::new();
::pgx::inoutfuncs::PgVarlenaInOutFuncs::output(&*input, &mut buffer);
buffer.into()
Expand Down
8 changes: 4 additions & 4 deletions pgx-pg-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ std::compile_error!("exactly one one feature must be provided (pg11, pg12, pg13,

pub mod submodules;

use core::ffi::CStr;
use core::ptr::NonNull;
use std::ffi::CStr;
use std::os::raw::c_char;

// for convenience we pull up everything submodules exposes
Expand Down Expand Up @@ -427,7 +427,7 @@ mod all_versions {

#[inline]
pub fn get_pg_major_version_string() -> &'static str {
let mver = std::ffi::CStr::from_bytes_with_nul(super::PG_MAJORVERSION).unwrap();
let mver = core::ffi::CStr::from_bytes_with_nul(super::PG_MAJORVERSION).unwrap();
mver.to_str().unwrap()
}

Expand All @@ -438,13 +438,13 @@ mod all_versions {

#[inline]
pub fn get_pg_version_string() -> &'static str {
let ver = std::ffi::CStr::from_bytes_with_nul(super::PG_VERSION_STR).unwrap();
let ver = core::ffi::CStr::from_bytes_with_nul(super::PG_VERSION_STR).unwrap();
ver.to_str().unwrap()
}

#[inline]
pub fn get_pg_major_minor_version_string() -> &'static str {
let mver = std::ffi::CStr::from_bytes_with_nul(super::PG_VERSION).unwrap();
let mver = core::ffi::CStr::from_bytes_with_nul(super::PG_VERSION).unwrap();
mver.to_str().unwrap()
}

Expand Down
2 changes: 1 addition & 1 deletion pgx-pg-sys/src/submodules/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ unsafe fn pg_guard_ffi_boundary_impl<T, F: FnOnce() -> T>(f: F) -> T {

// just use these here to avoid compilation warnings when #[cfg(feature = "postgrestd")] is on
use crate::panic::{CaughtError, ErrorReport, ErrorReportLocation, ErrorReportWithLevel};
use std::ffi::CStr;
use core::ffi::CStr;

// The next code is definitely thread-unsafe (it manipulates statics in an
// unsynchronized manner), so we may as well check here.
Expand Down
2 changes: 1 addition & 1 deletion pgx-pg-sys/src/submodules/panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ Use of this source code is governed by the MIT license that can be found in the
#![deny(unsafe_op_in_unsafe_fn)]
#![allow(non_snake_case)]

use core::ffi::CStr;
use std::any::Any;
use std::cell::Cell;
use std::ffi::CStr;
use std::fmt::{Display, Formatter};
use std::hint::unreachable_unchecked;
use std::panic::{
Expand Down
2 changes: 1 addition & 1 deletion pgx-pg-sys/src/submodules/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ use crate as pg_sys;
/// of the provided `pg_sys::NameData`
#[inline]
pub fn name_data_to_str(name_data: &pg_sys::NameData) -> &str {
unsafe { std::ffi::CStr::from_ptr(name_data.data.as_ptr()) }.to_str().unwrap()
unsafe { core::ffi::CStr::from_ptr(name_data.data.as_ptr()) }.to_str().unwrap()
}
1 change: 0 additions & 1 deletion pgx-sql-entity-graph/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ no-schema-generation = []

[dependencies]
seq-macro = "0.3"
cstr_core = "0.2"
convert_case = "0.5.0"
eyre = "0.6.8"
petgraph = "0.6.2"
Expand Down
22 changes: 2 additions & 20 deletions pgx-sql-entity-graph/src/metadata/sql_translatable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ unsafe impl SqlTranslatable for f64 {
}
}

unsafe impl SqlTranslatable for std::ffi::CStr {
unsafe impl SqlTranslatable for core::ffi::CStr {
fn argument_sql() -> Result<SqlMapping, ArgumentError> {
Ok(SqlMapping::literal("cstring"))
}
Expand All @@ -351,25 +351,7 @@ unsafe impl SqlTranslatable for std::ffi::CStr {
}
}

unsafe impl SqlTranslatable for &'static std::ffi::CStr {
fn argument_sql() -> Result<SqlMapping, ArgumentError> {
Ok(SqlMapping::literal("cstring"))
}
fn return_sql() -> Result<Returns, ReturnsError> {
Ok(Returns::One(SqlMapping::literal("cstring")))
}
}

unsafe impl SqlTranslatable for &'static cstr_core::CStr {
fn argument_sql() -> Result<SqlMapping, ArgumentError> {
Ok(SqlMapping::literal("cstring"))
}
fn return_sql() -> Result<Returns, ReturnsError> {
Ok(Returns::One(SqlMapping::literal("cstring")))
}
}

unsafe impl SqlTranslatable for cstr_core::CStr {
unsafe impl SqlTranslatable for &'static core::ffi::CStr {
fn argument_sql() -> Result<SqlMapping, ArgumentError> {
Ok(SqlMapping::literal("cstring"))
}
Expand Down
4 changes: 2 additions & 2 deletions pgx-tests/src/tests/fcinfo_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ fn fcinfo_not_named_no_arg(fcinfo: pg_sys::FunctionCallInfo) -> i32 {
pub struct NullStrict {}

impl InOutFuncs for NullStrict {
fn input(_input: &pgx::cstr_core::CStr) -> Self
fn input(_input: &core::ffi::CStr) -> Self
where
Self: Sized,
{
Expand All @@ -149,7 +149,7 @@ impl InOutFuncs for NullStrict {
pub struct NullError {}

impl InOutFuncs for NullError {
fn input(_input: &pgx::cstr_core::CStr) -> Self
fn input(_input: &core::ffi::CStr) -> Self
where
Self: Sized,
{
Expand Down
2 changes: 1 addition & 1 deletion pgx-tests/src/tests/postgres_type_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ All rights reserved.

Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/
use pgx::cstr_core::CStr;
use core::ffi::CStr;
use pgx::prelude::*;
use pgx::{InOutFuncs, PgVarlena, PgVarlenaInOutFuncs, StringInfo};
use serde::{Deserialize, Serialize};
Expand Down
4 changes: 2 additions & 2 deletions pgx-tests/src/tests/struct_type_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ unsafe impl SqlTranslatable for Complex {
}

#[pg_extern(immutable)]
fn complex_in(input: &std::ffi::CStr) -> PgBox<Complex, AllocatedByRust> {
fn complex_in(input: &core::ffi::CStr) -> PgBox<Complex, AllocatedByRust> {
let input_as_str = input.to_str().unwrap();
let re = regex::Regex::new(
r#"(?P<x>[-+]?([0-9]*\.[0-9]+|[0-9]+)),\s*(?P<y>[-+]?([0-9]*\.[0-9]+|[0-9]+))"#,
Expand All @@ -57,7 +57,7 @@ fn complex_in(input: &std::ffi::CStr) -> PgBox<Complex, AllocatedByRust> {
}

#[pg_extern(immutable)]
fn complex_out(complex: PgBox<Complex>) -> &'static std::ffi::CStr {
fn complex_out(complex: PgBox<Complex>) -> &'static core::ffi::CStr {
let mut sb = StringInfo::new();
sb.push_str(&format!("{}, {}", &complex.x, &complex.y));
sb.into()
Expand Down
1 change: 0 additions & 1 deletion pgx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ tracing-error = "0.2.0"
atomic-traits = "0.3.0" # PgAtomic and shmem init
bitflags = "1.3.2" # BackgroundWorker
bitvec = "1.0" # processing array nullbitmaps
cstr_core = "0.2.6" # no std compat
heapless = "0.7.16" # shmem and PgLwLock
libc = "0.2.139" # FFI type compat
seahash = "4.1.0" # derive(PostgresHash)
Expand Down
2 changes: 1 addition & 1 deletion pgx/src/datum/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ 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::ffi::CStr;
use core::num::TryFromIntError;
use pgx_sql_entity_graph::metadata::{
ArgumentError, Returns, ReturnsError, SqlMapping, SqlTranslatable,
};
use std::ffi::CStr;

pub const POSTGRES_EPOCH_JDATE: i32 = pg_sys::POSTGRES_EPOCH_JDATE as i32;
pub const UNIX_EPOCH_JDATE: i32 = pg_sys::UNIX_EPOCH_JDATE as i32;
Expand Down
21 changes: 3 additions & 18 deletions pgx/src/datum/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
pg_sys, text_to_rust_str_unchecked, varlena_to_byte_slice, AllocatedByPostgres, IntoDatum,
PgBox, PgMemoryContexts,
};
use std::ffi::CStr;
use core::ffi::CStr;
use std::num::NonZeroUsize;

/// If converting a Datum to a Rust type fails, this is the set of possible reasons why.
Expand Down Expand Up @@ -398,7 +398,7 @@ impl FromDatum for char {
}

/// for cstring
impl<'a> FromDatum for &'a std::ffi::CStr {
impl<'a> FromDatum for &'a core::ffi::CStr {
#[inline]
unsafe fn from_polymorphic_datum(
datum: pg_sys::Datum,
Expand All @@ -408,22 +408,7 @@ impl<'a> FromDatum for &'a std::ffi::CStr {
if is_null || datum.is_null() {
None
} else {
Some(std::ffi::CStr::from_ptr(datum.cast_mut_ptr()))
}
}
}

impl<'a> FromDatum for &'a crate::cstr_core::CStr {
#[inline]
unsafe fn from_polymorphic_datum(
datum: pg_sys::Datum,
is_null: bool,
_: pg_sys::Oid,
) -> Option<&'a crate::cstr_core::CStr> {
if is_null || datum.is_null() {
None
} else {
Some(crate::cstr_core::CStr::from_ptr(datum.cast_mut_ptr()))
Some(core::ffi::CStr::from_ptr(datum.cast_mut_ptr()))
}
}
}
Expand Down
Loading