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
use crate::ffi::RT_NAME_MAX;

use core::str::from_utf8_unchecked;
use cty::c_char;

#[allow(non_camel_case_types)]
pub type c_str = *const c_char;
#[allow(non_camel_case_types)]
pub type c_str_mut = *mut c_char;

type NameArray = [u8; RT_NAME_MAX as usize];

#[derive(Clone, Default)]
pub struct RtName {
    buf: NameArray,
}

impl RtName {
    pub fn new(buf: NameArray) -> Self {
        Self { buf }
    }

    #[inline]
    pub fn as_array_str(&self) -> &[u8] {
        &self.buf
    }
    #[inline]
    pub fn as_mut_array_str(&mut self) -> &mut [u8] {
        &mut self.buf
    }
}

impl From<&str> for RtName {
    fn from(s: &str) -> Self {
        let mut buf = [0; RT_NAME_MAX as usize];
        let src = s.as_bytes();
        let len = s.len().clamp(0, RT_NAME_MAX as usize);
        buf[..len].copy_from_slice(&src[..len]);
        RtName { buf }
    }
}

impl Into<c_str> for RtName {
    #[inline]
    fn into(self) -> c_str {
        self.buf.as_ptr().cast()
    }
}

impl Into<c_str_mut> for RtName {
    #[inline]
    fn into(mut self) -> c_str_mut {
        self.buf.as_mut_ptr().cast()
    }
}

pub struct RtNameRef<'a> {
    name: &'a NameArray,
}

impl<'a> RtNameRef<'a> {
    pub fn new(name: &'a NameArray) -> Self {
        Self { name }
    }
}

impl<'a> From<&'a [u8; RT_NAME_MAX as usize]> for RtNameRef<'a> {
    #[inline]
    fn from(name: &'a [u8; RT_NAME_MAX as usize]) -> Self {
        Self { name }
    }
}

impl<'a> From<&'a [i8; RT_NAME_MAX as usize]> for RtNameRef<'a> {
    #[inline]
    fn from(name: &'a [i8; RT_NAME_MAX as usize]) -> Self {
        Self {
            name: unsafe { &*(name.as_ptr().cast() as *const [u8; RT_NAME_MAX as usize]) },
        }
    }
}

impl<'a> Into<&'a str> for RtNameRef<'a> {
    #[inline]
    fn into(self) -> &'a str {
        unsafe { from_utf8_unchecked(self.name) }
    }
}

impl<'a> AsRef<str> for RtNameRef<'a> {
    #[inline]
    fn as_ref(&self) -> &str {
        unsafe { from_utf8_unchecked(self.name) }
    }
}