|
1 | 1 | use core::{mem::MaybeUninit, ptr, slice}; |
2 | 2 |
|
3 | 3 | use allocator_api2::{alloc::Allocator, boxed::Box, vec::Vec}; |
4 | | -use fspy_nostd::{CStr, Fat, Thin}; |
| 4 | +use fspy_nostd::{CStr, CStrUnit, Fat, Thin}; |
5 | 5 |
|
6 | 6 | /// An allocator-backed owned C string. |
7 | | -pub struct CString<R, A: Allocator> { |
8 | | - bytes: Box<[MaybeUninit<u8>], A>, |
| 7 | +pub struct CString<R, A: Allocator, U: CStrUnit = u8> { |
| 8 | + units: Box<[MaybeUninit<U>], A>, |
9 | 9 | repr: R, |
10 | 10 | } |
11 | 11 |
|
12 | | -impl<A: Allocator> CString<Fat, A> { |
13 | | - /// Converts a byte vector to a C string without checking its contents. |
| 12 | +/// An allocator-backed owned C string of the platform's native path code |
| 13 | +/// units: bytes on Unix and wide (`u16`) code units on Windows. |
| 14 | +#[cfg(unix)] |
| 15 | +pub type OsCString<R, A> = CString<R, A>; |
| 16 | +/// An allocator-backed owned C string of the platform's native path code |
| 17 | +/// units: bytes on Unix and wide (`u16`) code units on Windows. |
| 18 | +#[cfg(windows)] |
| 19 | +pub type OsCString<R, A> = CString<R, A, u16>; |
| 20 | + |
| 21 | +impl<A: Allocator, U: CStrUnit> CString<Fat, A, U> { |
| 22 | + /// Converts a code-unit vector that ends with the single NUL terminator |
| 23 | + /// to a C string. |
| 24 | + /// |
| 25 | + /// Returns [`None`] when `units` is empty, does not end with a NUL code |
| 26 | + /// unit, or contains an interior NUL code unit. |
| 27 | + #[must_use] |
| 28 | + pub fn from_vec_with_nul(units: Vec<U, A>) -> Option<Self> { |
| 29 | + CStr::<Fat, U>::from_units_with_nul(&units)?; |
| 30 | + // SAFETY: validated just above. |
| 31 | + Some(unsafe { Self::from_vec_with_nul_unchecked(units) }) |
| 32 | + } |
| 33 | + |
| 34 | + /// Converts a code-unit vector to a C string without checking its |
| 35 | + /// contents. |
14 | 36 | /// |
15 | 37 | /// # Safety |
16 | 38 | /// |
17 | | - /// `bytes` must end with exactly one NUL byte and contain no other NUL |
18 | | - /// bytes. |
| 39 | + /// `units` must end with exactly one NUL code unit and contain no other |
| 40 | + /// NUL code units. |
19 | 41 | #[must_use] |
20 | | - pub unsafe fn from_vec_with_nul_unchecked(bytes: Vec<u8, A>) -> Self { |
| 42 | + pub unsafe fn from_vec_with_nul_unchecked(units: Vec<U, A>) -> Self { |
21 | 43 | // SAFETY: upheld by the caller. |
22 | | - let repr = unsafe { CStr::<Fat>::from_units_with_nul_unchecked(&bytes) }.into_repr(); |
23 | | - let (bytes, _len, capacity, allocator) = bytes.into_raw_parts_with_alloc(); |
24 | | - let bytes = ptr::slice_from_raw_parts_mut(bytes.cast::<MaybeUninit<u8>>(), capacity); |
| 44 | + let repr = unsafe { CStr::<Fat, U>::from_units_with_nul_unchecked(&units) }.into_repr(); |
| 45 | + let (units, _len, capacity, allocator) = units.into_raw_parts_with_alloc(); |
| 46 | + let units = ptr::slice_from_raw_parts_mut(units.cast::<MaybeUninit<U>>(), capacity); |
25 | 47 |
|
26 | 48 | Self { |
27 | 49 | // SAFETY: this uses the vector's original pointer, capacity, and |
28 | 50 | // allocator. Its initialized prefix is described by `repr`; any |
29 | | - // spare capacity is valid as `MaybeUninit<u8>`. |
30 | | - bytes: unsafe { Box::from_raw_in(bytes, allocator) }, |
| 51 | + // spare capacity is valid as `MaybeUninit<U>`. |
| 52 | + units: unsafe { Box::from_raw_in(units, allocator) }, |
31 | 53 | repr, |
32 | 54 | } |
33 | 55 | } |
34 | 56 | } |
35 | 57 |
|
36 | | -impl<A: Allocator> CString<Thin, A> { |
| 58 | +impl<A: Allocator, U: CStrUnit> CString<Thin, A, U> { |
37 | 59 | /// Converts boxed storage to a thin C string without checking its |
38 | 60 | /// contents. |
39 | 61 | /// |
40 | 62 | /// # Safety |
41 | 63 | /// |
42 | | - /// `bytes` must begin with a valid NUL-terminated C string. Bytes after |
43 | | - /// that string may be uninitialized. |
| 64 | + /// `units` must begin with a valid NUL-terminated C string. Code units |
| 65 | + /// after that string may be uninitialized. |
44 | 66 | #[must_use] |
45 | | - pub unsafe fn from_boxed_with_nul_unchecked(bytes: Box<[MaybeUninit<u8>], A>) -> Self { |
| 67 | + pub unsafe fn from_boxed_with_nul_unchecked(units: Box<[MaybeUninit<U>], A>) -> Self { |
46 | 68 | // SAFETY: upheld by the caller. Creating a thin view does not scan the |
47 | 69 | // allocation or retain the string length. |
48 | | - let repr = unsafe { CStr::<Thin>::from_ptr(bytes.as_ptr().cast()) }.into_repr(); |
49 | | - Self { bytes, repr } |
| 70 | + let repr = unsafe { CStr::<Thin, U>::from_ptr(units.as_ptr().cast()) }.into_repr(); |
| 71 | + Self { units, repr } |
50 | 72 | } |
51 | 73 |
|
52 | 74 | /// Returns a thin borrowed view of this C string. |
53 | 75 | #[must_use] |
54 | | - pub fn as_c_str(&self) -> CStr<'_, Thin> { |
| 76 | + pub fn as_c_str(&self) -> CStr<'_, Thin, U> { |
55 | 77 | // SAFETY: upheld by the constructor; `self` owns the storage. |
56 | | - unsafe { CStr::from_ptr(self.bytes.as_ptr().cast()) } |
| 78 | + unsafe { CStr::from_ptr(self.units.as_ptr().cast()) } |
57 | 79 | } |
58 | 80 |
|
59 | 81 | /// Counts through the terminating NUL and returns a length-retaining C |
60 | 82 | /// string using the same allocation. |
61 | 83 | #[must_use] |
62 | | - pub fn count(self) -> CString<Fat, A> { |
| 84 | + pub fn count(self) -> CString<Fat, A, U> { |
63 | 85 | let repr = self.as_c_str().count().into_repr(); |
64 | | - CString { bytes: self.bytes, repr } |
| 86 | + CString { units: self.units, repr } |
65 | 87 | } |
66 | 88 | } |
67 | 89 |
|
68 | | -impl<A: Allocator> CString<Fat, A> { |
| 90 | +impl<A: Allocator, U: CStrUnit> CString<Fat, A, U> { |
69 | 91 | /// Extracts a borrowed C string view containing the entire string. |
70 | 92 | #[must_use] |
71 | | - pub fn as_c_str(&self) -> CStr<'_, Fat> { |
| 93 | + pub fn as_c_str(&self) -> CStr<'_, Fat, U> { |
72 | 94 | // SAFETY: the initialized prefix described by `repr` is a C string. |
73 | | - unsafe { CStr::from_units_with_nul_unchecked(self.as_bytes_with_nul()) } |
| 95 | + unsafe { CStr::from_units_with_nul_unchecked(self.as_units_with_nul()) } |
74 | 96 | } |
75 | 97 |
|
76 | 98 | /// Returns the contents of this C string without the terminating NUL. |
77 | 99 | #[must_use] |
78 | | - pub fn as_bytes(&self) -> &[u8] { |
79 | | - let bytes = self.as_bytes_with_nul(); |
80 | | - bytes.split_at(bytes.len() - 1).0 |
| 100 | + pub fn as_units(&self) -> &[U] { |
| 101 | + let units = self.as_units_with_nul(); |
| 102 | + units.split_at(units.len() - 1).0 |
81 | 103 | } |
82 | 104 |
|
83 | 105 | /// Returns the contents of this C string, including the terminating NUL. |
84 | 106 | #[must_use] |
85 | | - pub fn as_bytes_with_nul(&self) -> &[u8] { |
| 107 | + pub fn as_units_with_nul(&self) -> &[U] { |
86 | 108 | // SAFETY: `repr` describes the initialized C string prefix. |
87 | | - unsafe { slice::from_raw_parts(self.bytes.as_ptr().cast(), self.repr.len_with_nul()) } |
| 109 | + unsafe { slice::from_raw_parts(self.units.as_ptr().cast(), self.repr.len_with_nul()) } |
88 | 110 | } |
89 | 111 |
|
90 | | - /// Consumes this C string and returns its bytes without the terminating NUL. |
| 112 | + /// Consumes this C string and returns its code units without the |
| 113 | + /// terminating NUL. |
91 | 114 | #[must_use = "`self` will be dropped if the result is not used"] |
92 | | - pub fn into_bytes(self) -> Vec<u8, A> { |
| 115 | + pub fn into_units(self) -> Vec<U, A> { |
93 | 116 | let len = self.repr.len_with_nul() - 1; |
94 | 117 | self.into_vec(len) |
95 | 118 | } |
96 | 119 |
|
97 | | - /// Consumes this C string and returns its bytes, including the terminating |
98 | | - /// NUL. |
| 120 | + /// Consumes this C string and returns its code units, including the |
| 121 | + /// terminating NUL. |
99 | 122 | #[must_use = "`self` will be dropped if the result is not used"] |
100 | | - pub fn into_bytes_with_nul(self) -> Vec<u8, A> { |
| 123 | + pub fn into_units_with_nul(self) -> Vec<U, A> { |
101 | 124 | let len = self.repr.len_with_nul(); |
102 | 125 | self.into_vec(len) |
103 | 126 | } |
104 | 127 |
|
105 | | - fn into_vec(self, len: usize) -> Vec<u8, A> { |
106 | | - let Self { bytes, .. } = self; |
107 | | - let capacity = bytes.len(); |
108 | | - let (bytes, allocator) = Box::into_raw_with_allocator(bytes); |
| 128 | + fn into_vec(self, len: usize) -> Vec<U, A> { |
| 129 | + let Self { units, .. } = self; |
| 130 | + let capacity = units.len(); |
| 131 | + let (units, allocator) = Box::into_raw_with_allocator(units); |
109 | 132 |
|
110 | | - // SAFETY: the box was allocated for `capacity` bytes with `allocator`. |
111 | | - // The first `len` bytes are initialized. |
112 | | - unsafe { Vec::from_raw_parts_in(bytes.cast::<u8>(), len, capacity, allocator) } |
| 133 | + // SAFETY: the box was allocated for `capacity` units with `allocator`. |
| 134 | + // The first `len` units are initialized. |
| 135 | + unsafe { Vec::from_raw_parts_in(units.cast::<U>(), len, capacity, allocator) } |
113 | 136 | } |
114 | 137 | } |
0 commit comments