compio_buf/
io_vec_buf.rs

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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
use std::ops::{Deref, DerefMut};

use crate::{
    t_alloc, IndexedIter, IoBuf, IoBufMut, IoSlice, IoSliceMut, OwnedIterator, SetBufInit,
};

/// A type that's either owned or borrowed. Like [`Cow`](std::rc::Cow) but
/// without the requirement of [`ToOwned`].
pub enum MaybeOwned<'a, T> {
    /// Owned.
    Owned(T),
    /// Borrowed.
    Borrowed(&'a T),
}

impl<T> Deref for MaybeOwned<'_, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        match self {
            MaybeOwned::Owned(t) => t,
            MaybeOwned::Borrowed(t) => t,
        }
    }
}

/// A type that's either owned or mutably borrowed .
pub enum MaybeOwnedMut<'a, T> {
    /// Owned.
    Owned(T),
    /// Mutably borrowed.
    Borrowed(&'a mut T),
}

impl<T> Deref for MaybeOwnedMut<'_, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        match self {
            MaybeOwnedMut::Owned(t) => t,
            MaybeOwnedMut::Borrowed(t) => t,
        }
    }
}

impl<T> DerefMut for MaybeOwnedMut<'_, T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        match self {
            MaybeOwnedMut::Owned(t) => t,
            MaybeOwnedMut::Borrowed(t) => t,
        }
    }
}

/// A trait for vectored buffers.
pub trait IoVectoredBuf: 'static {
    /// The buffer.
    type Buf: IoBuf;

    /// The owned iterator that wraps `Self`.
    type OwnedIter: OwnedIterator<Inner = Self> + IoBuf;

    /// Collected [`IoSlice`]s of the buffers.
    ///
    /// # Safety
    ///
    /// The return slice will not live longer than self.
    /// It is static to provide convenience from writing self-referenced
    /// structure.
    unsafe fn io_slices(&self) -> Vec<IoSlice> {
        self.iter_buf().map(|buf| buf.as_io_slice()).collect()
    }

    /// An iterator over the buffers that's either owned or borrowed with
    /// [`MaybeOwned`].
    fn iter_buf(&self) -> impl Iterator<Item = MaybeOwned<'_, Self::Buf>>;

    /// Wrap self into an owned iterator.
    fn owned_iter(self) -> Result<Self::OwnedIter, Self>
    where
        Self: Sized;
}

impl<T: IoBuf> IoVectoredBuf for &'static [T] {
    type Buf = T;
    type OwnedIter = IndexedIter<Self>;

    fn iter_buf(&self) -> impl Iterator<Item = MaybeOwned<'_, T>> {
        self.iter().map(MaybeOwned::Borrowed)
    }

    fn owned_iter(self) -> Result<Self::OwnedIter, Self> {
        IndexedIter::new(self)
    }
}

impl<T: IoBuf> IoVectoredBuf for &'static mut [T] {
    type Buf = T;
    type OwnedIter = IndexedIter<Self>;

    fn iter_buf(&self) -> impl Iterator<Item = MaybeOwned<'_, T>> {
        self.iter().map(MaybeOwned::Borrowed)
    }

    fn owned_iter(self) -> Result<Self::OwnedIter, Self> {
        IndexedIter::new(self)
    }
}

impl<T: IoBuf, const N: usize> IoVectoredBuf for [T; N] {
    type Buf = T;
    type OwnedIter = IndexedIter<Self>;

    fn iter_buf(&self) -> impl Iterator<Item = MaybeOwned<'_, T>> {
        self.iter().map(MaybeOwned::Borrowed)
    }

    fn owned_iter(self) -> Result<Self::OwnedIter, Self> {
        IndexedIter::new(self)
    }
}

impl<T: IoBuf, #[cfg(feature = "allocator_api")] A: std::alloc::Allocator + 'static> IoVectoredBuf
    for t_alloc!(Vec, T, A)
{
    type Buf = T;
    type OwnedIter = IndexedIter<Self>;

    fn iter_buf(&self) -> impl Iterator<Item = MaybeOwned<'_, T>> {
        self.iter().map(MaybeOwned::Borrowed)
    }

    fn owned_iter(self) -> Result<Self::OwnedIter, Self> {
        IndexedIter::new(self)
    }
}

