From ca128f8a963303da8ec295a377e8490481b72b99 Mon Sep 17 00:00:00 2001 From: "T. Okubo" Date: Sat, 5 Nov 2016 12:34:32 +0900 Subject: [PATCH] Use AsRef for file path --- src/media.rs | 10 +++++++--- src/tools.rs | 10 +++++++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/media.rs b/src/media.rs index 6bda0ef..6591b25 100644 --- a/src/media.rs +++ b/src/media.rs @@ -5,7 +5,8 @@ use ffi; use ::{Instance, EventManager}; use ::enums::{State, Meta, TrackType}; -use ::tools::{to_cstr, from_cstr}; +use ::tools::{to_cstr, from_cstr, path_to_cstr}; +use std::path::Path; pub struct Media { pub ptr: *mut ffi::libvlc_media_t, @@ -27,8 +28,11 @@ impl Media { } /// Create a media for a certain file path. - pub fn new_path(instance: &Instance, path: &str) -> Option { - let cstr = to_cstr(path); + pub fn new_path>(instance: &Instance, path: T) -> Option { + let cstr = match path_to_cstr(path.as_ref()) { + Ok(s) => s, + Err(_) => { return None; }, + }; unsafe{ let p = ffi::libvlc_media_new_path(instance.ptr, cstr.as_ptr()); diff --git a/src/tools.rs b/src/tools.rs index 1d27451..71ac32c 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -2,7 +2,8 @@ // This file is part of vlc-rs. // Licensed under the MIT license, see the LICENSE file. -use std::ffi::{CString, CStr}; +use std::ffi::{CString, CStr, NulError}; +use std::path::Path; use std::borrow::Cow; use libc::c_char; @@ -33,3 +34,10 @@ pub unsafe fn from_cstr_ref<'a>(p: *const c_char) -> Option> { Some(cstr.to_string_lossy()) } } + +// Create CString from &Path +pub fn path_to_cstr(path: &Path) -> Result { + let path = try!(CString::new(path.to_string_lossy().into_owned())); + + Ok(path) +}