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

Modularize the interior of pgrx-pg-sys #1227

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
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