cstrptr/
ptr.rs

1#[cfg(all(not(feature = "std"), feature = "alloc"))]
2use alloc::borrow::Cow;
3#[cfg(feature = "std")]
4use std::borrow::Cow;
5use {
6    crate::{CStr, FromBytesWithNulError},
7    core::marker::PhantomData,
8    cty::c_char,
9};
10
11#[repr(transparent)]
12#[derive(Copy, Clone)]
13pub struct CStrPtr<'a> {
14    inner: *const c_char,
15    marker: PhantomData<&'a [c_char]>,
16}
17
18impl<'a> CStrPtr<'a> {
19    #[inline]
20    pub const fn from_c_str(str: &'a CStr) -> Self {
21        unsafe { Self::from_ptr(str.as_ptr()) }
22    }
23
24    #[inline]
25    pub const unsafe fn from_ptr(ptr: *const c_char) -> Self {
26        CStrPtr {
27            inner: ptr,
28            marker: PhantomData,
29        }
30    }
31
32    #[inline]
33    pub fn from_bytes_with_nul(bytes: &'a [u8]) -> Result<Self, FromBytesWithNulError> {
34        CStr::from_bytes_with_nul(bytes).map(From::from)
35    }
36
37    #[inline]
38    pub const unsafe fn from_bytes_with_nul_unchecked(bytes: &'a [u8]) -> Self {
39        Self::from_ptr(bytes.as_ptr() as *const _)
40    }
41
42    #[inline]
43    pub const fn as_ptr(self) -> *const c_char {
44        self.inner
45    }
46
47    #[inline]
48    pub fn to_bytes(self) -> &'a [u8] {
49        self.to_c_str().to_bytes()
50    }
51
52    pub fn to_bytes_with_nul(self) -> &'a [u8] {
53        self.to_c_str().to_bytes_with_nul()
54    }
55
56    #[inline]
57    pub fn to_c_str(self) -> &'a CStr {
58        unsafe { CStr::from_ptr(self.inner) }
59    }
60
61    #[inline]
62    pub fn to_str(self) -> Result<&'a str, core::str::Utf8Error> {
63        // TODO: inefficient
64        core::str::from_utf8(self.to_bytes())
65    }
66
67    #[inline]
68    #[cfg(any(feature = "alloc", feature = "std"))]
69    pub fn to_string_lossy(self) -> Cow<'a, str> {
70        self.to_c_str().to_string_lossy()
71    }
72}
73
74unsafe impl<'a> Send for CStrPtr<'a> {}
75unsafe impl<'a> Sync for CStrPtr<'a> {}
76
77impl<'a> PartialEq for CStrPtr<'a> {
78    #[inline]
79    fn eq(&self, other: &Self) -> bool {
80        self.to_bytes().eq(other.to_bytes())
81    }
82}
83
84impl<'a> Eq for CStrPtr<'a> {}
85
86impl<'a> PartialOrd for CStrPtr<'a> {
87    #[inline]
88    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
89        self.to_bytes().partial_cmp(&other.to_bytes())
90    }
91}
92
93impl<'a> Ord for CStrPtr<'a> {
94    #[inline]
95    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
96        self.to_bytes().cmp(&other.to_bytes())
97    }
98}
99
100impl<'a> AsRef<CStr> for CStrPtr<'a> {
101    // NOTE: this isn't really a cheap conversion...
102    #[inline]
103    fn as_ref(&self) -> &CStr {
104        self.to_c_str()
105    }
106}
107
108impl<'a> From<CStrPtr<'a>> for &'a CStr {
109    #[inline]
110    fn from(ptr: CStrPtr<'a>) -> Self {
111        ptr.to_c_str()
112    }
113}
114
115impl<'a> From<&'a CStr> for CStrPtr<'a> {
116    #[inline]
117    fn from(ptr: &'a CStr) -> Self {
118        Self::from_c_str(ptr)
119    }
120}