1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use haystack::{Hay, Haystack};
use std::ops::Range;

unsafe impl<T> Hay for [T] {
    type Index = usize;

    #[inline]
    fn empty<'a>() -> &'a Self {
        &[]
    }

    #[inline]
    fn start_index(&self) -> usize {
        0
    }

    #[inline]
    fn end_index(&self) -> usize {
        self.len()
    }

    #[inline]
    unsafe fn slice_unchecked(&self, range: Range<usize>) -> &Self {
        self.get_unchecked(range)
    }

    #[inline]
    unsafe fn next_index(&self, index: Self::Index) -> Self::Index {
        index + 1
    }

    #[inline]
    unsafe fn prev_index(&self, index: Self::Index) -> Self::Index {
        index - 1
    }
}

unsafe impl<'h, T: 'h> Haystack for &'h mut [T] {
    #[inline]
    fn empty() -> Self {
        &mut []
    }

    #[inline]
    unsafe fn slice_unchecked(self, range: Range<usize>) -> Self {
        self.get_unchecked_mut(range)
    }

    #[inline]
    unsafe fn split_around(self, range: Range<usize>) -> [Self; 3] {
        let (haystack, right) = self.split_at_mut(range.end);
        let (left, middle) = haystack.split_at_mut(range.start);
        [left, middle, right]
    }

    #[inline]
    fn restore_range(&self, range: Range<usize>, subrange: Range<usize>) -> Range<usize> {
        (subrange.start + range.start)..(subrange.end + range.start)
    }
}

#[cfg(feature = "std")]
unsafe impl<T> Haystack for Vec<T> {
    #[inline]
    fn empty() -> Self {
        Vec::new()
    }

    #[inline]
    unsafe fn slice_unchecked(mut self, range: Range<usize>) -> Self {
        self.truncate(range.end);
        self.drain(..range.start);
        self
    }

    #[inline]
    unsafe fn split_around(mut self, range: Range<usize>) -> [Self; 3] {
        let right = self.split_off(range.end);
        let middle = self.split_off(range.start);
        [self, middle, right]
    }

    #[inline]
    fn restore_range(&self, range: Range<usize>, subrange: Range<usize>) -> Range<usize> {
        (subrange.start + range.start)..(subrange.end + range.start)
    }
}

mod func;
pub(crate) mod slice;