pub use crate::prelude::*;
#[repr(C)]
pub struct FatPtrArray<'a, E, L = ()>
where
Self: 'a,
{
data: &'a mut FPArrayBlock<E, L>,
}
impl<'a, E, L> BaseArrayRef for FatPtrArray<'a, E, L> {
fn is_null(&self) -> bool {
self.data.is_null()
}
}
impl<'a, E, L> UnsafeArrayRef for FatPtrArray<'a, E, L> {
unsafe fn null_ref() -> Self {
Self {
data: &mut *(FPArrayBlock::null_ptr()),
}
}
}
impl<'a, E, L> Index<usize> for FatPtrArray<'a, E, L> {
type Output = E;
fn index(&self, idx: usize) -> &E {
&self.data[idx]
}
}
impl<'a, E, L> IndexMut<usize> for FatPtrArray<'a, E, L> {
fn index_mut(&mut self, idx: usize) -> &mut E {
&mut self.data[idx]
}
}
impl<'a, E, L> Clone for FatPtrArray<'a, E, L>
where
E: Clone,
L: Clone,
{
fn clone(&self) -> Self {
Self {
data: self.data.clone(),
}
}
fn clone_from(&mut self, source: &Self) {
if source.len() != self.len() {
*self = source.clone();
} else {
self.get_label_mut().clone_from(source.get_label());
for i in 0..source.len() {
self[i].clone_from(&source[i]);
}
}
}
}
impl<'a, E, L> Drop for FatPtrArray<'a, E, L> {
fn drop(&mut self) {
#[cfg(not(feature = "no-asserts"))]
assert!(!self.is_null());
let mut_ref = &mut self.data;
unsafe { mut_ref.dealloc() };
mem::forget(mut_ref);
}
}
impl<'a, E, L> Container<(usize, E)> for FatPtrArray<'a, E, L> {
fn add(&mut self, elem: (usize, E)) {
self[elem.0] = elem.1;
}
fn len(&self) -> usize {
self.data.len()
}
}
impl<'a, E, L> CopyMap<'a, usize, E> for FatPtrArray<'a, E, L>
where
E: 'a,
{
fn get(&'a self, key: usize) -> Option<&'a E> {
if key > self.len() {
None
} else {
Some(&self[key])
}
}
fn get_mut(&'a mut self, key: usize) -> Option<&'a mut E> {
if key > self.len() {
None
} else {
Some(&mut self[key])
}
}
fn insert(&mut self, key: usize, value: E) -> Option<E> {
if key > self.len() {
None
} else {
Some(mem::replace(&mut self[key], value))
}
}
}
impl<'a, E, L> Array<'a, E> for FatPtrArray<'a, E, L> where E: 'a {}
impl<'a, E> MakeArray<'a, E> for FatPtrArray<'a, E, ()>
where
E: 'a,
{
fn new<F>(len: usize, mut func: F) -> Self
where
F: FnMut(usize) -> E,
{
Self::with_label((), len, |_, idx| func(idx))
}
}
impl<'a, E, L> LabelledArray<'a, E, L> for FatPtrArray<'a, E, L>
where
E: 'a,
{
fn with_label<F>(label: L, len: usize, func: F) -> Self
where
F: FnMut(&mut L, usize) -> E,
{
Self {
data: FPArrayBlock::<E, L>::new_ptr(label, len, func),
}
}
unsafe fn with_label_unsafe(label: L, len: usize) -> Self {
let new_ptr = FPArrayBlock::<E, L>::new_ptr_unsafe(label, len);
Self { data: new_ptr }
}
fn get_label(&self) -> &L {
&self.data.label
}
fn get_label_mut(&mut self) -> &mut L {
&mut self.data.label
}
unsafe fn get_label_unsafe(&self) -> &mut L {
self.data.get_label_unsafe()
}
unsafe fn get_unsafe(&self, idx: usize) -> &mut E {
self.data.unchecked_access(idx)
}
}
impl<'a, E, L> DefaultLabelledArray<'a, E, L> for FatPtrArray<'a, E, L>
where
E: 'a + Default,
{
fn with_len(label: L, len: usize) -> Self {
Self {
data: FPArrayBlock::new_ptr_default(label, len),
}
}
}