1use crate::shared::{Link, Shareable, Shared};
6use std::sync::Arc;
7
8pub struct List<T>(Vec<ListEntry<T>>);
64
65#[allow(non_camel_case_types)]
66pub type share_entry_w<T> = fn(ListEntry<T>) -> Shared<T, super::W>;
67pub type Drain<'a, T> = std::iter::Map<std::vec::Drain<'a, ListEntry<T>>, share_entry_w<T>>;
68impl<T> List<T> {
69 pub fn append(&mut self, o: &mut Self) {
71 self.0.append(&mut o.0)
72 }
73 pub fn capacity(&self) -> usize {
75 self.0.capacity()
76 }
77 pub fn clear(&mut self) {
79 self.0.clear()
80 }
81 pub fn dedup(&mut self)
83 where
84 T: PartialEq,
85 {
86 self.dedup_by(PartialEq::eq)
87 }
88 pub fn dedup_by<F: FnMut(&T, &T) -> bool>(&mut self, mut f: F) {
90 self.0.dedup_by(|r, s| f(&r.0.borrow(), &s.0.borrow()))
91 }
92 pub fn dedup_by_key<K: PartialEq, F: FnMut(&T) -> K>(&mut self, mut f: F) {
94 self.0.dedup_by(|r, s| f(&r.0.borrow()) == f(&s.0.borrow()))
95 }
96 pub fn drain<R: std::ops::RangeBounds<usize>>(&mut self, range: R) -> Drain<T>
98 where
99 T: 'static,
100 {
101 self.0.drain(range).map(|l| Shared::from_link(l.0))
102 }
103 pub fn insert(&mut self, index: usize, element: T) {
105 self.0.insert(index, ListEntry::new(element))
106 }
107 pub fn is_empty(&self) -> bool {
109 self.0.is_empty()
110 }
111 pub fn len(&self) -> usize {
113 self.0.len()
114 }
115 pub fn new() -> Self {
117 Self(Vec::new())
118 }
119 pub fn pop(&mut self) -> Option<Shared<T, super::W>> {
121 self.0.pop().map(|l| Shared::from_link(l.0))
122 }
123 pub fn push(&mut self, value: T) {
125 self.0.push(ListEntry::new(value))
126 }
127 pub fn remove(&mut self, index: usize) -> Shared<T, super::W> {
129 Shared::from_link(self.0.remove(index).0)
130 }
131 pub fn reserve(&mut self, additional: usize) {
133 self.0.reserve(additional)
134 }
135 pub fn reserve_exact(&mut self, additional: usize) {
137 self.0.reserve_exact(additional)
138 }
139 pub fn resize(&mut self, new_len: usize, t: T)
141 where
142 T: Clone,
143 {
144 self.0.resize_with(new_len, || ListEntry::new(t.clone()))
145 }
146 pub fn resize_with<F: FnMut() -> T>(&mut self, new_len: usize, mut f: F) {
148 self.0.resize_with(new_len, || ListEntry::new(f()))
149 }
150 pub fn retain<F: FnMut(&T) -> bool>(&mut self, mut f: F) {
152 self.0.retain(|l| f(&l.0.borrow()))
153 }
154 pub fn retain_mut<F: FnMut(&mut ListEntry<T>) -> bool>(&mut self, f: F)
156 where
157 T: 'static,
158 {
159 self.0.retain_mut(f)
160 }
161 pub fn shrink_to(&mut self, min_capacity: usize) {
163 self.0.shrink_to(min_capacity)
164 }
165 pub fn shrink_to_fit(&mut self) {
167 self.0.shrink_to_fit()
168 }
169 pub fn splice<'a, R: std::ops::RangeBounds<usize>, I: 'a + IntoIterator<Item = T>>(
171 &'a mut self,
172 range: R,
173 replace_with: I,
174 ) -> impl 'a + Iterator<Item = Shared<T, super::W>>
175 where
176 T: 'static,
177 {
178 self.0
179 .splice(range, replace_with.into_iter().map(ListEntry::new))
180 .map(|l| Shared::from_link(l.0))
181 }
182 pub fn split_off(&mut self, at: usize) -> Self {
184 Self(self.0.split_off(at))
185 }
186 pub fn swap_remove(&mut self, index: usize) -> Shared<T, super::W> {
188 Shared::from_link(self.0.swap_remove(index).0)
189 }
190 pub fn truncate(&mut self, len: usize) {
192 self.0.truncate(len)
193 }
194 pub fn try_reserve(
196 &mut self,
197 additional: usize,
198 ) -> Result<(), std::collections::TryReserveError> {
199 self.0.try_reserve(additional)
200 }
201 pub fn try_reserve_exact(
203 &mut self,
204 additional: usize,
205 ) -> Result<(), std::collections::TryReserveError> {
206 self.0.try_reserve_exact(additional)
207 }
208 pub fn with_capcity(capacity: usize) -> Self {
210 Self(Vec::with_capacity(capacity))
211 }
212 pub fn binary_search(&self, x: &T) -> Result<usize, usize>
214 where
215 T: Ord,
216 {
217 self.binary_search_by(|l| x.cmp(l))
218 }
219 pub fn binary_search_by<F: FnMut(&T) -> std::cmp::Ordering>(
221 &self,
222 mut f: F,
223 ) -> Result<usize, usize> {
224 self.0.binary_search_by(|l| f(&l.0.borrow()))
225 }
226 pub fn binary_search_by_key<B: std::cmp::Ord, F: FnMut(&T) -> B>(
228 &self,
229 b: &B,
230 mut f: F,
231 ) -> Result<usize, usize> {
232 self.0.binary_search_by_key(b, |l| f(&l.0.borrow()))
233 }
234 pub fn contains(&self, x: &T) -> bool
236 where
237 T: PartialEq,
238 {
239 self.0.iter().any(|l| x == &*l.0.borrow())
240 }
241 pub fn ends_with(&self, needle: &[T]) -> bool
243 where
244 T: PartialEq,
245 {
246 self.0.len() >= needle.len()
247 && std::iter::zip(self.0.iter().rev(), needle.iter().rev())
248 .all(|(l, x)| x == &*l.0.borrow())
249 }
250 pub fn fill(&mut self, t: T)
255 where
256 T: Clone,
257 {
258 self.0.fill_with(|| ListEntry::new(t.clone()))
259 }
260 pub fn fill_with<F: FnMut() -> T>(&mut self, mut f: F) {
265 self.0.fill_with(|| ListEntry::new(f()))
266 }
267 pub fn first(&self) -> Option<ListEntry<T>> {
269 self.0.first().cloned()
270 }
271 pub fn get(&self, index: usize) -> Option<ListEntry<T>> {
273 self.0.get(index).cloned()
274 }
275 pub unsafe fn get_unchecked(&self, index: usize) -> ListEntry<T> {
280 self.0.get_unchecked(index).clone()
281 }
282 pub fn iter(&self) -> <&Self as IntoIterator>::IntoIter {
284 self.into_iter()
285 }
286 pub fn last(&self) -> Option<ListEntry<T>> {
288 self.0.last().cloned()
289 }
290 pub fn partition_point<P: FnMut(&T) -> bool>(&self, mut pred: P) -> usize {
292 self.0.partition_point(|l| pred(&l.0.borrow()))
293 }
294 pub fn reverse(&mut self) {
296 self.0.reverse()
297 }
298 pub fn rotate_left(&mut self, mid: usize) {
300 self.0.rotate_left(mid)
301 }
302 pub fn rotate_right(&mut self, mid: usize) {
304 self.0.rotate_right(mid)
305 }
306 pub fn sort(&mut self)
308 where
309 T: Ord,
310 {
311 self.sort_by(Ord::cmp)
312 }
313 pub fn sort_by<F: FnMut(&T, &T) -> std::cmp::Ordering>(&mut self, mut f: F) {
315 self.0.sort_by(|a, b| f(&a.0.borrow(), &b.0.borrow()))
316 }
317 pub fn sort_by_cached_key<U: Ord, F: FnMut(&T) -> U>(&mut self, mut f: F) {
319 self.0.sort_by_cached_key(|a| f(&a.0.borrow()))
320 }
321 pub fn sort_by_key<U: Ord, F: FnMut(&T) -> U>(&mut self, mut f: F) {
323 self.0.sort_by_key(|a| f(&a.0.borrow()))
324 }
325 pub fn sort_unstable(&mut self)
327 where
328 T: Ord,
329 {
330 self.sort_unstable_by(Ord::cmp)
331 }
332 pub fn sort_unstable_by<F: FnMut(&T, &T) -> std::cmp::Ordering>(&mut self, mut f: F) {
334 self.0
335 .sort_unstable_by(|a, b| f(&a.0.borrow(), &b.0.borrow()))
336 }
337 pub fn sort_unstable_by_key<U: Ord, F: FnMut(&T) -> U>(&mut self, mut f: F) {
339 self.0.sort_unstable_by_key(|a| f(&a.0.borrow()))
340 }
341 pub fn starts_with(&self, needle: &[T]) -> bool
343 where
344 T: PartialEq,
345 {
346 self.0.len() >= needle.len()
347 && std::iter::zip(&self.0, needle).all(|(l, x)| x == &*l.0.borrow())
348 }
349 pub fn swap(&mut self, a: usize, b: usize) {
351 self.0.swap(a, b)
352 }
353}
354impl<T> Default for List<T> {
355 fn default() -> Self {
356 Self::new()
357 }
358}
359impl<'a, T> IntoIterator for &'a List<T> {
360 type Item = ListEntry<T>;
361 type IntoIter = std::iter::Cloned<std::slice::Iter<'a, ListEntry<T>>>;
362 fn into_iter(self) -> Self::IntoIter {
363 self.0.iter().cloned()
364 }
365}
366impl<T> FromIterator<T> for List<T> {
367 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
368 Self(iter.into_iter().map(ListEntry::new).collect())
369 }
370}
371impl<T> Extend<T> for List<T> {
372 fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
373 self.0.extend(iter.into_iter().map(ListEntry::new))
374 }
375}
376impl<'a, T: 'a + Clone> Extend<&'a T> for List<T> {
377 fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
378 self.0.extend(iter.into_iter().cloned().map(ListEntry::new))
379 }
380}
381
382pub struct ListEntry<T>(Arc<Link<T>>);
390impl<T> PartialEq for ListEntry<T> {
391 fn eq(&self, o: &Self) -> bool {
392 Arc::ptr_eq(&self.0, &o.0)
393 }
394}
395impl<T> Clone for ListEntry<T> {
396 fn clone(&self) -> Self {
397 Self(self.0.clone())
398 }
399}
400impl<T> ListEntry<T> {
401 fn new(t: T) -> Self {
402 ListEntry(Arc::new(Link::new(t)))
403 }
404 pub fn share(&self) -> Shared<T, super::W> {
410 Shared::from_link(self.0.clone())
411 }
412 pub fn use_w<'a, P>(&self, cx: &dioxus_core::Scope<'a, P>) -> &'a mut Shared<T, super::W> {
418 let mut opt = Shareable(Some(self.0.clone()));
419 Shared::init(cx, &mut opt, || unreachable!(), super::W)
420 }
421 pub fn use_rw<'a, P>(&self, cx: &dioxus_core::Scope<'a, P>) -> &'a mut Shared<T, super::RW> {
429 let mut opt = Shareable(Some(self.0.clone()));
430 Shared::init(cx, &mut opt, || unreachable!(), super::RW)
431 }
432}