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 crate::*;
use std::{
    borrow::Cow,
    fmt::{Debug, Formatter},
    iter::FromIterator,
    ptr,
};

/// Wrapper for a `*const *const libc::c_char` with a terminating null pointer
pub struct UstrPtr<'a> {
    ustrs: Vec<Cow<'a, Ustr>>,
    ptrs: Vec<*const c::c_char>,
}

impl<'a> UstrPtr<'a> {
    /// Creates a new `UstrPtr`
    pub fn new() -> Self {
        Self {
            ustrs: vec![],
            ptrs: vec![ptr::null()],
        }
    }

    /// Appends a `*const libc::c_char`
    pub fn push(&mut self, s: impl IntoUstr<'a>) {
        let s = s.into_ustr();
        self.ustrs.reserve_exact(1);
        self.ptrs.reserve_exact(1);
        self.ptrs.pop();
        self.ptrs.push(s.as_ptr());
        self.ptrs.push(ptr::null());
        self.ustrs.push(s);
    }

    /// Returns the `*const *const c::c_char`
    pub fn as_ptr(&self) -> &*const c::c_char {
        &self.ptrs[0]
    }
}

impl<'a, T: IntoUstr<'a>> Extend<T> for UstrPtr<'a> {
    fn extend<U: IntoIterator<Item = T>>(&mut self, iter: U) {
        for ustr in iter {
            self.push(ustr);
        }
    }
}

impl<'a, T: IntoUstr<'a>> FromIterator<T> for UstrPtr<'a> {
    fn from_iter<U: IntoIterator<Item = T>>(iter: U) -> Self {
        let mut buf = UstrPtr::new();
        buf.extend(iter);
        buf
    }
}

impl<'a> Default for UstrPtr<'a> {
    fn default() -> Self {
        UstrPtr::new()
    }
}

impl<'a> Debug for UstrPtr<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        Debug::fmt(&self.ustrs, f)
    }
}