Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed extra allocation from IntoDatum for char #1887

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading