1use alloc::collections::{LinkedList, VecDeque};
23use alloc::string::String;
24use alloc::vec::Vec;
25use core::alloc::Allocator;
26
27use super::{Collection, CollectionAlloc, CollectionBase, CollectionBytes, CollectionCapacity};
28
29impl CollectionBase for String {
30 type Item = char;
31
32 #[inline]
33 fn item_count(&self) -> usize {
34 self.chars().count()
35 }
36
37 #[inline]
38 fn is_empty(&self) -> bool {
39 String::is_empty(self)
40 }
41
42 #[inline]
43 fn push(&mut self, elem: Self::Item) {
44 String::push(self, elem)
45 }
46
47 #[inline]
48 fn clear(&mut self) {
49 String::clear(self)
50 }
51}
52
53impl CollectionBytes for String {
54 #[inline]
55 fn byte_len(&self) -> usize {
56 String::len(self)
57 }
58
59 #[inline]
60 fn item_byte_len(item: &Self::Item) -> usize {
61 item.len_utf8()
62 }
63}
64
65impl Collection for String {
66 #[inline]
67 fn with_capacity(capacity: usize) -> Self {
68 String::with_capacity(capacity)
69 }
70}
71
72impl CollectionCapacity for String {
73 type ReserveError = alloc::collections::TryReserveError;
74
75 #[inline]
76 fn capacity(&self) -> usize {
77 String::capacity(self)
78 }
79
80 #[inline]
81 fn reserve(&mut self, additional: usize) {
82 String::reserve(self, additional)
83 }
84
85 #[inline]
86 fn try_reserve(&mut self, additional: usize) -> Result<(), Self::ReserveError> {
87 String::try_reserve(self, additional)
88 }
89
90 #[inline]
91 fn try_push(&mut self, elem: Self::Item) -> Result<(), Self::Item> {
92 if String::try_reserve(self, elem.len_utf8()).is_err() {
93 return Err(elem);
94 }
95 String::push(self, elem);
96 Ok(())
97 }
98}
99
100impl<T, A: Allocator> CollectionBase for Vec<T, A> {
101 type Item = T;
102
103 fn item_count(&self) -> usize {
104 Vec::len(self)
105 }
106
107 fn push(&mut self, elem: Self::Item) {
108 Vec::push(self, elem)
109 }
110
111 fn clear(&mut self) {
112 Vec::clear(self)
113 }
114}
115
116impl<T> Collection for Vec<T> {
117 fn with_capacity(capacity: usize) -> Self {
118 Vec::with_capacity(capacity)
119 }
120}
121
122impl<T, A: Allocator> CollectionAlloc for Vec<T, A> {
123 type Allocator = A;
124
125 fn with_capacity_in(capacity: usize, allocator: Self::Allocator) -> Self {
126 Vec::with_capacity_in(capacity, allocator)
127 }
128
129 fn allocator(&self) -> &Self::Allocator {
130 Vec::allocator(self)
131 }
132}
133
134impl<T, A: Allocator> CollectionCapacity for Vec<T, A> {
135 type ReserveError = alloc::collections::TryReserveError;
136
137 fn capacity(&self) -> usize {
138 Vec::capacity(self)
139 }
140
141 fn reserve(&mut self, additional: usize) {
142 Vec::reserve(self, additional)
143 }
144
145 fn try_reserve(&mut self, additional: usize) -> Result<(), Self::ReserveError> {
146 Vec::try_reserve(self, additional)
147 }
148}
149
150impl<A: Allocator> CollectionBytes for Vec<u8, A> {
151 fn byte_len(&self) -> usize {
152 Vec::len(self)
153 }
154
155 fn item_byte_len(_item: &Self::Item) -> usize {
156 1
157 }
158}
159
160impl<T> CollectionBase for VecDeque<T> {
161 type Item = T;
162
163 fn item_count(&self) -> usize {
164 VecDeque::len(self)
165 }
166
167 fn push(&mut self, elem: Self::Item) {
168 VecDeque::push_back(self, elem)
169 }
170
171 fn clear(&mut self) {
172 VecDeque::clear(self)
173 }
174}
175
176impl<T> Collection for VecDeque<T> {
177 fn with_capacity(capacity: usize) -> Self {
178 VecDeque::with_capacity(capacity)
179 }
180}
181
182impl<T> CollectionCapacity for VecDeque<T> {
183 type ReserveError = alloc::collections::TryReserveError;
184
185 fn capacity(&self) -> usize {
186 VecDeque::capacity(self)
187 }
188
189 fn reserve(&mut self, additional: usize) {
190 VecDeque::reserve(self, additional)
191 }
192
193 fn try_reserve(&mut self, additional: usize) -> Result<(), Self::ReserveError> {
194 VecDeque::try_reserve(self, additional)
195 }
196}
197
198impl<T> CollectionBase for LinkedList<T> {
199 type Item = T;
200
201 fn item_count(&self) -> usize {
202 LinkedList::len(self)
203 }
204
205 fn push(&mut self, elem: Self::Item) {
206 LinkedList::push_back(self, elem)
207 }
208
209 fn clear(&mut self) {
210 LinkedList::clear(self)
211 }
212}
213
214impl<T> Collection for LinkedList<T> {
215 fn with_capacity(_capacity: usize) -> Self {
216 LinkedList::new()
217 }
218}