Skip to main content

pgrx_pg_sys/submodules/
utils.rs

1//LICENSE Portions Copyright 2019-2021 ZomboDB, LLC.
2//LICENSE
3//LICENSE Portions Copyright 2021-2023 Technology Concepts & Design, Inc.
4//LICENSE
5//LICENSE Portions Copyright 2023-2023 PgCentral Foundation, Inc. <contact@pgcentral.org>
6//LICENSE
7//LICENSE All rights reserved.
8//LICENSE
9//LICENSE Use of this source code is governed by the MIT license that can be found in the LICENSE file.
10//! General utility functions
11use crate as pg_sys;
12
13/// Converts a `pg_sys::NameData` struct into a `&str`.
14///
15/// This is a zero-copy operation and the returned `&str` is tied to the lifetime
16/// of the provided `pg_sys::NameData`
17#[inline]
18pub fn name_data_to_str(name_data: &pg_sys::NameData) -> &str {
19    fn transmute<const N: usize>(x: &[core::ffi::c_char; N]) -> &[core::ffi::c_uchar; N] {
20        unsafe { std::mem::transmute(x) }
21    }
22
23    core::ffi::CStr::from_bytes_until_nul(transmute(&name_data.data)).unwrap().to_str().unwrap()
24}