fromsoftware_shared/util/
incomplete_array_field.rs

1use std::fmt;
2use std::marker::PhantomData;
3use std::slice;
4
5/// This is an adaptation of the __IncompleteArrayField type that rust-bindgen
6/// generates.
7#[repr(transparent)]
8#[derive(Default)]
9pub struct IncompleteArrayField<T>(PhantomData<T>, [T; 0]);
10impl<T> IncompleteArrayField<T> {
11    /// Creates a new field with no contents.
12    #[inline]
13    pub const fn new() -> Self {
14        IncompleteArrayField(PhantomData, [])
15    }
16
17    /// Returns a constant pointer to the beginning of this field.
18    #[inline]
19    pub fn as_ptr(&self) -> *const T {
20        self as *const _ as *const T
21    }
22
23    /// Returns a mutable pointer to the beginning of this field.
24    #[inline]
25    pub fn as_mut_ptr(&mut self) -> *mut T {
26        self as *mut _ as *mut T
27    }
28
29    /// Returns a slice representing the data in this field.
30    ///
31    /// ## Safety
32    ///
33    /// The caller must have out-of-band knowledge that the field is at least
34    /// `len` entries long.
35    #[inline]
36    pub unsafe fn as_slice(&self, len: usize) -> &[T] {
37        unsafe { slice::from_raw_parts(self.as_ptr(), len) }
38    }
39
40    /// Returns a mutable slice representing the data in this field.
41    ///
42    /// ## Safety
43    ///
44    /// The caller must have out-of-band knowledge that the field is at least
45    /// `len` entries long.
46    #[inline]
47    pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] {
48        unsafe { slice::from_raw_parts_mut(self.as_mut_ptr(), len) }
49    }
50}
51
52impl<T> ::std::fmt::Debug for IncompleteArrayField<T> {
53    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> ::std::fmt::Result {
54        fmt.write_str("__IncompleteArrayField")
55    }
56}