iof/write/
separators.rs

1use super::separator::Separator;
2
3/// Separator by.
4pub trait Separators: Copy {
5    /// Separator type.
6    type Separator: Separator;
7
8    /// Residual type.
9    type Residual: Separators;
10
11    /// Split the separator on current dimension and other dimensions.
12    ///
13    /// Please ensure that the separator is not empty.
14    fn split(self) -> (Option<Self::Separator>, Self::Residual);
15}
16
17impl<T: Separator + Copy> Separators for T {
18    type Separator = Self;
19    type Residual = Self;
20
21    #[inline]
22    fn split(self) -> (Option<Self::Separator>, Self::Residual) {
23        (Some(self), self)
24    }
25}
26
27impl<'r, T: Separator, const N: usize> Separators for &'r [T; N] {
28    type Separator = &'r T;
29    type Residual = &'r [T];
30
31    #[inline]
32    fn split(self) -> (Option<Self::Separator>, Self::Residual) {
33        Separators::split(self.as_slice())
34    }
35}
36
37impl<'r, T: Separator> Separators for &'r [T] {
38    type Separator = &'r T;
39    type Residual = &'r [T];
40
41    #[inline]
42    fn split(self) -> (Option<Self::Separator>, Self::Residual) {
43        if let Some((first, residual)) = self.split_first() {
44            (Some(first), residual)
45        } else {
46            (None, self)
47        }
48    }
49}
50
51/// Use default separator.
52#[derive(Clone, Copy, Default)]
53pub struct DefaultSeparator;
54
55impl DefaultSeparator {
56    /// Create a new default separator.
57    #[inline]
58    pub const fn new() -> Self {
59        Self
60    }
61}
62
63impl Separators for DefaultSeparator {
64    type Separator = &'static str;
65    type Residual = Self;
66
67    #[inline]
68    fn split(self) -> (Option<Self::Separator>, Self) {
69        (None, self)
70    }
71}