Skip to content

Commit

Permalink
various tweaks and comments to chainer
Browse files Browse the repository at this point in the history
  • Loading branch information
meadowsys committed Aug 16, 2024
1 parent 3c75fa9 commit e0dec3b
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 111 deletions.
53 changes: 23 additions & 30 deletions src/chainer/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,33 @@ impl<const N: usize> ArrayChain<f64, N> {
}

impl<T, const N: usize> ArrayChain<MaybeUninit<T>, N> {
/// Assumes all slots inside the array are initialised according to `T`'s
/// requirements, and converts into an array of T
///
/// Note: this implementation is currently subpar, as it does fully copy `self`
/// into a new container. I have to do this because, at the time of writing:
///
/// - `transmute` is a bit too dumb, and is not able to prove `[T; N]` and
/// `[MaybeUninit<T>; N]` are guaranteed to be equal sized, even though
/// we can see and prove it
/// - `transmute_unchecked` is like `transmute` but without that size check,
/// but it is unstable, and according to a code comment will almost certainly
/// never be stabilised (reasoning is that it's too unsafe, too much power to
/// give users :p, and to hopefully find other methods for achieving things
/// without it so its no longer needed)
/// - `MaybeUninit::array_assume_init` is unstable (it internally makes use of
/// `transmute_unchecked`)
///
/// # Safety
///
/// All slots in `self` must be fully initialised with valid values of `T`.
#[inline]
pub unsafe fn assume_init(self) -> ArrayChain<T, N> {
// TODO: this is subpar (its copying), but I can't find a better way to do it?
// all ways to do it seem to be unstable (transmute is too dumb, transmute_unchecked
// is unstable and likely won't ever be stable, MaybeUninit::array_assume_init
// is unstable (it uses transmute_unchecked internally))
let me = ManuallyDrop::new(self);
let ptr = me.as_nonchain().as_ptr().cast::<[T; N]>();
// TODO: this fn's impl is subpar (its copying), see note in doc comment

let ptr = self.as_nonchain().as_ptr().cast::<[T; N]>();

// SAFETY: this `ptr::read` call is valid because:
// - `me` is made to prevent double-dropping `self` after reading from the
// ptr (...which is also prevented by MaybeUninit, thinking about it)
// - `ptr` points to / is obtained from the [MaybeUninit<T>; N] inside ArrayChain
// - `ptr`, type [MaybeUninit<T>; N] is cast to ptr of [T; N], which is
// valid because MaybeUninit<T> has same layout as T, so [MaybeUninit<T>; N]
Expand All @@ -69,28 +84,6 @@ impl<T, const N: usize> ArrayChain<MaybeUninit<T>, N> {
}
}

impl<T, const N: usize> ArrayChain<T, N> {
#[inline]
pub fn nc_ptr(&self) -> *const T {
self.as_nonchain().as_ptr()
}

#[inline]
pub fn nc_ptr_mut(&mut self) -> *mut T {
self.as_nonchain_mut().as_mut_ptr()
}

#[inline]
pub fn nc_slice(&self) -> &[T] {
self.as_nonchain()
}

#[inline]
pub fn nc_slice_mut(&mut self) -> &mut [T] {
self.as_nonchain_mut()
}
}

impl<T, const N: usize> AsMut<[T]> for ArrayChain<T, N> {
#[inline]
fn as_mut(&mut self) -> &mut [T] {
Expand Down
22 changes: 0 additions & 22 deletions src/chainer/slice_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,28 +67,6 @@ impl<T> SliceBoxChain<MaybeUninit<T>> {
}
}

impl<T> SliceBoxChain<T> {
#[inline]
pub fn nc_ptr(&self) -> *const T {
self.as_nonchain().as_ptr()
}

#[inline]
pub fn nc_ptr_mut(&mut self) -> *mut T {
self.as_nonchain_mut().as_mut_ptr()
}

#[inline]
pub fn nc_slice(&self) -> &[T] {
self.as_nonchain()
}

#[inline]
pub fn nc_slice_mut(&mut self) -> &mut [T] {
self.as_nonchain_mut()
}
}

// TODO: align_to
// TODO: align_to_mut
// TODO: array_chunks
Expand Down
22 changes: 0 additions & 22 deletions src/chainer/slice_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,28 +615,6 @@ impl<'h> SliceMutChain<'h, f64> {
}
}

impl<'h, T> SliceMutChain<'h, T> {
#[inline]
pub fn nc_ptr(&self) -> *const T {
self.as_nonchain().as_ptr()
}

#[inline]
pub fn nc_ptr_mut(&mut self) -> *mut T {
self.as_nonchain_mut().as_mut_ptr()
}

#[inline]
pub fn nc_slice(&'h self) -> &'h [T] {
self.as_nonchain()
}

#[inline]
pub fn nc_slice_mut(&'h mut self) -> &'h mut [T] {
self.as_nonchain_mut()
}
}

// TODO: align_to
// TODO: align_to_mut
// TODO: array_chunks
Expand Down
14 changes: 1 addition & 13 deletions src/chainer/slice_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl<'h, T, const N: usize> SliceRefChain<'h, [T; N]> {
// ptr returned by `self.nc_ptr()` is valid for `self.len()` reads of `[T; N]`,
// so casting it to `T` will mean it is valid for `self.len() * N` reads of `T`.
// `len` var above is this multiplication amount
let ptr = self.nc_ptr().cast::<T>();
let ptr = self.as_nonchain().as_ptr().cast::<T>();

// SAFETY: `ptr` is valid for `len` reads (see comment above)
let slice = unsafe { slice::from_raw_parts(ptr, len) };
Expand All @@ -54,18 +54,6 @@ impl<'h, T, const N: usize> SliceRefChain<'h, [T; N]> {
}
}

impl<'h, T> SliceRefChain<'h, T> {
#[inline]
pub fn nc_ptr(&self) -> *const T {
self.as_nonchain().as_ptr()
}

#[inline]
pub fn nc_slice(&'h self) -> &[T] {
self.as_nonchain()
}
}

// TODO: align_to
// TODO: align_to_mut
// TODO: array_chunks
Expand Down
4 changes: 2 additions & 2 deletions src/chainer/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ where
Self: Sized + private::Sealed + Into<Self::NonChain> + AsRef<Self::NonChain> + AsMut<Self::NonChain>,
Self::NonChain: Into<Self> + NonChainHalf<Chain = Self>
{
/// This chainer's nonchain
/// This chainer's nonchain, "inner" type
type NonChain;

/// Borrows `self` as its nonchain
Expand Down Expand Up @@ -60,7 +60,7 @@ where
Self: Sized + private::Sealed + Into<Self::Chain>,
Self::Chain: Into<Self> + ChainHalf<NonChain = Self> + AsRef<Self> + AsMut<Self>
{
/// This type's chainer
/// This type's chainer wrapper type
type Chain;

/// Converts `self` into its associated chain type
Expand Down
22 changes: 0 additions & 22 deletions src/chainer/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1153,28 +1153,6 @@ impl VecChain<u8> {
// TODO: as_ascii/unchecked nightly
}

impl<T> VecChain<T> {
#[inline]
pub fn nc_ptr(&self) -> *const T {
self.as_nonchain().as_ptr()
}

#[inline]
pub fn nc_ptr_mut(&mut self) -> *mut T {
self.as_nonchain_mut().as_mut_ptr()
}

#[inline]
pub fn nc_slice(&self) -> &[T] {
self.as_nonchain()
}

#[inline]
pub fn nc_slice_mut(&mut self) -> &mut [T] {
self.as_nonchain_mut()
}
}

impl<T, const N: usize> VecChain<[T; N]> {
#[inline]
pub fn flatten(mut self) -> VecChain<T> {
Expand Down

0 comments on commit e0dec3b

Please sign in to comment.