-
-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modularize the interior of pgrx-pg-sys (#1227)
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
1 parent
bbcbad1
commit c83c203
Showing
20 changed files
with
781 additions
and
800 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
} | ||
} | ||
} |
Oops, something went wrong.