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 ElemRef<'a> where Self: 'a;
27    type Slice: ?Sized + Slice;
28    type Collection: FromIterator<Self::Elem>;
29    type Drain<'a>: Iterator<Item = Self::Elem> where Self: 'a;
30
31    fn as_slice(&self) -> &Self::Slice;
32
33    fn as_mut_slice(&mut self) -> &mut Self::Slice;
34
35    fn as_mut_collection(&mut self) -> &mut Self::Collection;
36
37    fn capacity(&self) -> usize;
38
39    fn reserve(&mut self, additional: usize) { noop!(additional) }
40
41    fn reserve_exact(&mut self, additional: usize) { noop!(additional) }
42
43    fn shrink_to_fit(&mut self) { noop!() }
44
45    fn shrink_to(&mut self, min_capacity: usize) { noop!(min_capacity) }
46
47    fn truncate(&mut self, len: usize);
48
49    fn insert(&mut self, index: usize, element: Self::Elem);
50
51    fn remove(&mut self, index: usize) -> Self::Elem;
52
53    fn push(&mut self, value: Self::Elem);
54
55    fn pop(&mut self) -> Option<Self::Elem>;
56
57    fn append(&mut self, other: &mut Self::Collection);
58
59    fn drain<R>(&mut self, range: R) -> Self::Drain<'_>
60    where R: RangeBounds<usize>,;
61
62    fn clear(&mut self);
63
64    fn len(&self) -> usize;
65
66    fn is_empty(&self) -> bool;
67
68    #[must_use = "use `.truncate()` if you don't need the other half"]
69    fn split_off(&mut self, at: usize) -> Self::Collection {
70        self.drain(at..).collect()
71    }
72
73    fn resize(&mut self, new_len: usize, value: Self::Elem)
74    where Self::Elem: Clone,;
75
76    fn resize_with<F>(&mut self, new_len: usize, f: F)
77    where F: FnMut() -> Self::Elem,;
78
79    fn retain<F>(&mut self, f: F)
80    where F: FnMut(Self::ElemRef<'_>) -> bool,;
81}
82
83pub trait VecLikeSolid: VecLike {
84    fn swap_remove(&mut self, index: usize) -> Self::Elem;
85
86    fn retain_mut<F>(&mut self, f: F)
87    where F: FnMut(&mut Self::Elem) -> bool,;
88
89    fn pop_if<F>(&mut self, predicate: F) -> Option<Self::Elem>
90    where F: FnOnce(&mut Self::Elem) -> bool,;
91
92    // FIXME: 该方法较为冷门, 暂时不实现
93
94    //fn dedup_by_key<F, K>(&mut self, key: F)
95    //where
96    //    F: FnMut(&mut Self::Elem) -> K,
97    //    K: PartialEq,;
98
99    //fn dedup_by<F>(&mut self, same_bucket: F)
100    //where
101    //    F: FnMut(&mut Self::Elem, &mut Self::Elem) -> bool,;
102}