Use AsRef<Path> for file path

merge-requests/7/merge
T. Okubo 2016-11-05 12:34:32 +09:00
parent dfd17859ed
commit ca128f8a96
2 changed files with 16 additions and 4 deletions

View File

@ -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<Media> {
let cstr = to_cstr(path);
pub fn new_path<T: AsRef<Path>>(instance: &Instance, path: T) -> Option<Media> {
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());

View File

@ -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<Cow<'a, str>> {
Some(cstr.to_string_lossy())
}
}
// Create CString from &Path
pub fn path_to_cstr(path: &Path) -> Result<CString, NulError> {
let path = try!(CString::new(path.to_string_lossy().into_owned()));
Ok(path)
}