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
//! Functions to work with uninit memory

use core::{ptr, mem};

///Encoding function writing to uninit slice.
///
///# Arguments
///- `src` - Input to encode;
///- `dst` - Output to write;
///
///# Result
///
///Returns `Some` if successful, containing number of bytes written.
///
///Returns `None` if data cannot be encoded due to insufficient buffer size or size calculation overflow happens.
#[inline]
pub fn encode(table: &[u8; 64], src: &[u8], dst: &mut [mem::MaybeUninit<u8>]) -> Option<usize> {
    let mut len = dst.len();
    let dst = unsafe {
        ptr::NonNull::new_unchecked(dst.as_ptr() as *mut u8)
    };
    match unsafe { super::raw::encode(table, src, dst, &mut len) } {
        true => Some(len),
        false => None,
    }
}

///Decoding function writing to uninit slice.
///
///# Arguments
///
///- `src` - Input to decode;
///- `dst` - Output to write;
///
///# Result
///
/// Returns `Some` if successful, containing number of bytes written.
///
/// Returns `None` if data cannot be encoded due to insufficient buffer size or invalid input.
#[inline]
pub fn decode(table: &[u8; 64], src: &[u8], dst: &mut [mem::MaybeUninit<u8>]) -> Option<usize> {
    let mut len = dst.len();
    let dst = unsafe {
        ptr::NonNull::new_unchecked(dst.as_ptr() as *mut u8)
    };
    match unsafe { super::raw::decode(table, src, dst, &mut len) } {
        true => Some(len),
        false => None,
    }
}