aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorC. Morgan Hamill <me@cmhamill.org>2015-03-20 15:43:06 +0100
committerC. Morgan Hamill <me@cmhamill.org>2015-03-20 15:48:41 +0100
commit8a9a4314f0a39b3e2ea626e7fdbbbbf20ba8e0e8 (patch)
treee7613f3041bef8ddac09be5285172ac3f2b133d2 /src
parentb94a736f5d4ba73182e1c6b2b1f49a1a718a5216 (diff)
downloadmail-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.rs2
-rw-r--r--src/utils.rs12
2 files changed, 13 insertions, 1 deletions
diff --git a/src/lib.rs b/src/lib.rs
index f7358b7..379be2f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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>;
}