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
96
use leveldb_sys as sys;
use std::borrow::Cow;
use std::ffi::CStr;
use std::os::raw::c_char;
use std::str::Utf8Error;

/// A LevelDB CString.
///
/// Like regular CStrings, it is a byte-array with no specified encoding.
/// The length is also stored alongside the pointer, so this supports interior NULs.
pub struct String {
    ptr: *mut c_char,
    len: usize,
}

impl String {
    /// Make a [`String`] from a ptr.
    ///
    /// This will use strlen to determine the string length.
    ///
    /// # Safety
    /// The pointer must be a malloc-ed C string from the leveldb C api.
    ///
    /// # Panics
    /// Panics if the ptr is null.
    pub unsafe fn from_ptr(ptr: *mut c_char) -> Self {
        Self::try_from_ptr(ptr).expect("ptr is null")
    }

    /// Make a [`String`] from a ptr.
    ///
    /// Fallibly make a string from a ptr.
    ///
    /// # Safety
    /// The pointer must be a malloc-ed C string from the leveldb C api.
    pub unsafe fn try_from_ptr(ptr: *mut c_char) -> Option<Self> {
        if ptr.is_null() {
            None
        } else {
            let len = unsafe { CStr::from_ptr(ptr).to_bytes().len() };
            Some(Self { ptr, len })
        }
    }

    /// Make a [`String`] from a ptr and a len, fallibly.
    ///
    /// # Safety
    /// * The pointer must be a malloc-ed C string from the leveldb C api.
    /// * The length must be the length of the string, excluding the nul byte.
    pub unsafe fn try_from_ptr_len(ptr: *mut c_char, len: usize) -> Option<Self> {
        if ptr.is_null() {
            None
        } else {
            Some(Self { ptr, len })
        }
    }

    /// Get the len.
    pub fn len(&self) -> usize {
        self.len
    }

    /// Check if this is empty.
    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    /// Get the contents as a byte slice.
    pub fn as_bytes(&self) -> &[u8] {
        unsafe { std::slice::from_raw_parts(self.ptr.cast(), self.len) }
    }

    /// Try to convert this into a str.
    pub fn to_str(&self) -> Result<&str, Utf8Error> {
        std::str::from_utf8(self.as_bytes())
    }

    /// Lossily convert this into a str.
    pub fn to_string_lossy(&self) -> Cow<str> {
        std::string::String::from_utf8_lossy(self.as_bytes())
    }
}

impl Drop for String {
    fn drop(&mut self) {
        unsafe {
            sys::leveldb_free(self.ptr.cast());
        }
    }
}

impl std::fmt::Debug for String {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "\"{}\"", self.as_bytes().escape_ascii())
    }
}