1use crate::{raw::StorageWithCapacity, SimpleVec, Storage};
2
3#[allow(unused_imports)]
4use core::{
5 borrow::{Borrow, BorrowMut},
6 hash::{Hash, Hasher},
7 ops::{Index, IndexMut},
8 ptr::NonNull,
9 slice::SliceIndex,
10};
11
12#[cfg(feature = "alloc")]
13use std::vec::Vec;
14
15impl<S: StorageWithCapacity> Clone for SimpleVec<S>
16where
17 S::Item: Clone,
18{
19 fn clone(&self) -> Self {
20 let mut vec = Self::with_capacity(self.len());
21 vec.extend_from_slice(self);
22 vec
23 }
24
25 fn clone_from(&mut self, source: &Self) { self.clone_from(source); }
26}
27
28impl<S: StorageWithCapacity + Default> Default for SimpleVec<S> {
29 fn default() -> Self { Self::with_storage(Default::default()) }
30}
31
32impl<O: ?Sized + AsRef<[S::Item]>, S: ?Sized + Storage> PartialEq<O> for SimpleVec<S>
33where
34 S::Item: PartialEq,
35{
36 fn eq(&self, other: &O) -> bool { self.as_slice() == other.as_ref() }
37}
38
39impl<S: ?Sized + Storage> Eq for SimpleVec<S> where S::Item: Eq {}
40
41impl<O: ?Sized + AsRef<[S::Item]>, S: ?Sized + Storage> PartialOrd<O> for SimpleVec<S>
42where
43 S::Item: PartialOrd,
44{
45 fn partial_cmp(&self, other: &O) -> Option<core::cmp::Ordering> { self.as_slice().partial_cmp(other.as_ref()) }
46}
47
48impl<S: ?Sized + Storage> Ord for SimpleVec<S>
49where
50 S::Item: Ord,
51{
52 fn cmp(&self, other: &Self) -> core::cmp::Ordering { self.as_slice().cmp(other.as_ref()) }
53}
54
55impl<S: ?Sized + Storage> Hash for SimpleVec<S>
56where
57 S::Item: Hash,
58{
59 fn hash<H: Hasher>(&self, state: &mut H) { self.as_slice().hash(state) }
60}
61
62use core::fmt;
63impl<S: ?Sized + Storage> fmt::Debug for SimpleVec<S>
64where
65 S::Item: fmt::Debug,
66{
67 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.as_slice().fmt(f) }
68}
69
70impl<S: ?Sized + Storage> AsRef<[S::Item]> for SimpleVec<S> {
71 fn as_ref(&self) -> &[S::Item] { self }
72}
73
74impl<S: ?Sized + Storage> AsMut<[S::Item]> for SimpleVec<S> {
75 fn as_mut(&mut self) -> &mut [S::Item] { self }
76}
77
78impl<S: ?Sized + Storage> Borrow<[S::Item]> for SimpleVec<S> {
79 fn borrow(&self) -> &[S::Item] { self }
80}
81
82impl<S: ?Sized + Storage> BorrowMut<[S::Item]> for SimpleVec<S> {
83 fn borrow_mut(&mut self) -> &mut [S::Item] { self }
84}
85
86#[cfg(any(doc, feature = "nightly"))]
87impl<T, const N: usize> From<[T; N]> for crate::ArrayVec<T, N> {
88 fn from(array: [T; N]) -> Self { Self::from_array(array) }
89}
90
91#[cfg(any(doc, feature = "nightly"))]
92impl<T, const N: usize> TryFrom<crate::ArrayVec<T, N>> for [T; N] {
93 type Error = crate::ArrayVec<T, N>;
94
95 fn try_from(value: crate::ArrayVec<T, N>) -> Result<Self, Self::Error> { value.try_into_array() }
96}
97
98#[cfg(not(doc))]
99#[cfg(feature = "alloc")]
100#[cfg(not(feature = "nightly"))]
101impl<T> From<Vec<T>> for crate::HeapVec<T> {
102 fn from(vec: Vec<T>) -> Self {
103 let mut vec = core::mem::ManuallyDrop::new(vec);
104
105 let len = vec.len();
106 let cap = vec.capacity();
107 let ptr = unsafe { NonNull::new_unchecked(vec.as_mut_ptr()) };
108
109 unsafe { crate::HeapVec::from_raw_parts(len, crate::raw::heap::stable::box_from_raw_parts(ptr, cap)) }
110 }
111}
112
113#[cfg(any(doc, feature = "alloc"))]
114#[cfg(any(doc, feature = "nightly"))]
115impl<T, A: std::alloc::Allocator> From<Vec<T, A>> for crate::HeapVec<T, A> {
116 fn from(vec: Vec<T, A>) -> Self {
117 let (ptr, len, cap, alloc) = vec.into_raw_parts_with_alloc();
118
119 unsafe {
120 crate::HeapVec::from_raw_parts(
121 len,
122 crate::raw::heap::nightly::box_from_raw_parts_in(NonNull::new_unchecked(ptr), cap, alloc),
123 )
124 }
125 }
126}
127
128#[cfg(not(doc))]
129#[cfg(feature = "alloc")]
130#[cfg(not(feature = "nightly"))]
131impl<T> From<crate::HeapVec<T>> for Vec<T> {
132 fn from(vec: crate::HeapVec<T>) -> Self {
133 let (length, alloc) = vec.into_raw_parts();
134 let (ptr, capacity) = crate::raw::heap::stable::box_into_raw_parts(alloc);
135
136 unsafe { Vec::from_raw_parts(ptr.as_ptr(), length, capacity) }
137 }
138}
139
140#[cfg(any(doc, feature = "alloc"))]
141#[cfg(any(doc, feature = "nightly"))]
142impl<T, A: std::alloc::Allocator> From<crate::HeapVec<T, A>> for Vec<T, A> {
143 fn from(vec: crate::HeapVec<T, A>) -> Self {
144 let (length, alloc) = vec.into_raw_parts();
145 let (ptr, capacity, alloc) = crate::raw::heap::nightly::box_into_raw_parts_with_alloc(alloc);
146
147 unsafe { Vec::from_raw_parts_in(ptr.as_ptr(), length, capacity, alloc) }
148 }
149}
150
151impl<S: Storage + ?Sized, I> Index<I> for SimpleVec<S>
152where
153 I: SliceIndex<[S::Item]>,
154{
155 type Output = I::Output;
156
157 fn index(&self, index: I) -> &Self::Output { self.as_slice().index(index) }
158}
159
160impl<S: Storage + ?Sized, I> IndexMut<I> for SimpleVec<S>
161where
162 I: SliceIndex<[S::Item]>,
163{
164 fn index_mut(&mut self, index: I) -> &mut Self::Output { self.as_mut_slice().index_mut(index) }
165}