pub struct PgVarlena<T>where
T: Copy + Sized,{ /* private fields */ }
Expand description
Wraps a Postgres varlena *
, presenting it as if it’s a Rust type of a fixed size.
The wrapped varlena *
is behind a Rust alloc::borrow:Cow
which ensures that in the
common-use case of creating a PgVarlena
from a Postgres-provided Datum
, it’s not
possible to scribble on that Postgres-allocated memory.
Generally, PgVarlena
is meant to be used in conjunction with pgx’s PostgresType
derive macro
to provide transparent mapping of fixed-size Rust types as Postgres datums.
Example
use std::str::FromStr;
use pgx::prelude::*;
#[derive(Copy, Clone, PostgresType)]
#[pgvarlena_inoutfuncs]
struct MyType {
a: f32,
b: f32,
c: i64
}
impl PgVarlenaInOutFuncs for MyType {
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());
let mut result = PgVarlena::<MyType>::new();
result.a = f32::from_str(a.unwrap()).expect("a is not a valid f32");
result.b = f32::from_str(b.unwrap()).expect("b is not a valid f32");
result.c = i64::from_str(c.unwrap()).expect("c is not a valid i64");
result
}
fn output(&self, buffer: &mut pgx::StringInfo) {
buffer.push_str(&format!("{},{},{}", self.a, self.b, self.c));
}
}
#[pg_extern]
fn do_a_thing(mut input: PgVarlena<MyType>) -> PgVarlena<MyType> {
input.c += 99; // performs a copy-on-write
input
}
Implementations§
source§impl<T> PgVarlena<T>where
T: Copy + Sized,
impl<T> PgVarlena<T>where T: Copy + Sized,
sourcepub fn new() -> Self
pub fn new() -> Self
Create a new PgVarlena
representing a Rust type. The backing varlena is allocated
by Postgres and initially zero’d (using pg_sys::palloc0
). Unless .into_pg()
is called,
the Postgres-allocated memory will follow Rust’s drop semantics.
Example
use pgx::prelude::*;
#[derive(Copy, Clone)]
struct MyType {
a: f32,
b: f32,
c: i64
}
let mut v = PgVarlena::<MyType>::new();
v.a = 42.0;
v.b = 0.424242;
v.c = 42;
sourcepub unsafe fn from_datum(datum: Datum) -> Self
pub unsafe fn from_datum(datum: Datum) -> Self
Construct a PgVarlena
from a known-to-be-non-null pg_sys::Datum
. As
FromDatum for PgVarlena<T> where T: Copy + Sized
is already implemented, it is unlikely
that this function will need to be called directly.
The provided datum is automatically detoasted and the returned PgVarlena
will either
be considered borrowed or owned based on if detoasting actually needed to allocate memory.
If it didn’t, then we’re borrowed, otherwise we’re owned.
Safety
This function is considered unsafe as it cannot guarantee the provided pg_sys::Datum
is a
valid *mut pg_sys::varlena
.
Trait Implementations§
source§impl<T> AsMut<T> for PgVarlena<T>where
T: Copy + Sized,
impl<T> AsMut<T> for PgVarlena<T>where T: Copy + Sized,
Does a copy-on-write if the backing varlena pointer is borrowed
source§impl<T> Drop for PgVarlena<T>where
T: Copy + Sized,
impl<T> Drop for PgVarlena<T>where T: Copy + Sized,
pg_sys::pfree
a PgVarlena
if we allocated it, instead of Postgres
source§impl<T> FromDatum for PgVarlena<T>where
T: Copy + Sized,
impl<T> FromDatum for PgVarlena<T>where T: Copy + Sized,
source§unsafe fn from_polymorphic_datum(
datum: Datum,
is_null: bool,
_typoid: Oid
) -> Option<Self>
unsafe fn from_polymorphic_datum( datum: Datum, is_null: bool, _typoid: Oid ) -> Option<Self>
from_datum
for instantiating polymorphic types
which require preserving the dynamic type metadata. Read moresource§unsafe fn from_datum_in_memory_context(
memory_context: PgMemoryContexts,
datum: Datum,
is_null: bool,
_typoid: Oid
) -> Option<Self>
unsafe fn from_datum_in_memory_context( memory_context: PgMemoryContexts, datum: Datum, is_null: bool, _typoid: Oid ) -> Option<Self>
FromDatum::from_datum(...)
from within that context. Read moresource§const GET_TYPOID: bool = false
const GET_TYPOID: bool = false
from_datum
?source§unsafe fn from_datum(datum: Datum, is_null: bool) -> Option<Self>where
Self: Sized,
unsafe fn from_datum(datum: Datum, is_null: bool) -> Option<Self>where Self: Sized,
source§unsafe fn try_from_datum(
datum: Datum,
is_null: bool,
type_oid: Oid
) -> Result<Option<Self>, TryFromDatumError>where
Self: Sized + IntoDatum,
unsafe fn try_from_datum( datum: Datum, is_null: bool, type_oid: Oid ) -> Result<Option<Self>, TryFromDatumError>where Self: Sized + IntoDatum,
try_from_datum
is a convenience wrapper around FromDatum::from_datum
that returns a
a Result
around an Option
, as a Datum can be null. It’s intended to be used in
situations where the caller needs to know whether the type conversion succeeded or failed. Read moresource§unsafe fn try_from_datum_in_memory_context(
memory_context: PgMemoryContexts,
datum: Datum,
is_null: bool,
type_oid: Oid
) -> Result<Option<Self>, TryFromDatumError>where
Self: Sized + IntoDatum,
unsafe fn try_from_datum_in_memory_context( memory_context: PgMemoryContexts, datum: Datum, is_null: bool, type_oid: Oid ) -> Result<Option<Self>, TryFromDatumError>where Self: Sized + IntoDatum,
try_from_datum
that switches to the given context to convert from Datumsource§impl<T> SqlTranslatable for PgVarlena<T>where
T: SqlTranslatable + Copy,
impl<T> SqlTranslatable for PgVarlena<T>where T: SqlTranslatable + Copy,
fn argument_sql() -> Result<SqlMapping, ArgumentError>
fn return_sql() -> Result<Returns, ReturnsError>
fn type_name() -> &'static str
fn variadic() -> bool
fn optional() -> bool
fn entity() -> FunctionMetadataTypeEntity
Auto Trait Implementations§
impl<T> RefUnwindSafe for PgVarlena<T>where T: RefUnwindSafe,
impl<T> !Send for PgVarlena<T>
impl<T> !Sync for PgVarlena<T>
impl<T> Unpin for PgVarlena<T>where T: Unpin,
impl<T> UnwindSafe for PgVarlena<T>where T: UnwindSafe,
Blanket Implementations§
source§impl<A, T> AsBits<T> for Awhere
A: AsRef<[T]>,
T: BitStore,
impl<A, T> AsBits<T> for Awhere A: AsRef<[T]>, T: BitStore,
source§impl<A, T> AsMutBits<T> for Awhere
A: AsMut<[T]>,
T: BitStore,
impl<A, T> AsMutBits<T> for Awhere A: AsMut<[T]>, T: BitStore,
source§fn as_mut_bits<O>(&mut self) -> &mut BitSlice<T, O> ⓘwhere
O: BitOrder,
fn as_mut_bits<O>(&mut self) -> &mut BitSlice<T, O> ⓘwhere O: BitOrder,
self
as a mutable bit-slice region with the O
ordering.source§fn try_as_mut_bits<O>(&mut self) -> Result<&mut BitSlice<T, O>, BitSpanError<T>>where
O: BitOrder,
fn try_as_mut_bits<O>(&mut self) -> Result<&mut BitSlice<T, O>, BitSpanError<T>>where O: BitOrder,
§impl<T> Conv for T
impl<T> Conv for T
§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where Self: Display,
self
to use its Display
implementation when
Debug
-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where &'a Self: for<'a> IntoIterator,
§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere Self: Borrow<B>, B: 'a + ?Sized, R: 'a,
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> Rwhere
Self: BorrowMut<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R ) -> Rwhere Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere Self: AsRef<U>, U: 'a + ?Sized, R: 'a,
self
, then passes self.as_ref()
into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere
Self: AsMut<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere Self: AsMut<U>, U: 'a + ?Sized, R: 'a,
self
, then passes self.as_mut()
into the pipe
function.§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere Self: Borrow<B>, B: ?Sized,
Borrow<B>
of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere Self: BorrowMut<B>, B: ?Sized,
BorrowMut<B>
of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere Self: AsRef<R>, R: ?Sized,
AsRef<R>
view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere Self: AsMut<R>, R: ?Sized,
AsMut<R>
view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere
Self: Deref<Target = T>,
T: ?Sized,
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere Self: Deref<Target = T>, T: ?Sized,
Deref::Target
of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere Self: DerefMut<Target = T> + Deref, T: ?Sized,
Deref::Target
of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere Self: Borrow<B>, B: ?Sized,
.tap_borrow()
only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere Self: BorrowMut<B>, B: ?Sized,
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere Self: AsRef<R>, R: ?Sized,
.tap_ref()
only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere Self: AsMut<R>, R: ?Sized,
.tap_ref_mut()
only in debug builds, and is erased in release
builds.