nibblecode 0.1.0

A serialization format based on rkyv
Documentation
use core::borrow::Borrow;
use core::cmp::Ordering;
use core::fmt::{self, Debug};
use core::hash::{Hash, Hasher};
use core::marker::PhantomData;
use core::ops::{Deref, DerefMut, Index, IndexMut};
use core::slice;
use core::slice::SliceIndex;

use crate::primitive::ArchivedUsize;

pub struct ArchivedList<T> {
	pub(crate) len: ArchivedUsize,
	pub(crate) offset: ArchivedUsize,
	_phantom_data: PhantomData<T>,
}
impl<T> ArchivedList<T> {
	/// Create a new `ArchivedList` from its parts.
	///
	/// # Safety
	/// `len` and `offset` must be small enough to fit in `FixedUsize`s
	pub(crate) unsafe fn new(len: usize, offset: usize) -> ArchivedList<T> {
		ArchivedList {
			offset: ArchivedUsize::from_native(offset as _),
			len: ArchivedUsize::from_native(len as _),
			_phantom_data: PhantomData,
		}
	}

	/// Returns a pointer to the first element of the archived vec.
	#[must_use]
	pub fn as_ptr(&self) -> *const T {
		unsafe { <*const ArchivedList<T>>::cast::<u8>(self).add(self.offset.to_native() as usize) }
			.cast()
	}

	/// Returns the number of elements in the archived vec.
	#[must_use]
	pub fn len(&self) -> ArchivedUsize {
		self.len
	}

	/// Returns whether the archived vec is empty.
	#[must_use]
	pub fn is_empty(&self) -> bool {
		self.len == 0
	}

	/// Gets the elements of the archived vec as a slice.
	#[must_use]
	pub fn as_slice(&self) -> &[T] {
		unsafe { slice::from_raw_parts(self.as_ptr(), self.len.to_native() as usize) }
	}

	/// Gets the elements of the archived vec as a mutable slice.
	#[must_use]
	pub fn as_slice_mut(&mut self) -> &mut [T] {
		unsafe {
			slice::from_raw_parts_mut(self.as_ptr().cast_mut(), self.len.to_native() as usize)
		}
	}
}

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

impl<T> Borrow<[T]> for ArchivedList<T> {
	fn borrow(&self) -> &[T] {
		self.as_slice()
	}
}

impl<T: Debug> Debug for ArchivedList<T> {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		f.debug_list().entries(self.as_slice()).finish()
	}
}

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

	fn deref(&self) -> &[T] {
		self.as_slice()
	}
}

impl<T> DerefMut for ArchivedList<T> {
	fn deref_mut(&mut self) -> &mut [T] {
		self.as_slice_mut()
	}
}

impl<T: Eq> Eq for ArchivedList<T> {}

impl<T: Hash> Hash for ArchivedList<T> {
	fn hash<H: Hasher>(&self, state: &mut H) {
		self.as_slice().hash(state);
	}
}

impl<T, I: SliceIndex<[T]>> Index<I> for ArchivedList<T> {
	type Output = <[T] as Index<I>>::Output;

	fn index(&self, index: I) -> &<[T] as Index<I>>::Output {
		self.as_slice().index(index)
	}
}

impl<T, I: SliceIndex<[T]>> IndexMut<I> for ArchivedList<T> {
	fn index_mut(&mut self, index: I) -> &mut <[T] as Index<I>>::Output {
		self.as_slice_mut().index_mut(index)
	}
}

impl<T: Ord> Ord for ArchivedList<T> {
	fn cmp(&self, other: &ArchivedList<T>) -> Ordering {
		self.as_slice().cmp(other.as_slice())
	}
}

impl<T: PartialEq<U>, U> PartialEq<ArchivedList<U>> for ArchivedList<T> {
	fn eq(&self, other: &ArchivedList<U>) -> bool {
		self.as_slice().eq(other.as_slice())
	}
}

impl<T: PartialEq<U>, U, const N: usize> PartialEq<[U; N]> for ArchivedList<T> {
	fn eq(&self, other: &[U; N]) -> bool {
		self.as_slice().eq(&other[..])
	}
}

impl<T: PartialEq<U>, U, const N: usize> PartialEq<ArchivedList<T>> for [U; N] {
	fn eq(&self, other: &ArchivedList<T>) -> bool {
		other.eq(self)
	}
}

impl<T: PartialEq<U>, U> PartialEq<[U]> for ArchivedList<T> {
	fn eq(&self, other: &[U]) -> bool {
		self.as_slice().eq(other)
	}
}

impl<T: PartialEq<U>, U> PartialEq<ArchivedList<U>> for [T] {
	fn eq(&self, other: &ArchivedList<U>) -> bool {
		self.eq(other.as_slice())
	}
}

impl<T: PartialOrd> PartialOrd<ArchivedList<T>> for ArchivedList<T> {
	fn partial_cmp(&self, other: &ArchivedList<T>) -> Option<Ordering> {
		self.as_slice().partial_cmp(other.as_slice())
	}
}

impl<T: PartialOrd> PartialOrd<[T]> for ArchivedList<T> {
	fn partial_cmp(&self, other: &[T]) -> Option<Ordering> {
		self.as_slice().partial_cmp(other)
	}
}

impl<T: PartialOrd> PartialOrd<ArchivedList<T>> for [T] {
	fn partial_cmp(&self, other: &ArchivedList<T>) -> Option<Ordering> {
		self.partial_cmp(other.as_slice())
	}
}