#[cfg(feature = "arrayvec")]
impl<T: IoBuf, const N: usize> IoVectoredBuf for arrayvec::ArrayVec<T, N> {
    type Buf = T;
    type OwnedIter = IndexedIter<Self>;

    fn iter_buf(&self) -> impl Iterator<Item = MaybeOwned<'_, T>> {
        self.iter().map(MaybeOwned::Borrowed)
    }

    fn owned_iter(self) -> Result<Self::OwnedIter, Self> {
        IndexedIter::new(self)
    }
}

#[cfg(feature = "smallvec")]
impl<T: IoBuf, const N: usize> IoVectoredBuf for smallvec::SmallVec<[T; N]>
where
    [T; N]: smallvec::Array<Item = T>,
{
    type Buf = T;
    type OwnedIter = IndexedIter<Self>;

    fn iter_buf(&self) -> impl Iterator<Item = MaybeOwned<'_, T>> {
        self.iter().map(MaybeOwned::Borrowed)
    }

    fn owned_iter(self) -> Result<Self::OwnedIter, Self> {
        IndexedIter::new(self)
    }
}

/// A trait for mutable vectored buffers.
pub trait IoVectoredBufMut: IoVectoredBuf<Buf: IoBufMut, OwnedIter: IoBufMut> + SetBufInit {
    /// An iterator for the [`IoSliceMut`]s of the buffers.
    ///
    /// # Safety
    ///
    /// The return slice will not live longer than self.
    /// It is static to provide convenience from writing self-referenced
    /// structure.
    unsafe fn io_slices_mut(&mut self) -> Vec<IoSliceMut> {
        self.iter_buf_mut()
            .map(|mut buf| buf.as_io_slice_mut())
            .collect()
    }

    /// An iterator over the buffers that's either owned or mutably borrowed
    /// with [`MaybeOwnedMut`].
    fn iter_buf_mut(&mut self) -> impl Iterator<Item = MaybeOwnedMut<'_, Self::Buf>>;
}

impl<T: IoBufMut> IoVectoredBufMut for &'static mut [T] {
    fn iter_buf_mut(&mut self) -> impl Iterator<Item = MaybeOwnedMut<'_, Self::Buf>> {
        self.iter_mut().map(MaybeOwnedMut::Borrowed)
    }
}

impl<T: IoBufMut, const N: usize> IoVectoredBufMut for [T; N] {
    fn iter_buf_mut(&mut self) -> impl Iterator<Item = MaybeOwnedMut<'_, Self::Buf>> {
        self.iter_mut().map(MaybeOwnedMut::Borrowed)
    }
}

impl<T: IoBufMut, #[cfg(feature = "allocator_api")] A: std::alloc::Allocator + 'static>
    IoVectoredBufMut for t_alloc!(Vec, T, A)
{
    fn iter_buf_mut(&mut self) -> impl Iterator<Item = MaybeOwnedMut<'_, Self::Buf>> {
        self.iter_mut().map(MaybeOwnedMut::Borrowed)
    }
}

#[cfg(feature = "arrayvec")]
impl<T: IoBufMut, const N: usize> IoVectoredBufMut for arrayvec::ArrayVec<T, N> {
    fn iter_buf_mut(&mut self) -> impl Iterator<Item = MaybeOwnedMut<'_, Self::Buf>> {
        self.iter_mut().map(MaybeOwnedMut::Borrowed)
    }
}

#[cfg(feature = "smallvec")]
impl<T: IoBufMut, const N: usize> IoVectoredBufMut for smallvec::SmallVec<[T; N]>
where
    [T; N]: smallvec::Array<Item = T>,
{
    fn iter_buf_mut(&mut self) -> impl Iterator<Item = MaybeOwnedMut<'_, Self::Buf>> {
        self.iter_mut().map(MaybeOwnedMut::Borrowed)
    }
}