offset_vec/
vec_like.rs

1#![deny(unconditional_recursion)]
2
3use core::ops::RangeBounds;
4
5mod pointers_impl;
6mod vec_impl;
7mod string_impl;
8#[cfg(feature = "smallvec")]
9#[cfg_attr(docsrs, doc(cfg(feature = "smallvec")))]
10mod smallvec_impl;
11#[cfg(feature = "smallstr")]
12#[cfg_attr(docsrs, doc(cfg(feature = "smallstr")))]
13mod smallstr_impl;
14#[cfg(feature = "rc-vec")]
15#[cfg_attr(docsrs, doc(cfg(feature = "rc-vec")))]
16mod rc_vec_impl;
17
18use crate::Slice;
19
20macro_rules! noop {
21    ($($e:expr),*) => {{ $(let _ = $e;)* }};
22}
23
24pub trait VecLike {
25    type Elem;
26    type Slice: ?Sized + Slice;
27    type Collection: FromIterator<Self::Elem>;
28    type Drain<'a>: Iterator<Item = Self::Elem> where Self: 'a;
29
30    fn as_slice(&self) -> &Self::Slice;
31
32    fn as_mut_slice(&mut self) -> &mut Self::Slice;
33
34    fn as_mut_collection(&mut self) -> &mut Self::Collection;
35
36    fn capacity(&self) -> usize;
37
38    fn reserve(&mut self, additional: usize) { noop!(additional) }
39
40    fn reserve_exact(&mut self, additional: usize) { noop!(additional) }
41
42    fn shrink_to_fit(&mut self) { noop!() }
43
44    fn shrink_to(&mut self, min_capacity: usize) { noop!(min_capacity) }
45
46    fn truncate(&mut self, len: usize);
47
48    fn insert(&mut self, index: usize, element: Self::Elem);
49
50    fn remove(&mut self, index: usize) -> Self::Elem;
51
52    fn push(&mut self, value: Self::Elem);
53
54    fn pop(&mut self) -> Option<Self::Elem>;
55
56    fn append(&mut self, other: &mut Self::Collection);
57
58    fn drain<R>(&mut self, range: R) -> Self::Drain<'_>
59    where R: RangeBounds<usize>,;
60
61    fn clear(&mut self);
62
63    fn len(&self) -> usize;
64
65    fn is_empty(&self) -> bool;
66
67    #[must_use = "use `.truncate()` if you don't need the other half"]
68    fn split_off(&mut self, at: usize) -> Self::Collection {
69        self.drain(at..).collect()
70    }
71
72    fn resize(&mut self, new_len: usize, value: Self::Elem)
73    where Self::Elem: Clone,;
74
75    fn resize_with<F>(&mut self, new_len: usize, f: F)
76    where F: FnMut() -> Self::Elem,;
77}
78
79pub trait VecLikeSolid: VecLike {
80    fn swap_remove(&mut self, index: usize) -> Self::Elem;
81
82    fn retain<F>(&mut self, mut f: F)
83    where F: FnMut(&Self::Elem) -> bool,
84    {
85        self.retain_mut(|elem| f(&*elem));
86    }
87
88    fn retain_mut<F>(&mut self, f: F)
89    where F: FnMut(&mut Self::Elem) -> bool,;
90
91    fn pop_if<F>(&mut self, predicate: F) -> Option<Self::Elem>
92    where F: FnOnce(&mut Self::Elem) -> bool,;
93
94    // FIXME: 该方法较为冷门, 暂时不实现
95
96    //fn dedup_by_key<F, K>(&mut self, key: F)
97    //where
98    //    F: FnMut(&mut Self::Elem) -> K,
99    //    K: PartialEq,;
100
101    //fn dedup_by<F>(&mut self, same_bucket: F)
102    //where
103    //    F: FnMut(&mut Self::Elem, &mut Self::Elem) -> bool,;
104}
105
106pub trait VecLikeAbstract: VecLike {
107    type Indices<'a>: Iterator<Item = (usize, Self::Elem)> where Self: 'a;
108
109    fn retain<F>(&mut self, f: F)
110    where F: FnMut(Self::Elem) -> bool,;
111
112    fn elem_indices(&self) -> Self::Indices<'_>;
113}