Skip to content

Commit

Permalink
Modularize the interior of pgrx-pg-sys (#1227)
Browse files Browse the repository at this point in the history
The pgrx-pg-sys crate has been gradually doing better, but the primary
module was still a mess of bizarre configuration code strewn about.
There's a certain inevitability of that, in some ways, but we can
recover a lot of implicit information by organizing it, with fairly
little effort. Now it's easy to tell the difference between macro ports
and cshim functions at a glance.
  • Loading branch information
workingjubilee authored Jul 25, 2023
1 parent bbcbad1 commit c83c203
Show file tree
Hide file tree
Showing 20 changed files with 781 additions and 800 deletions.
2 changes: 1 addition & 1 deletion pgrx-pg-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ impl BuildPaths {
let manifest_dir = env_tracked("CARGO_MANIFEST_DIR").map(PathBuf::from).unwrap();
let out_dir = env_tracked("OUT_DIR").map(PathBuf::from).unwrap();
Self {
src_dir: manifest_dir.join("src"),
src_dir: manifest_dir.join("src/include"),
shim_src: manifest_dir.join("cshim"),
shim_dst: out_dir.join("cshim"),
out_dir,
Expand Down
27 changes: 27 additions & 0 deletions pgrx-pg-sys/src/cshim.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#![cfg(feature = "cshim")]

use crate as pg_sys;
use core::ffi;

#[pgrx_macros::pg_guard]
extern "C" {
pub fn pgrx_list_nth(list: *mut pg_sys::List, nth: i32) -> *mut ffi::c_void;
pub fn pgrx_list_nth_int(list: *mut pg_sys::List, nth: i32) -> i32;
pub fn pgrx_list_nth_oid(list: *mut pg_sys::List, nth: i32) -> pg_sys::Oid;
pub fn pgrx_list_nth_cell(list: *mut pg_sys::List, nth: i32) -> *mut pg_sys::ListCell;

#[link_name = "pgrx_planner_rt_fetch"]
pub fn planner_rt_fetch(
index: pg_sys::Index,
root: *mut pg_sys::PlannerInfo,
) -> *mut pg_sys::RangeTblEntry;

#[link_name = "pgrx_SpinLockInit"]
pub fn SpinLockInit(lock: *mut pg_sys::slock_t);
#[link_name = "pgrx_SpinLockAcquire"]
pub fn SpinLockAcquire(lock: *mut pg_sys::slock_t);
#[link_name = "pgrx_SpinLockRelease"]
pub fn SpinLockRelease(lock: *mut pg_sys::slock_t);
#[link_name = "pgrx_SpinLockFree"]
pub fn SpinLockFree(lock: *mut pg_sys::slock_t) -> bool;
}
65 changes: 65 additions & 0 deletions pgrx-pg-sys/src/cstr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use core::ffi;

/// A trait for converting a thing into a `char *` that is allocated by Postgres' palloc
pub trait AsPgCStr {
/// Consumes `self` and converts it into a Postgres-allocated `char *`
fn as_pg_cstr(self) -> *mut ffi::c_char;
}

impl<'a> AsPgCStr for &'a str {
fn as_pg_cstr(self) -> *mut ffi::c_char {
let self_bytes = self.as_bytes();
let pg_cstr = unsafe { crate::palloc0(self_bytes.len() + 1) as *mut u8 };
let slice = unsafe { std::slice::from_raw_parts_mut(pg_cstr, self_bytes.len()) };
slice.copy_from_slice(self_bytes);
pg_cstr as *mut ffi::c_char
}
}

impl<'a> AsPgCStr for Option<&'a str> {
fn as_pg_cstr(self) -> *mut ffi::c_char {
match self {
Some(s) => s.as_pg_cstr(),
None => std::ptr::null_mut(),
}
}
}

impl AsPgCStr for String {
fn as_pg_cstr(self) -> *mut ffi::c_char {
self.as_str().as_pg_cstr()
}
}

impl AsPgCStr for &String {
fn as_pg_cstr(self) -> *mut ffi::c_char {
self.as_str().as_pg_cstr()
}
}

impl AsPgCStr for Option<String> {
fn as_pg_cstr(self) -> *mut ffi::c_char {
match self {
Some(s) => s.as_pg_cstr(),
None => std::ptr::null_mut(),
}
}
}

impl AsPgCStr for Option<&String> {
fn as_pg_cstr(self) -> *mut ffi::c_char {
match self {
Some(s) => s.as_pg_cstr(),
None => std::ptr::null_mut(),
}
}
}

impl AsPgCStr for &Option<String> {
fn as_pg_cstr(self) -> *mut ffi::c_char {
match self {
Some(s) => s.as_pg_cstr(),
None => std::ptr::null_mut(),
}
}
}
Loading

0 comments on commit c83c203

Please sign in to comment.