From 8a9a4314f0a39b3e2ea626e7fdbbbbf20ba8e0e8 Mon Sep 17 00:00:00 2001 From: "C. Morgan Hamill" Date: Fri, 20 Mar 2015 10:43:06 -0400 Subject: 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`. --- src/lib.rs | 2 +- src/utils.rs | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) (limited to 'src') 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; +} + +impl ToCString for T { + fn to_cstring(&self) -> Result { + self.as_os_str().to_cstring() + } +} + pub trait ToStr { fn to_str(&self) -> Result<&str, str::Utf8Error>; } -- cgit v1.2.1