aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils.rs
blob: ad8bcac12800d33d6e6ea3cc021c321609475483 (plain) (blame)
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
use std::{
    ffi,
    str,
};

use libc;

pub trait NotmuchEnum {
    type NotmuchT;

    fn from_notmuch_t(notmuch_t: Self::NotmuchT) -> Self;
    fn to_notmuch_t(self) -> Self::NotmuchT;
}

pub trait ToStr {
    fn to_str(&self) -> Result<&str, str::Utf8Error>;
}

impl ToStr for *const libc::c_char {
    fn to_str(&self) -> Result<&str, str::Utf8Error> {
        str::from_utf8(unsafe {
            ffi::CStr::from_ptr(*self)
        }.to_bytes())
    }
}

pub trait ToStaticStr {
    fn to_static_str(&self) -> Result<&'static str, str::Utf8Error>;
}

impl ToStaticStr for *const libc::c_char {
    fn to_static_str(&self) -> Result<&'static str, str::Utf8Error> {
        str::from_utf8(unsafe {
            ffi::CStr::from_ptr(*self)
        }.to_bytes())
    }
}