value_traits/impls/
vectors.rs

1#![cfg(feature = "alloc")]
2
3#[cfg(all(feature = "alloc", not(feature = "std")))]
4use alloc::vec::Vec;
5
6use core::{
7    iter::{Cloned, Skip},
8    ops::{Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive},
9};
10
11use crate::{
12    iter::{IterableByValue, IterableByValueFrom},
13    slices::{
14        SliceByValue, SliceByValueGet, SliceByValueRepl, SliceByValueSet, SliceByValueSubsliceGat,
15        SliceByValueSubsliceGatMut, SliceByValueSubsliceRange, SliceByValueSubsliceRangeMut,
16        Subslice, SubsliceMut,
17    },
18};
19
20impl<T> SliceByValue for Vec<T> {
21    type Value = T;
22    #[inline]
23    fn len(&self) -> usize {
24        <[T]>::len(self)
25    }
26}
27
28impl<T: Clone> SliceByValueGet for Vec<T> {
29    #[inline]
30    fn get_value(&self, index: usize) -> Option<Self::Value> {
31        // slice.get returns Option<&T>, .copied() converts to Option<T>
32        (*self).get(index).cloned()
33    }
34
35    #[inline]
36    fn index_value(&self, index: usize) -> Self::Value {
37        // Standard indexing panics on out-of-bounds.
38        // It returns &T, which we copy to return T.
39        self[index].clone()
40    }
41
42    #[inline]
43    unsafe fn get_value_unchecked(&self, index: usize) -> Self::Value {
44        // Safety: The caller must ensure that `*self` (the index) is in bounds.
45        // slice.get_unchecked returns &T, which we dereference and copy.
46        unsafe { (*self).get_unchecked(index).clone() }
47    }
48}
49
50impl<T: Clone> SliceByValueRepl for Vec<T> {
51    #[inline]
52    fn replace_value(&mut self, index: usize, value: Self::Value) -> Self::Value {
53        // Standard indexing panics on out-of-bounds.
54        // We get a mutable reference `&mut T`.
55        // mem::replace swaps the value at the location with the new `value`
56        // and returns the old value.
57        core::mem::replace(&mut self[index], value)
58    }
59
60    #[inline]
61    unsafe fn replace_value_unchecked(&mut self, index: usize, value: Self::Value) -> Self::Value {
62        // Safety: The caller must ensure that `*self` (the index) is in bounds.
63        unsafe {
64            let elem = self.get_unchecked_mut(index);
65            core::mem::replace(elem, value)
66        }
67    }
68}
69
70impl<T: Clone> SliceByValueSet for Vec<T> {
71    #[inline]
72    fn set_value(&mut self, index: usize, value: Self::Value) {
73        // Standard indexing panics on out-of-bounds
74        self[index] = value;
75    }
76
77    #[inline]
78    unsafe fn set_value_unchecked(&mut self, index: usize, value: Self::Value) {
79        // Safety: The caller must ensure that `*self` (the index) is in bounds.
80        unsafe {
81            let elem = self.get_unchecked_mut(index);
82            *elem = value;
83        }
84    }
85}
86
87impl<'a, T: Clone> SliceByValueSubsliceGat<'a> for Vec<T> {
88    type Subslice = &'a [T];
89}
90impl<'a, T: Clone> SliceByValueSubsliceGatMut<'a> for Vec<T> {
91    type Subslice = &'a mut [T];
92}
93
94macro_rules! impl_range_vecs {
95    ($range:ty) => {
96        impl<T: Clone> SliceByValueSubsliceRange<$range> for Vec<T> {
97            #[inline]
98            fn get_subslice(&self, index: $range) -> Option<Subslice<'_, Self>> {
99                // slice.get returns Option<&T>, .copied() converts to Option<T>
100                (*self).get(index)
101            }
102
103            #[inline]
104            fn index_subslice(&self, index: $range) -> Subslice<'_, Self> {
105                &self[index]
106            }
107
108            #[inline]
109            unsafe fn get_subslice_unchecked(&self, index: $range) -> Subslice<'_, Self> {
110                unsafe { (*self).get_unchecked(index) }
111            }
112        }
113        impl<T: Clone> SliceByValueSubsliceRangeMut<$range> for Vec<T> {
114            #[inline]
115            fn get_subslice_mut(&mut self, index: $range) -> Option<SubsliceMut<'_, Self>> {
116                (*self).get_mut(index)
117            }
118
119            #[inline]
120            fn index_subslice_mut(&mut self, index: $range) -> SubsliceMut<'_, Self> {
121                &mut self[index]
122            }
123
124            #[inline]
125            unsafe fn get_subslice_unchecked_mut(
126                &mut self,
127                index: $range,
128            ) -> SubsliceMut<'_, Self> {
129                unsafe { (*self).get_unchecked_mut(index) }
130            }
131        }
132    };
133}
134
135impl_range_vecs!(RangeFull);
136impl_range_vecs!(RangeFrom<usize>);
137impl_range_vecs!(RangeTo<usize>);
138impl_range_vecs!(Range<usize>);
139impl_range_vecs!(RangeInclusive<usize>);
140impl_range_vecs!(RangeToInclusive<usize>);
141
142impl<T: Clone> IterableByValue for Vec<T> {
143    type Item = T;
144    type Iter<'a>
145        = Cloned<core::slice::Iter<'a, T>>
146    where
147        T: 'a;
148
149    fn iter_value(&self) -> Self::Iter<'_> {
150        self.iter().cloned()
151    }
152}
153
154impl<T: Clone> IterableByValueFrom for Vec<T> {
155    type IterFrom<'a>
156        = Cloned<Skip<core::slice::Iter<'a, T>>>
157    where
158        T: 'a;
159
160    fn iter_value_from(&self, from: usize) -> Self::IterFrom<'_> {
161        self.iter().skip(from).cloned()
162    }
163}