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
use MaybeUninit;
/// Returns a mutable `MaybeUninit<u8>` view over the full spare allocation of a `Vec<u8>`.
///
/// The returned slice length is `vec.capacity()`, not `vec.len()`. This is intended for
/// low-level [`crate::NormalizeChunk`] callers that want to let a normalizer write directly into
/// a vector's spare capacity and then set the vector length to the reported initialized length.
///
/// # Safety contract for callers
///
/// This function itself is safe to call, but it is usually paired with an unsafe
/// [`Vec::set_len`] call after normalization. Only call `set_len` with a byte count returned by a
/// successful [`crate::NormalizeChunk::normalize_chunk`] call using this buffer. Reading from the
/// vector's spare capacity, or setting the vector length beyond the number of initialized bytes,
/// is undefined behavior.
///
/// # Example
///
/// ```rust
/// use eolify::{helpers::vec_to_uninit_mut, NormalizeChunk, CRLF};
///
/// let input = b"a\nb";
/// let mut output = Vec::with_capacity(CRLF::max_output_size_for_chunk(input.len(), None, true));
///
/// let status = CRLF::normalize_chunk(input, vec_to_uninit_mut(&mut output), None, true)?;
/// unsafe {
/// output.set_len(status.output_len());
/// }
///
/// assert_eq!(output, b"a\r\nb");
/// # Ok::<(), eolify::Error>(())
/// ```
/// Returns a mutable `MaybeUninit<u8>` view over an initialized byte slice.
///
/// This is primarily useful when calling [`crate::NormalizeChunk::normalize_chunk`] with an
/// already-sized output buffer, such as the fixed buffers used by this crate's I/O wrappers.
/// Unlike [`vec_to_uninit_mut`], the returned slice covers only the existing slice length because
/// a `&mut [u8]` has no spare capacity.
///
/// The bytes may be overwritten by the normalizer. After a successful call, only the first
/// [`crate::NormalizeChunkResult::output_len`] bytes are part of the normalized output.
///
/// # Example
///
/// ```rust
/// use eolify::{helpers::slice_to_uninit_mut, NormalizeChunk, LF};
///
/// let input = b"a\r\nb";
/// let mut output = [0; 8];
///
/// let status = LF::normalize_chunk(input, slice_to_uninit_mut(&mut output), None, true)?;
///
/// assert_eq!(&output[..status.output_len()], b"a\nb");
/// # Ok::<(), eolify::Error>(())
/// ```