1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// SPDX-License-Identifier: Apache-2.0

#![allow(
    non_camel_case_types,
    non_snake_case,
    clippy::missing_safety_doc,
    clippy::too_many_arguments,
    clippy::type_complexity,
    clippy::upper_case_acronyms
)]

use alloc::borrow::Cow;
use alloc::string::String;
use core::ffi::{c_char, CStr};
use core::fmt;
use core::hash;
use core::ops;

/// An array containing a sequence of bytes.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct ByteArray<const N: usize>(pub [u8; N]);

impl<const N: usize> Default for ByteArray<N> {
    #[inline]
    fn default() -> Self {
        Self([0; N])
    }
}

impl<const N: usize> ops::Deref for ByteArray<N> {
    type Target = [u8; N];

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<const N: usize> fmt::Debug for ByteArray<N> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "ByteArray<{}>({:?})", N, self.0)
    }
}

impl<const N: usize> fmt::Display for ByteArray<N> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self.0)
    }
}

impl<const N: usize> From<[u8; N]> for ByteArray<N> {
    #[inline]
    fn from(array: [u8; N]) -> Self {
        Self(array)
    }
}

impl<const N: usize> From<ByteArray<N>> for [u8; N] {
    #[inline]
    fn from(array: ByteArray<N>) -> Self {
        array.0
    }
}

/// An array containing a null-terminated string.
///
/// # Equality / Hashing
///
/// For the purposes of comparing and hashing array strings, any characters
/// after the first null terminator are ignored. The below example demonstrates
/// this property with two strings that differ in the characters that come
/// after the first null terminators.
///
/// ```
/// # use std::collections::hash_map::DefaultHasher;
/// # use std::hash::{Hash, Hasher};
/// # use vulkanalia_sys::StringArray;
/// let string1 = StringArray::<3>::new([0x61, 0, 0]);
/// let string2 = StringArray::<3>::new([0x61, 0, 0x61]);
///
/// assert_eq!(string1, string2);
///
/// let mut hasher1 = DefaultHasher::new();
/// string1.hash(&mut hasher1);
/// let mut hasher2 = DefaultHasher::new();
/// string2.hash(&mut hasher2);
///
/// assert_eq!(hasher1.finish(), hasher2.finish());
/// ```
#[derive(Copy, Clone, PartialOrd, Ord)]
#[repr(transparent)]
pub struct StringArray<const N: usize>([c_char; N]);

impl<const N: usize> StringArray<N> {
    /// Constructs a string array from a character array.
    ///
    /// # Panics
    ///
    /// * `characters` does not contain a null-terminator
    #[inline]
    pub fn new(array: [c_char; N]) -> Self {
        assert!(array.contains(&0));
        Self(array)
    }

    /// Constructs a string array from a byte string.
    ///
    /// If the byte string is longer than `N - 1`, then the byte string will
    /// be truncated to fit inside of the constructed string array (the last
    /// character is reserved for a null terminator). The constructed string
    /// array will always be null-terminated regardless if the byte string is
    /// or is not null-terminated.
    #[inline]
    pub const fn from_bytes(bytes: &[u8]) -> Self {
        let mut array = [0; N];

        let mut index = 0;
        while index < bytes.len() && index + 1 < N {
            if bytes[index] != 0 {
                array[index] = bytes[index] as c_char;
                index += 1;
            } else {
                break;
            }
        }

        Self(array)
    }

    /// Constructs a string array from a borrowed C string.
    ///
    /// If the borrowed C string is longer than `N - 1`, then the borrowed C
    /// string will be truncated to fit inside of the constructed string array
    /// (the last character is reserved for a null terminator).
    #[inline]
    pub fn from_cstr(cstr: &CStr) -> Self {
        Self::from_bytes(cstr.to_bytes())
    }

    /// Constructs a string array from a pointer to a null-terminated string.
    ///
    /// If the null-terminated string is longer than `N - 1`, then the
    /// null-terminated string will be truncated to fit inside of the
    /// constructed string array (the last character is reserved for a null
    /// terminator).
    ///
    /// # Safety
    ///
    /// * `ptr` must be a pointer to a null-terminated string
    #[inline]
    pub unsafe fn from_ptr(ptr: *const c_char) -> Self {
        Self::from_cstr(CStr::from_ptr(ptr))
    }

    /// Gets the underlying character array for this string array.
    #[inline]
    pub fn as_array(&self) -> &[c_char; N] {
        &self.0
    }

    /// Gets this string array as a slice of bytes.
    #[inline]
    pub fn as_bytes(&self) -> &[u8] {
        unsafe { self.as_array().align_to::<u8>().1 }
    }

    /// Gets this string array as a borrowed C string.
    #[inline]
    pub fn as_cstr(&self) -> &CStr {
        let bytes = self.as_bytes();
        let nul = bytes.iter().position(|b| *b == b'\0');
        let end = nul.unwrap_or(N - 1);
        unsafe { CStr::from_bytes_with_nul_unchecked(&bytes[..end + 1]) }
    }

    /// Converts this string array to a UTF-8 string (lossily).
    #[inline]
    pub fn to_string_lossy(&self) -> Cow<str> {
        let bytes = self.as_bytes();
        let nul = bytes.iter().position(|b| *b == b'\0');
        let end = nul.unwrap_or(N);
        String::from_utf8_lossy(&bytes[..end])
    }
}

