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
use std::{
io::IoSlice,
iter::{IntoIterator, Iterator},
mem::MaybeUninit,
};
pub fn init_maybeuninit_io_slices_mut<'a, 'io_slice, Iterable>(
uninit_io_slices: &'a mut [MaybeUninit<IoSlice<'io_slice>>],
iterable: Iterable,
) -> &'a mut [IoSlice<'io_slice>]
where
Iterable: IntoIterator<Item = IoSlice<'io_slice>>,
{
fn inner<'a, 'io_slice>(
uninit_io_slices: &'a mut [MaybeUninit<IoSlice<'io_slice>>],
iterable: &mut dyn Iterator<Item = IoSlice<'io_slice>>,
) -> &'a mut [IoSlice<'io_slice>] {
let mut cnt = 0;
uninit_io_slices
.iter_mut()
.zip(iterable)
.for_each(|(uninit_io_slice, io_slice)| {
uninit_io_slice.write(io_slice);
cnt += 1;
});
unsafe { &mut *((&mut uninit_io_slices[..cnt]) as *mut _ as *mut [IoSlice<'io_slice>]) }
}
inner(uninit_io_slices, &mut iterable.into_iter())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::IoSliceExt;
#[test]
fn test() {
let mut uninit_io_slices = [MaybeUninit::<IoSlice<'_>>::uninit(); 5];
let io_slices = [
IoSlice::new(b"1023x"),
IoSlice::new(b"1qwe"),
IoSlice::new(b"''weqdq"),
IoSlice::new(b"jiasodjx"),
IoSlice::new(b"aqw34f"),
];
assert_io_slices_eq(
init_maybeuninit_io_slices_mut(&mut uninit_io_slices, io_slices),
&io_slices,
);
}
fn assert_io_slices_eq(x: &[IoSlice<'_>], y: &[IoSlice<'_>]) {
assert_eq!(x.len(), y.len());
let x: Vec<&[u8]> = x.iter().copied().map(IoSliceExt::into_inner).collect();
let y: Vec<&[u8]> = y.iter().copied().map(IoSliceExt::into_inner).collect();
assert_eq!(x, y);
}
}