array_trait/
into_array.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
use slice_trait::IntoBoxedSlice;

use crate::AsArray;

#[const_trait]
pub trait IntoArray: ~const AsArray + IntoBoxedSlice
{
    /// Returns self as an array
    /// 
    /// Useful in the case where a trait is implemented using a generic bound to the [Array](Array) trait.
    /// In this case, the compiler does not automatically know that the type with the [Array](Array)-trait is an actual array.
    /// This method lets you tell the compiler that you are now working with an actual array, and not just something
    /// which implements the trait [Array](Array).
    fn into_array(self) -> [Self::Item; Self::LENGTH];
}

impl<Item, const LENGTH: usize> const IntoArray for [Item; LENGTH]
{
    fn into_array(self) -> [Self::Item; Self::LENGTH]
    {
        let array = unsafe {core::mem::transmute_copy(&self)};
        core::mem::forget(self);
        array
    }
}