From 2ad2099b5907b32c07a85f3434adcf314ff565c2 Mon Sep 17 00:00:00 2001 From: Yoh Deadfall Date: Fri, 27 Sep 2024 17:46:12 +0300 Subject: [PATCH] Made IntoDatum for char non-allocating --- pgrx/src/datum/into.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pgrx/src/datum/into.rs b/pgrx/src/datum/into.rs index c2f841e95b..c1ae7a755f 100644 --- a/pgrx/src/datum/into.rs +++ b/pgrx/src/datum/into.rs @@ -15,7 +15,7 @@ use crate::{pg_sys, rust_regtypein, set_varsize_4b, PgBox, PgOid, WhoAllocated}; use core::fmt::Display; use pgrx_pg_sys::panic::ErrorReportable; -use std::{any::Any, ptr::addr_of_mut}; +use std::{any::Any, ptr::addr_of_mut, str}; /// Convert a Rust type into a `pg_sys::Datum`. /// @@ -297,7 +297,13 @@ impl IntoDatum for &String { impl IntoDatum for char { #[inline] fn into_datum(self) -> Option { - self.to_string().into_datum() + let mut buf = [0; 4]; + self.encode_utf8(&mut buf); + unsafe { + // SAFETY: The buffer contains only valid UTF8 data + // coming from the encode_utf8 method used above. + str::from_utf8_unchecked(&buf).into_datum() + } } fn type_oid() -> pg_sys::Oid {