From f70b053fe2b1d4be1a766defe86196b0b56a0daf Mon Sep 17 00:00:00 2001 From: usamoi Date: Mon, 1 Apr 2024 21:38:32 +0800 Subject: [PATCH] add ::new API for catalog wrappers Signed-off-by: usamoi --- pgrx/src/pg_catalog.rs | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/pgrx/src/pg_catalog.rs b/pgrx/src/pg_catalog.rs index 1485c44e6..c7fa12173 100644 --- a/pgrx/src/pg_catalog.rs +++ b/pgrx/src/pg_catalog.rs @@ -213,9 +213,14 @@ macro_rules! define_column { paste::paste! { impl [<$table:camel>]<'_> { $(#[$column_meta])* + /// # Panics + /// + /// This function panics if the catalog wrapper is created by `Self::new` but there is no `cache_id` provided. pub fn $column(&self) -> Option<$column_type> { - unsafe { - get_attr::<$column_type>(self.inner, self.cache_id, pg_sys::[]) + if let Some(cache_id) = self.cache_id { + unsafe { get_attr::<$column_type>(self.inner, cache_id, pg_sys::[]) } + } else { + panic!("`cache_id` is not provided") } } } @@ -227,10 +232,15 @@ macro_rules! define_column { paste::paste! { impl [<$table:camel>]<'_> { $(#[$column_meta])* + /// # Panics + /// + /// This function panics if the catalog wrapper is created by `Self::new` but there is no `cache_id` provided. pub fn $column(&self) -> $column_type { - unsafe { - get_attr::<$column_type>(self.inner, self.cache_id, pg_sys::[]) - }.unwrap() + if let Some(cache_id) = self.cache_id { + unsafe { get_attr::<$column_type>(self.inner, cache_id, pg_sys::[]).unwrap() } + } else { + panic!("`cache_id` is not provided") + } } } } @@ -247,7 +257,21 @@ macro_rules! define_catalog { pub struct [<$table:camel>]<'a> { inner: &'a pg_sys::HeapTupleData, #[allow(dead_code)] - cache_id: i32, + cache_id: Option, + } + + impl<'a> [<$table:camel>]<'a> { + /// Create a catalog wrapper by a heap tuple. + /// + /// # Safety + /// + /// If `cache_id` is provided, `cache_id` must be an ID for a `syscache` for this catalog. + pub unsafe fn new(inner: &'a pg_sys::HeapTupleData, cache_id: Option) -> Self { + Self { + inner, + cache_id, + } + } } #[doc = concat!("The search result for pg_catalog.", stringify!($table), ".")] @@ -266,7 +290,7 @@ macro_rules! define_catalog { unsafe { Some([<$table:camel>] { inner: self.inner?.as_ref(), - cache_id: self.cache_id, + cache_id: Some(self.cache_id), }) } } @@ -304,7 +328,7 @@ macro_rules! define_catalog { unsafe { Some([<$table:camel>] { inner: syscache_get(self.inner, i)?, - cache_id: self.cache_id + cache_id: Some(self.cache_id) }) } }