impl<const N: usize> Default for StringArray<N> {
    #[inline]
    fn default() -> Self {
        Self([0; N])
    }
}

impl<const N: usize> PartialEq for StringArray<N> {
    fn eq(&self, other: &Self) -> bool {
        self.as_cstr() == other.as_cstr()
    }
}

impl<const N: usize> Eq for StringArray<N> {}

impl<const N: usize> hash::Hash for StringArray<N> {
    fn hash<H: hash::Hasher>(&self, hasher: &mut H) {
        self.as_cstr().hash(hasher);
    }
}

impl<const N: usize> ops::Deref for StringArray<N> {
    type Target = [c_char; N];

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<const N: usize> fmt::Debug for StringArray<N> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "StringArray<{}>({:?})", N, self.to_string_lossy())
    }
}

impl<const N: usize> fmt::Display for StringArray<N> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.to_string_lossy())
    }
}

impl<const N: usize> From<[c_char; N]> for StringArray<N> {
    #[inline]
    fn from(array: [c_char; N]) -> Self {
        Self(array)
    }
}

impl<const N: usize> From<StringArray<N>> for [c_char; N] {
    #[inline]
    fn from(array: StringArray<N>) -> Self {
        array.0
    }
}

#[cfg(test)]
mod test {
    use super::*;

    use std::collections::hash_map::DefaultHasher;
    use std::hash::{Hash, Hasher};

    fn hash(hash: impl Hash) -> u64 {
        let mut hasher = DefaultHasher::new();
        hash.hash(&mut hasher);
        hasher.finish()
    }

    #[test]
    fn test_string_array_from_bytes() {
        type S1 = StringArray<1>;

        assert_eq!(b"\0", S1::from_bytes(b"").as_bytes());
        assert_eq!(b"\0", S1::from_bytes(b"\0").as_bytes());
        assert_eq!(b"\0", S1::from_bytes(b"\0bar").as_bytes());

        assert_eq!(b"\0", S1::from_bytes(b"322").as_bytes());
        assert_eq!(b"\0", S1::from_bytes(b"322\0").as_bytes());
        assert_eq!(b"\0", S1::from_bytes(b"322\0bar").as_bytes());

        type S4 = StringArray<4>;

        assert_eq!(b"\0\0\0\0", S4::from_bytes(b"").as_bytes());
        assert_eq!(b"\0\0\0\0", S4::from_bytes(b"\0").as_bytes());
        assert_eq!(b"\0\0\0\0", S4::from_bytes(b"\0bar").as_bytes());

        assert_eq!(b"322\0", S4::from_bytes(b"322").as_bytes());
        assert_eq!(b"322\0", S4::from_bytes(b"322\0").as_bytes());
        assert_eq!(b"322\0", S4::from_bytes(b"322\0bar").as_bytes());

        assert_eq!(b"128\0", S4::from_bytes(b"1288").as_bytes());
        assert_eq!(b"128\0", S4::from_bytes(b"1288\0").as_bytes());
        assert_eq!(b"128\0", S4::from_bytes(b"1288\0bar").as_bytes());
    }

    #[test]
    fn test_string_array_cmp() {
        macro_rules! assert_cmp_eq {
            ($left:expr, $right:expr) => {
                assert_eq!($left, $right);
                assert_eq!(hash($left), hash($right));
            };
        }

        macro_rules! assert_cmp_ne {
            ($left:expr, $right:expr) => {
                assert_ne!($left, $right);
                assert_ne!(hash($left), hash($right));
            };
        }

        type S32 = StringArray<32>;

        assert_cmp_eq!(S32::from_bytes(b""), S32::from_bytes(b""));
        assert_cmp_eq!(S32::from_bytes(b"\0"), S32::from_bytes(b""));
        assert_cmp_eq!(S32::from_bytes(b""), S32::from_bytes(b"\0"));
        assert_cmp_eq!(S32::from_bytes(b"\0"), S32::from_bytes(b"\0"));
        assert_cmp_eq!(S32::from_bytes(b"\0foo"), S32::from_bytes(b"\0bar"));

        assert_cmp_eq!(S32::from_bytes(b"322"), S32::from_bytes(b"322"));
        assert_cmp_eq!(S32::from_bytes(b"322\0"), S32::from_bytes(b"322"));
        assert_cmp_eq!(S32::from_bytes(b"322"), S32::from_bytes(b"322\0"));
        assert_cmp_eq!(S32::from_bytes(b"322\0"), S32::from_bytes(b"322\0"));
        assert_cmp_eq!(S32::from_bytes(b"322\0foo"), S32::from_bytes(b"322\0bar"));

        assert_cmp_ne!(S32::from_bytes(b"322"), S32::from_bytes(b"422"));
        assert_cmp_ne!(S32::from_bytes(b"322"), S32::from_bytes(b"332"));
        assert_cmp_ne!(S32::from_bytes(b"322"), S32::from_bytes(b"323"));

        assert_cmp_ne!(S32::from_bytes(b"322"), S32::from_bytes(b"32"));
        assert_cmp_ne!(S32::from_bytes(b"322"), S32::from_bytes(b"3222"));
    }
}