Skip to content

Commit

Permalink
Removed extra allocation from IntoDatum for char (#1887)
Browse files Browse the repository at this point in the history
  • Loading branch information
YohDeadfall authored Sep 27, 2024
1 parent ef4064e commit 1ccd02b
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions pgrx/src/datum/into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
///
Expand Down Expand Up @@ -297,7 +297,13 @@ impl IntoDatum for &String {
impl IntoDatum for char {
#[inline]
fn into_datum(self) -> Option<pg_sys::Datum> {
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 {
Expand Down

0 comments on commit 1ccd02b

Please sign in to comment.