use std::ffi::{OsStr, CStr, CString};
use std::path::{Path, PathBuf};
use std::os::unix::ffi::OsStrExt;
use {Entry};
pub trait AsPath {
type Buffer: AsRef<CStr>;
fn to_path(self) -> Option<Self::Buffer>;
}
impl<'a> AsPath for &'a Path {
type Buffer = CString;
fn to_path(self) -> Option<CString> {
CString::new(self.as_os_str().as_bytes()).ok()
}
}
impl<'a> AsPath for &'a PathBuf {
type Buffer = CString;
fn to_path(self) -> Option<CString> {
CString::new(self.as_os_str().as_bytes()).ok()
}
}
impl<'a> AsPath for &'a OsStr {
type Buffer = CString;
fn to_path(self) -> Option<CString> {
CString::new(self.as_bytes()).ok()
}
}
impl<'a> AsPath for &'a str {
type Buffer = CString;
fn to_path(self) -> Option<CString> {
CString::new(self.as_bytes()).ok()
}
}
impl<'a> AsPath for &'a String {
type Buffer = CString;
fn to_path(self) -> Option<CString> {
CString::new(self.as_bytes()).ok()
}
}
impl<'a> AsPath for String {
type Buffer = CString;
fn to_path(self) -> Option<CString> {
CString::new(self).ok()
}
}
impl<'a> AsPath for &'a CStr {
type Buffer = &'a CStr;
fn to_path(self) -> Option<&'a CStr> {
Some(self)
}
}
impl<'a> AsPath for &'a Entry {
type Buffer = &'a CStr;
fn to_path(self) -> Option<&'a CStr> {
Some(&self.name)
}
}