value_traits/impls/
vectors.rs

1/*
2 * SPDX-FileCopyrightText: 2025 Tommaso Fontana
3 * SPDX-FileCopyrightText: 2025 Sebastiano Vigna
4 * SPDX-FileCopyrightText: 2025 Inria
5 *
6 * SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
7 */
8
9//! Implementations of by-value traits for vectors of [cloneable](Clone) types.
10//!
11//! This module is only available if the `alloc` feature is enabled.
12
13#![cfg(feature = "alloc")]
14
15#[cfg(all(feature = "alloc", not(feature = "std")))]
16use alloc::vec::Vec;
17
18use core::{
19    iter::{Cloned, Skip},
20    ops::{Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive},
21};
22
23use crate::{
24    iter::{
25        Iter, IterFrom, IterateByValue, IterateByValueFrom, IterateByValueFromGat,
26        IterateByValueGat,
27    },
28    slices::{
29        SliceByValue, SliceByValueGet, SliceByValueRepl, SliceByValueSet, SliceByValueSubsliceGat,
30        SliceByValueSubsliceGatMut, SliceByValueSubsliceRange, SliceByValueSubsliceRangeMut,
31        Subslice, SubsliceMut,
32    },
33};
34
35impl<T> SliceByValue for Vec<T> {
36    type Value = T;
37    #[inline]
38    fn len(&self) -> usize {
39        <[T]>::len(self)
40    }
41}
42
43impl<T: Clone> SliceByValueGet for Vec<T> {
44    #[inline]
45    fn get_value(&self, index: usize) -> Option<Self::Value> {
46        (*self).get(index).cloned()
47    }
48
49    #[inline]
50    fn index_value(&self, index: usize) -> Self::Value {
51        self[index].clone()
52    }
53
54    #[inline]
55    unsafe fn get_value_unchecked(&self, index: usize) -> Self::Value {
56        // SAFETY: index is within bounds
57        let val_ref = unsafe { (*self).get_unchecked(index) };
58        val_ref.clone()
59    }
60}
61
62impl<T: Clone> SliceByValueRepl for Vec<T> {
63    #[inline]
64    fn replace_value(&mut self, index: usize, value: Self::Value) -> Self::Value {
65        core::mem::replace(&mut self[index], value)
66    }
67
68    #[inline]
69    unsafe fn replace_value_unchecked(&mut self, index: usize, value: Self::Value) -> Self::Value {
70        // SAFETY: index is within bounds
71        let val_mut = unsafe { self.get_unchecked_mut(index) };
72        core::mem::replace(val_mut, value)
73    }
74}
75
76impl<T: Clone> SliceByValueSet for Vec<T> {
77    #[inline]
78    fn set_value(&mut self, index: usize, value: Self::Value) {
79        self[index] = value;
80    }
81
82    #[inline]
83    unsafe fn set_value_unchecked(&mut self, index: usize, value: Self::Value) {
84        // SAFETY: index is within bounds
85        let val_mut = { self.get_unchecked_mut(index) };
86        *val_mut = value;
87    }
88}
89
90impl<'a, T: Clone> SliceByValueSubsliceGat<'a> for Vec<T> {
91    type Subslice = &'a [T];
92}
93impl<'a, T: Clone> SliceByValueSubsliceGatMut<'a> for Vec<T> {
94    type SubsliceMut = &'a mut [T];
95}
96
97macro_rules! impl_range_vecs {
98    ($range:ty) => {
99        impl<T: Clone> SliceByValueSubsliceRange<$range> for Vec<T> {
100            #[inline]
101            fn get_subslice(&self, index: $range) -> Option<Subslice<'_, Self>> {
102                (*self).get(index)
103            }
104
105            #[inline]
106            fn index_subslice(&self, index: $range) -> Subslice<'_, Self> {
107                &self[index]
108            }
109
110            #[inline]
111            unsafe fn get_subslice_unchecked(&self, index: $range) -> Subslice<'_, Self> {
112                unsafe { (*self).get_unchecked(index) }
113            }
114        }
115        impl<T: Clone> SliceByValueSubsliceRangeMut<$range> for Vec<T> {
116            #[inline]
117            fn get_subslice_mut(&mut self, index: $range) -> Option<SubsliceMut<'_, Self>> {
118                (*self).get_mut(index)
119            }
120
121            #[inline]
122            fn index_subslice_mut(&mut self, index: $range) -> SubsliceMut<'_, Self> {
123                &mut self[index]
124            }
125
126            #[inline]
127            unsafe fn get_subslice_unchecked_mut(
128                &mut self,
129                index: $range,
130            ) -> SubsliceMut<'_, Self> {
131                unsafe { (*self).get_unchecked_mut(index) }
132            }
133        }
134    };
135}
136
137impl_range_vecs!(RangeFull);
138impl_range_vecs!(RangeFrom<usize>);
139impl_range_vecs!(RangeTo<usize>);
140impl_range_vecs!(Range<usize>);
141impl_range_vecs!(RangeInclusive<usize>);
142impl_range_vecs!(RangeToInclusive<usize>);
143
144impl<'a, T: Clone> IterateByValueGat<'a> for Vec<T> {
145    type Item = T;
146    type Iter = Cloned<core::slice::Iter<'a, T>>;
147}
148
149impl<T: Clone> IterateByValue for Vec<T> {
150    fn iter_value(&self) -> Iter<'_, Self> {
151        self.iter().cloned()
152    }
153}
154
155impl<'a, T: Clone> IterateByValueFromGat<'a> for Vec<T> {
156    type Item = T;
157    type IterFrom = Cloned<Skip<core::slice::Iter<'a, T>>>;
158}
159
160impl<T: Clone> IterateByValueFrom for Vec<T> {
161    fn iter_value_from(&self, from: usize) -> IterFrom<'_, Self> {
162        self.iter().skip(from).cloned()
163    }
164}