slice_utils/
interleave.rs1use crate::{Slice, SliceBorrowed, SliceMut, SliceOwned, Unique};
2
3pub struct Interleave<S1, S2>(pub S1, pub S2);
5
6impl<S1, S2> Slice for Interleave<S1, S2>
7where
8 S1: Slice,
9 S2: Slice<Output = S1::Output>,
10{
11 type Output = S1::Output;
12
13 fn len(&self) -> usize {
14 self.0.len() + self.1.len()
15 }
16
17 fn get_with<W: FnMut(&Self::Output) -> R, R>(&self, index: usize, f: &mut W) -> Option<R> {
18 if index % 2 == 0 {
19 self.0.get_with(index / 2, f)
20 } else {
21 self.1.get_with(index / 2, f)
22 }
23 }
24}
25
26impl<S1, S2> SliceOwned for Interleave<S1, S2>
27where
28 S1: SliceOwned,
29 S2: SliceOwned<Output = S1::Output>,
30{
31 fn get_owned(&self, index: usize) -> Option<Self::Output> {
32 if index % 2 == 0 {
33 self.0.get_owned(index / 2)
34 } else {
35 self.1.get_owned(index / 2)
36 }
37 }
38}
39
40impl<S1, S2> SliceBorrowed for Interleave<S1, S2>
41where
42 S1: SliceBorrowed,
43 S2: SliceBorrowed<Output = S1::Output>,
44{
45 fn get(&self, index: usize) -> Option<&Self::Output> {
46 if index % 2 == 0 {
47 self.0.get(index / 2)
48 } else {
49 self.1.get(index / 2)
50 }
51 }
52}
53
54impl<S1, S2> SliceMut for Interleave<S1, S2>
55where
56 S1: SliceMut,
57 S2: SliceMut<Output = S1::Output>,
58{
59 fn get_mut(&mut self, index: usize) -> Option<&mut Self::Output> {
60 if index % 2 == 0 {
61 self.0.get_mut(index / 2)
62 } else {
63 self.1.get_mut(index / 2)
64 }
65 }
66}
67
68unsafe impl<S1, S2> Unique for Interleave<S1, S2>
71where
72 S1: Unique,
73 S2: Unique,
74{
75}