use core::marker::PhantomData;
#[repr(C)]
pub struct Array<T: Sized> {
pub items: *mut T,
pub len: usize,
pub align: usize,
_marker: PhantomData<T>,
}
impl<T: Sized> Array<T> {
pub const fn empty() -> Self {
Self {
items: core::ptr::null_mut(),
len: 0,
align: core::mem::align_of::<T>(),
_marker: PhantomData,
}
}
pub fn new(items: *mut T, len: usize) -> Self {
Self {
items,
len,
align: core::mem::align_of::<T>(),
_marker: PhantomData,
}
}
pub fn is_empty(&self) -> bool {
self.items.is_null() || self.len == 0
}
}
unsafe impl<T: Sized + Send> Send for Array<T> {}
unsafe impl<T: Sized + Sync> Sync for Array<T> {}
#[cfg(test)]
mod tests {
use super::Array;
use core::mem::{align_of, offset_of, size_of};
#[test]
fn layout_array() {
assert_eq!(size_of::<Array<u64>>(), 24);
assert_eq!(align_of::<Array<u64>>(), 8);
assert_eq!(offset_of!(Array<u64>, items), 0);
assert_eq!(offset_of!(Array<u64>, len), 8);
assert_eq!(offset_of!(Array<u64>, align), 16);
}
#[test]
fn empty_array() {
let arr: Array<u64> = Array::empty();
assert!(arr.is_empty());
assert_eq!(arr.len, 0);
assert!(arr.items.is_null());
}
}