diff options
| author | C. Morgan Hamill <me@cmhamill.org> | 2015-03-20 15:43:06 +0100 |
|---|---|---|
| committer | C. Morgan Hamill <me@cmhamill.org> | 2015-03-20 15:48:41 +0100 |
| commit | 8a9a4314f0a39b3e2ea626e7fdbbbbf20ba8e0e8 (patch) | |
| tree | e7613f3041bef8ddac09be5285172ac3f2b133d2 /src | |
| parent | b94a736f5d4ba73182e1c6b2b1f49a1a718a5216 (diff) | |
| download | mail-8a9a4314f0a39b3e2ea626e7fdbbbbf20ba8e0e8.tar.gz | |
Add `ToCString` trait.
Allows easy conversion of any `T: AsOsStr` to a `CString`, whose
`as_ptr()` method is quite convenient for FFI calls.
Note that `std::path::Path` implements `AsOsStr`, which means we can
easily take paths and get a pointer to `*const libc::c_char`.
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib.rs | 2 | ||||
| -rw-r--r-- | src/utils.rs | 12 |
2 files changed, 13 insertions, 1 deletions
@@ -1,4 +1,4 @@ -#![feature(core, libc)] +#![feature(core, libc, std_misc)] extern crate libc; #[macro_use] diff --git a/src/utils.rs b/src/utils.rs index ad8bcac..7e17ab9 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -3,6 +3,8 @@ use std::{ str, }; +use std::os::unix::ffi::OsStrExt; + use libc; pub trait NotmuchEnum { @@ -12,6 +14,16 @@ pub trait NotmuchEnum { fn to_notmuch_t(self) -> Self::NotmuchT; } +pub trait ToCString { + fn to_cstring(&self) -> Result<ffi::CString, ffi::NulError>; +} + +impl<T: ffi::AsOsStr> ToCString for T { + fn to_cstring(&self) -> Result<ffi::CString, ffi::NulError> { + self.as_os_str().to_cstring() + } +} + pub trait ToStr { fn to_str(&self) -> Result<&str, str::Utf8Error>; } |
