fontconfig/
strings.rs

1//! To help applications deal with these UTF-8 strings in a locale-insensitive manner.
2
3use std::ffi::CStr;
4use std::fmt;
5use std::ops::Deref;
6use std::ptr::NonNull;
7
8use fontconfig_sys as sys;
9
10#[cfg(feature = "dlopen")]
11use sys::statics::LIB;
12#[cfg(not(feature = "dlopen"))]
13use sys::*;
14
15use sys::ffi_dispatch;
16
17/// C String
18#[repr(transparent)]
19pub struct FcStr(NonNull<sys::FcChar8>);
20
21/// Represented with the FcChar8 type with ownership.
22impl FcStr {
23    pub(crate) unsafe fn from_ptr(ptr: *mut sys::FcChar8) -> Option<Self> {
24        NonNull::new(ptr).map(FcStr)
25    }
26}
27
28impl Drop for FcStr {
29    fn drop(&mut self) {
30        unsafe { ffi_dispatch!(LIB, FcStrFree, self.0.as_ptr()) }
31    }
32}
33
34impl Deref for FcStr {
35    type Target = CStr;
36
37    fn deref(&self) -> &Self::Target {
38        unsafe { CStr::from_ptr(self.0.as_ptr() as *const _) }
39    }
40}
41
42impl AsRef<CStr> for FcStr {
43    fn as_ref(&self) -> &CStr {
44        self.deref()
45    }
46}
47
48impl fmt::Debug for FcStr {
49    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
50        self.deref().fmt(f)
51    }
52}