xuko-core 0.6.1

Core routines, and data structures for xuko
Documentation
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#![expect(
    unsafe_op_in_unsafe_fn,
    reason = "Array requires unsafe code in some places"
)]

//! Variable length array

use std::alloc;
use std::ops::{Deref, DerefMut, Index, IndexMut};

/// An error that can occur when creating an [`Array`]
#[derive(Debug, thiserror::Error)]
pub enum ArrayCreationError {
    /// Occurs when the allocation fails
    #[error("allocation error: {0}")]
    AllocationError(String),

    /// Occurs on [`alloc::LayoutError`]
    #[error("layout error: {0}")]
    LayoutError(#[from] alloc::LayoutError),
}

/// Variable length array.
///
/// The purpose of this data structure is to provide an alternative to [`Vec`] with a static length.
/// Use a normal array if you know the length at compile time.
///
/// A possible use for [`Array`] is where you need to read from a [`std::io::Read`]er to a [`Vec`] using
/// the [`std::io::Read::read`] function, this wouldn't work because some elements of a [`Vec`] may be
/// uninitialized whereas [`Array`] functions just like a static compile-time known array.
///
/// # Examples
///
/// ```
/// use xuko_core::array::Array;
///
/// let mut array = Array::new(5).expect("Failed to create array");
///
/// array[0] = 1;
/// array[1] = 2;
/// array[2] = 3;
/// array[3] = 4;
/// array[4] = 5;
/// ```
pub struct Array<T> {
    ptr: *mut T,
    size: usize,
}

impl<T> Array<T> {
    /// Allocate an [`Array`]
    ///
    /// Returns an [`ArrayCreationError`] when the allocation fails.
    pub fn new(size: usize) -> Result<Self, ArrayCreationError> {
        unsafe {
            // TODO: Should this just panic on failed memory allocation?
            // The chances of that happening are low anyways.
            // Well if you want robust code it'd be annoying if this has the possibility of panicing.
            let layout = alloc::Layout::array::<T>(size)?;
            let ptr = alloc::alloc(layout) as *mut T;

            if ptr.is_null() {
                return Err(ArrayCreationError::AllocationError(
                    "null pointer".to_owned(),
                ));
            }

            Ok(Self { ptr, size })
        }
    }

    /// Get the size of the array
    #[must_use]
    pub const fn size(&self) -> usize {
        self.size
    }

    /// Return the array as an immutable pointer
    #[must_use]
    pub const fn as_ptr(&self) -> *const T {
        self.ptr
    }

    /// Return the array as a mutable pointer
    #[must_use]
    pub const fn as_mut_ptr(&self) -> *mut T {
        self.ptr
    }

    /// Set value at `index` to `value`
    ///
    /// # Panics
    ///
    /// Panics when `index` is out of bounds
    pub fn set(&mut self, index: usize, value: T) {
        if index >= self.size {
            panic!("index out of bounds");
        }

        // SAFETY: it is ensured that whatever this pointer points to is inside of the `Array` by
        // the previous statement
        unsafe { *(self.ptr.add(index)) = value }
    }

    /// Get value at index `index`
    #[must_use]
    pub const fn get(&self, index: usize) -> Option<&T> {
        if index >= self.size {
            return None;
        }

        // SAFETY: it is ensured that whatever this pointer points to is inside of the `Array` by
        // the previous statement
        unsafe { Some(&(*(self.ptr.add(index)))) }
    }

    /// Get value at index `index`
    #[must_use]
    pub fn get_mut(&mut self, index: usize) -> Option<&mut T> {
        if index >= self.size {
            return None;
        }

        // SAFETY: it is ensured that whatever this pointer points to is inside of the `Array` by
        // the previous statement
        unsafe { Some(&mut (*(self.ptr.add(index)))) }
    }

    /// Returns an immutable pointer to value at `index`.
    ///
    /// # Safety
    ///
    /// The pointer is obtained using unchecked pointer arithmetic.
    #[must_use]
    pub const unsafe fn get_ptr(&self, index: usize) -> *const T {
        self.ptr.add(index) as *const T
    }

    /// Returns a mutable pointer to value at `index`.
    ///
    /// # Safety
    ///
    /// The pointer is obtained using unchecked pointer arithmetic.
    #[must_use]
    pub unsafe fn get_ptr_mut(&mut self, index: usize) -> *mut T {
        self.ptr.add(index)
    }

    /// Return an iterator over the array
    #[must_use]
    pub fn iter(&self) -> iter::Iter<'_, T> {
        iter::Iter::new(self)
    }

    /// Return a mutable iterator over the array
    #[must_use]
    pub fn iter_mut(&mut self) -> iter::IterMut<'_, T> {
        iter::IterMut::new(self)
    }
}

impl<T> Drop for Array<T> {
    fn drop(&mut self) {
        unsafe {
            self.ptr.drop_in_place();
        }
    }
}

impl<T> Deref for Array<T> {
    type Target = [T];

    fn deref(&self) -> &Self::Target {
        // SAFETY: the length is known in the struct so this wont result in UB
        unsafe { std::slice::from_raw_parts(self.ptr, self.size) }
    }
}

