1use crate::prelude::*;
2
3pub trait Input: Sized {
4 type ConstSize<const N: usize>: Sized;
5 type ConstSizeOwned<const N: usize>: Sized + 'static;
6
7 fn len(&self) -> usize;
8
9 #[inline]
10 fn is_empty(&self) -> bool {
11 self.len() == 0
12 }
13
14 #[inline]
15 fn starts_with<N>(&self, needle: &N) -> bool
16 where
17 N: Needle<Self>
18 {
19 needle.input_starts_with(self)
20 }
21
22 fn take_first(&self, i: usize) -> Option<(Self, Self)>;
23 fn take_first_const<const N: usize>(&self) -> Option<(Self::ConstSize<N>, Self)>;
24 fn take_first_const_owned<const N: usize>(&self) -> Option<(Self::ConstSizeOwned<N>, Self)>;
25}
26
27pub trait Needle<I>
28where
29 I: Input
30{
31 fn len(&self) -> usize;
32
33 #[inline]
34 fn is_empty(&self) -> bool {
35 self.len() == 0
36 }
37
38 fn input_starts_with(&self, input: &I) -> bool;
39}
40
41impl<'h> Input for &'h [u8] {
42 type ConstSize<const N: usize> = &'h [u8; N];
43 type ConstSizeOwned<const N: usize> = [u8; N];
44
45 #[inline]
46 fn len(&self) -> usize {
47 (**self).len()
48 }
49
50 #[inline]
51 fn take_first(&self, i: usize) -> Option<(&'h [u8], &'h [u8])> {
52 self.split_at_checked(i)
53 }
54
55 #[inline]
56 fn take_first_const<const N: usize>(&self) -> Option<(Self::ConstSize<N>, Self)> {
57 self.split_at_checked(N).map(|(output, remaining_input)| (
58 unsafe { &*output.as_ptr().cast() },
60 remaining_input
61 ))
62 }
63
64 #[inline]
65 fn take_first_const_owned<const N: usize>(&self) -> Option<(Self::ConstSizeOwned<N>, Self)> {
66 self.take_first_const().map(|(output, remaining_input)| (*output, remaining_input))
67 }
68}
69
70impl<'h> Needle<&'h [u8]> for &[u8] {
71 #[inline]
72 fn len(&self) -> usize {
73 (**self).len()
74 }
75
76 #[inline]
77 fn input_starts_with(&self, input: &&'h [u8]) -> bool {
78 input.starts_with(self)
79 }
80}