aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils.rs
blob: 9de7e08a9557144241bf90ac401942ab48874740 (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
38
39
use std::{
    ffi,
    str,
};
use libc;

pub trait FromPtr<T> {
    fn from_ptr(ptr: T) -> Self;
}

// pub trait NewFromPtr<T, P> {
//     fn new(ptr: T, parent: Rc<P>) -> Self;
// }

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

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

pub trait ToString {
    fn to_string(&self) -> String;
}

impl ToString for *const libc::c_char {
    fn to_string(&self) -> String {
        unsafe {
            ffi::CStr::from_ptr(*self).to_string_lossy().into_owned()
        }
    }
}