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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use crate::*;

/// The inner implementation of a [`OwnedIter`].
pub trait OwnedIterator: IntoInner + Sized {
    /// Get the next iterator. Will return Err with the inner buffer if it
    /// reaches the end.
    fn next(self) -> Result<Self, Self::Inner>;

    /// Get the current buffer.
    fn current(&self) -> &dyn IoBuf;
}

/// The mutable part of inner implementation of a [`OwnedIter`].
pub trait OwnedIteratorMut: OwnedIterator {
    /// Get the current mutable buffer.
    fn current_mut(&mut self) -> &mut dyn IoBufMut;
}

/// An owned buffer iterator for vectored buffers.
/// See [`IoVectoredBuf::owned_iter`].
#[derive(Debug)]
pub struct OwnedIter<I: OwnedIterator>(I);

impl<I: OwnedIterator> OwnedIter<I> {
    /// Create [`OwnedIter`] from inner impls.
    pub fn new(inner: I) -> Self {
        Self(inner)
    }

    /// Get the next buffer. Will return Err with the inner buffer if it reaches
    /// the end.
    pub fn next(self) -> Result<Self, I::Inner> {
        self.0.next().map(Self::new)
    }
}

impl<I: OwnedIterator> IntoInner for OwnedIter<I> {
    type Inner = I::Inner;

    fn into_inner(self) -> Self::Inner {
        self.0.into_inner()
    }
}

unsafe impl<I: OwnedIterator + 'static> IoBuf for OwnedIter<I> {
    fn as_buf_ptr(&self) -> *const u8 {
        self.0.current().as_buf_ptr()
    }

    fn buf_len(&self) -> usize {
        self.0.current().buf_len()
    }

    fn buf_capacity(&self) -> usize {
        self.0.current().buf_capacity()
    }
}

unsafe impl<I: OwnedIteratorMut + 'static> IoBufMut for OwnedIter<I> {
    fn as_buf_mut_ptr(&mut self) -> *mut u8 {
        self.0.current_mut().as_buf_mut_ptr()
    }
}

impl<I: OwnedIteratorMut + 'static> SetBufInit for OwnedIter<I> {
    unsafe fn set_buf_init(&mut self, len: usize) {
        self.0.current_mut().set_buf_init(len)
    }
}

/// An owned buffer iterator for vectored buffers.
/// See [`IoVectoredBuf::owned_iter`].
pub(crate) struct IndexedIter<T> {
    bufs: T,
    nth: usize,
}

impl<T: IoIndexedBuf> IndexedIter<T> {
    pub(crate) fn new(bufs: T, nth: usize) -> Result<Self, T> {
        if bufs.buf_nth(nth).is_none() {
            Err(bufs)
        } else {
            Ok(Self { bufs, nth })
        }
    }
}

impl<T: IoIndexedBuf> OwnedIterator for IndexedIter<T> {
    fn next(self) -> Result<Self, Self::Inner> {
        Self::new(self.bufs, self.nth + 1)
    }

    fn current(&self) -> &dyn IoBuf {
        self.bufs
            .buf_nth(self.nth)
            .expect("the nth buf should exist")
    }
}

impl<T: IoIndexedBufMut> OwnedIteratorMut for IndexedIter<T> {
    fn current_mut(&mut self) -> &mut dyn IoBufMut {
        self.bufs
            .buf_nth_mut(self.nth)
            .expect("the nth buf should exist")
    }
}

impl<T> IntoInner for IndexedIter<T> {
    type Inner = T;

    fn into_inner(self) -> Self::Inner {
        self.bufs
    }
}