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
137
138
139
140
141
//! This module contains functionality common to both `Array`s and
//! `Dictionary`s.

use std::ops::{Deref, DerefMut};
use std::slice;

use libc::size_t;

use crate::NonOwning;

#[repr(C)]
pub struct Collection<T> {
    pub(crate) items: *mut T,
    pub size: size_t,
    pub(crate) capacity: size_t,
}

impl<T> Default for Collection<T> {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

impl<T> Collection<T> {
    /// Creates a new empty `Collection`.
    #[inline]
    pub const fn new() -> Self {
        Self { items: std::ptr::null_mut(), size: 0, capacity: 0 }
    }

    /// The number of items in the collection.
    #[inline]
    pub const fn len(&self) -> usize {
        self.size
    }

    #[inline]
    pub const fn is_empty(&self) -> bool {
        self.len() == 0
    }

    #[inline]
    pub(crate) fn as_slice(&self) -> &[T] {
        if self.items.is_null() {
            &[]
        } else {
            unsafe { slice::from_raw_parts(self.items, self.size) }
        }
    }

    #[inline]
    pub(crate) fn as_mut_slice(&mut self) -> &mut [T] {
        unsafe { slice::from_raw_parts_mut(self.items, self.size) }
    }

    #[inline]
    pub(crate) unsafe fn from_raw_parts(
        ptr: *mut T,
        size: usize,
        capacity: usize,
    ) -> Self {
        Self { items: ptr, size, capacity }
    }

    /// Make a non-owning version of this `Collection`.
    #[inline]
    #[doc(hidden)]
    pub fn non_owning(&self) -> NonOwning<'_, Self> {
        NonOwning::new(Self { ..*self })
    }
}

impl<T: Clone> Clone for Collection<T> {
    fn clone(&self) -> Self {
        self.as_slice().to_owned().into()
    }
}

impl<T> Drop for Collection<T> {
    fn drop(&mut self) {
        let _ =
            unsafe { Vec::from_raw_parts(self.items, self.size, self.size) };
    }
}

impl<T: PartialEq> PartialEq for Collection<T> {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.as_slice() == other.as_slice()
    }
}

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

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

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

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

// fn index(&self, index: I) -> &Self::Output {
//     self.deref().index(index)
// }
// }

impl<T> From<Vec<T>> for Collection<T> {
    #[inline]
    fn from(vec: Vec<T>) -> Self {
        let size = vec.len();
        let capacity = vec.capacity();
        let ptr = vec.leak() as *mut [T] as *mut T;

        unsafe { Self::from_raw_parts(ptr, size, capacity) }
    }
}

impl<T> From<Collection<T>> for Vec<T> {
    #[inline]
    fn from(coll: Collection<T>) -> Self {
        unsafe {
            if coll.items.is_null() {
                Vec::new()
            } else {
                Vec::from_raw_parts(coll.items, coll.size, coll.capacity)
            }
        }
    }
}