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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//! `ThinBoxedSlice` stores the size of the slice before the content of the slice, so that `size_of::<ThinBoxedSlice>` is only the size of a pointer:
//!
//! ```
//! use core::mem::size_of;
//! use thin_boxed_slice::ThinBoxedSlice;
//! assert_eq!(size_of::<ThinBoxedSlice<u8>>(), size_of::<*mut u8>());
//! ```
//!
//! # Examples
//!
//! ```
//! use thin_boxed_slice::ThinBoxedSlice;
//! use core::ops::Deref;
//!
//! let data = &[1, 2, 3];
//! let result = ThinBoxedSlice::<i32>::from(data);
//! assert_eq!(result.len(), 3);
//! assert_eq!(result.deref(), data);
//! ```

#![cfg_attr(not(test), no_std)]

use core::borrow::Borrow;
use core::cmp::max;
use core::hash::{Hash, Hasher};
use core::marker::PhantomData;
use core::mem::{align_of, size_of};
use core::ops::Deref;
use core::ptr::NonNull;
use core::slice;

use allocator_api2::alloc::{self, Allocator, Global};

mod tests;

#[derive(Debug)]
pub struct ThinBoxedSlice<T, A: Allocator = Global> {
    p: NonNull<u8>,
    allocator: A,
    phantom: PhantomData<T>,
}

impl<T, A: Allocator> ThinBoxedSlice<T, A> {
    const fn array_offset() -> usize {
        let align = align_of::<T>();
        let misalign = size_of::<usize>() % align;
        let padding = if misalign == 0 { 0 } else { align - misalign };
        size_of::<usize>() + padding
    }
    fn layout(n: usize) -> alloc::Layout {
        let alloc_len = Self::array_offset() + n * size_of::<T>();
        let align = max(align_of::<usize>(), align_of::<T>());
        alloc::Layout::from_size_align(alloc_len, align).unwrap()
    }
    fn array_ptr(&self) -> *mut T {
        unsafe { self.p.clone().as_ptr().add(Self::array_offset()) as *mut T }
    }
    fn len(&self) -> usize {
        // Useful unstable: non_null_convenience
        unsafe { self.p.cast::<usize>().clone().as_ptr().read() }
    }
}

impl<T: Clone, A: Allocator> ThinBoxedSlice<T, A> {
    pub fn new_in(s: &[T], allocator: A) -> Self {
        let layout = Self::layout(s.len());
        unsafe {
            let p = allocator.allocate(layout).unwrap().cast::<u8>();
            let ret = Self {
                p: p.clone(),
                allocator,
                phantom: PhantomData::default(),
            };
            // Useful unstable: non_null_convenience
            p.cast::<usize>().as_ptr().write(s.len());
            let mut v = ret.array_ptr();
            // Useful unstable: maybe_uninit_write_slice
            for i in 0..s.len() {
                v.write(s[i].clone());
                v = v.add(1);
            }
            ret
        }
    }
}

impl<T, A: Allocator> Drop for ThinBoxedSlice<T, A> {
    fn drop(&mut self) {
        unsafe {
            self.allocator.deallocate(self.p, Self::layout(self.len()));
        }
    }
}

impl<T: Clone, A: Allocator + Default> From<&[T]> for ThinBoxedSlice<T, A> {
    fn from(value: &[T]) -> Self {
        Self::new_in(value, A::default())
    }
}

impl<T: Clone, A: Allocator + Default, const N: usize> From<&[T; N]>
    for ThinBoxedSlice<T, A>
{
    fn from(value: &[T; N]) -> Self {
        Self::from(value.as_slice())
    }
}

impl<T, A: Allocator> Deref for ThinBoxedSlice<T, A> {
    type Target = [T];
    fn deref(&self) -> &Self::Target {
        unsafe { slice::from_raw_parts(self.array_ptr(), self.len()) }
    }
}

impl<T, A: Allocator> Borrow<[T]> for ThinBoxedSlice<T, A> {
    fn borrow(&self) -> &[T] {
        self.deref()
    }
}

impl<T: PartialEq, A: Allocator> PartialEq for ThinBoxedSlice<T, A> {
    fn eq(&self, other: &Self) -> bool {
        self.deref() == other.deref()
    }
}

impl<T: PartialEq, A: Allocator> Eq for ThinBoxedSlice<T, A> {
    fn assert_receiver_is_total_eq(&self) {}
}

impl<T: Hash, A: Allocator> Hash for ThinBoxedSlice<T, A> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.deref().hash(state);
    }
}