impl<T> DerefMut for Array<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        // SAFETY: the length is known in the struct so this wont result in UB
        unsafe { std::slice::from_raw_parts_mut(self.ptr, self.size) }
    }
}

impl<T> AsRef<[T]> for Array<T> {
    fn as_ref(&self) -> &[T] {
        self
    }
}

impl<T> AsMut<[T]> for Array<T> {
    fn as_mut(&mut self) -> &mut [T] {
        &mut *self
    }
}

/// [`std::io::Write`] is implemented for any [`Array<u8>`]
impl std::io::Write for Array<u8> {
    // TODO: is this implementation even good?
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        let mut index = 0;
        for b in buf {
            self[index] = *b;
            index += 1;
        }
        Ok(index + 1)
    }

    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}

impl<T> Index<usize> for Array<T> {
    type Output = T;

    fn index(&self, index: usize) -> &Self::Output {
        self.get(index).expect("index out of bounds")
    }
}

impl<T> IndexMut<usize> for Array<T> {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        self.get_mut(index).expect("index out of bounds")
    }
}

impl<T: Clone> Clone for Array<T> {
    fn clone(&self) -> Self {
        let mut array = Array::new(self.size).expect("allocation failed");

        for (i, v) in self.iter().enumerate() {
            array[i] = v.clone();
        }

        array
    }
}

impl<T> IntoIterator for Array<T> {
    type Item = T;
    type IntoIter = iter::IntoIter<T>;

    fn into_iter(self) -> Self::IntoIter {
        iter::IntoIter::new(self)
    }
}

impl<'a, T> IntoIterator for &'a Array<T> {
    type Item = &'a T;
    type IntoIter = iter::Iter<'a, T>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl<'a, T> IntoIterator for &'a mut Array<T> {
    type Item = &'a mut T;
    type IntoIter = iter::IterMut<'a, T>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter_mut()
    }
}

/// Iterators for [`Array`]
pub mod iter {
    use super::Array;
    use std::marker::PhantomData;

    /// Immutable [`Array`] iterator.
    pub struct Iter<'a, T> {
        _marker: PhantomData<&'a T>,
        ptr: *const T,
        end: *const T,
    }

    impl<'a, T> Iter<'a, T> {
        pub(crate) fn new(array: &'a Array<T>) -> Self {
            let ptr = array.ptr;
            Self {
                _marker: PhantomData,
                ptr,
                end: unsafe { ptr.add(array.size) },
            }
        }
    }

    impl<'a, T> Iterator for Iter<'a, T> {
        type Item = &'a T;

        fn next(&mut self) -> Option<Self::Item> {
            if self.ptr == self.end {
                None
            } else {
                unsafe {
                    let ptr = self.ptr;
                    self.ptr = self.ptr.add(1);
                    Some(&*ptr)
                }
            }
        }
    }

    /// Mutable [`Array`] iterator
    pub struct IterMut<'a, T> {
        _marker: PhantomData<&'a T>,
        ptr: *mut T,
        end: *mut T,
    }

    impl<'a, T> IterMut<'a, T> {
        pub(crate) fn new(array: &'a Array<T>) -> Self {
            let ptr = array.ptr;
            Self {
                _marker: PhantomData,
                ptr,
                end: unsafe { ptr.add(array.size) },
            }
        }
    }

    impl<'a, T> Iterator for IterMut<'a, T> {
        type Item = &'a mut T;

        fn next(&mut self) -> Option<Self::Item> {
            if self.ptr == self.end {
                None
            } else {
                unsafe {
                    let ptr = self.ptr;
                    self.ptr = self.ptr.add(1);
                    Some(&mut *ptr)
                }
            }
        }
    }

    /// Owned [`Array`] Iterator
    pub struct IntoIter<T> {
        _array: Array<T>,
        ptr: *const T,
        end: *const T,
    }

    impl<T> IntoIter<T> {
        pub(crate) fn new(array: Array<T>) -> Self {
            unsafe {
                let ptr = array.ptr.cast_const();
                let end = ptr.add(array.size);
                Self {
                    _array: array,
                    ptr,
                    end,
                }
            }
        }
    }

    impl<T> Iterator for IntoIter<T> {
        type Item = T;

        fn next(&mut self) -> Option<Self::Item> {
            if self.ptr == self.end {
                None
            } else {
                unsafe {
                    let ptr = self.ptr;
                    self.ptr = self.ptr.add(1);
                    Some(ptr.read())
                }
            }
        }
    }
}

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

    #[test]
    fn array_basics() {
        let mut array = Array::new(5).expect("failed to allocate");
        array[0] = 1;
        array[1] = 2;
        array[2] = 3;
        array[3] = 4;
        array[4] = 5;

        for (i, v) in array.iter().enumerate() {
            match i {
                0 => assert_eq!(*v, 1),
                1 => assert_eq!(*v, 2),
                2 => assert_eq!(*v, 3),
                3 => assert_eq!(*v, 4),
                4 => assert_eq!(*v, 5),
                _ => unreachable!(),
            }
        }
    }
}