aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils.rs
blob: 57e40045ba2f1a08b8150dca2c8d07a9ff6e91e8 (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
40
41
42
43
44
45
46
47
48
49
50
51
use libc;
use std::{ffi, str};
use std::borrow::Cow;
use supercow::{Supercow, DefaultFeatures/*, NonSyncFeatures*/};
use supercow::ext::{BoxedStorage};


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

    fn to_str_unchecked<'a>(&self) -> &'a str;

    fn to_string_lossy<'a>(&self) -> Cow<'a, str>;
}

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())
    }

    fn to_str_unchecked<'a>(&self) -> &'a str {
        unsafe { str::from_utf8_unchecked(ffi::CStr::from_ptr(*self).to_bytes()) }
    }

    fn to_string_lossy<'a>(&self) -> Cow<'a, str> {
        unsafe { ffi::CStr::from_ptr(*self) }.to_string_lossy()
    }
}

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() }
    }
}

// pub type ScopedNonSyncSupercow<'a, OWNED, BORROWED = OWNED> =
//     Supercow<'a, OWNED, BORROWED,
//              Box<NonSyncFeatures<'a> + 'a>,
//              BoxedStorage>;

pub type ScopedPhantomcow<'a, OWNED, BORROWED = OWNED, 
                              SHARED = Box<dyn DefaultFeatures<'a> + 'a>,
                              STORAGE = BoxedStorage> =
    Supercow<'a, OWNED, BORROWED, SHARED, STORAGE, ()>;

pub type ScopedSupercow<'a, OWNED, BORROWED = OWNED, SHARED = Box<dyn DefaultFeatures<'a> + 'a>> =
    Supercow<'a, OWNED, BORROWED, SHARED, BoxedStorage>;