Use AsRef<Path> for file path
parent
dfd17859ed
commit
ca128f8a96
10
src/media.rs
10
src/media.rs
|
@ -5,7 +5,8 @@
|
||||||
use ffi;
|
use ffi;
|
||||||
use ::{Instance, EventManager};
|
use ::{Instance, EventManager};
|
||||||
use ::enums::{State, Meta, TrackType};
|
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 struct Media {
|
||||||
pub ptr: *mut ffi::libvlc_media_t,
|
pub ptr: *mut ffi::libvlc_media_t,
|
||||||
|
@ -27,8 +28,11 @@ impl Media {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a media for a certain file path.
|
/// Create a media for a certain file path.
|
||||||
pub fn new_path(instance: &Instance, path: &str) -> Option<Media> {
|
pub fn new_path<T: AsRef<Path>>(instance: &Instance, path: T) -> Option<Media> {
|
||||||
let cstr = to_cstr(path);
|
let cstr = match path_to_cstr(path.as_ref()) {
|
||||||
|
Ok(s) => s,
|
||||||
|
Err(_) => { return None; },
|
||||||
|
};
|
||||||
|
|
||||||
unsafe{
|
unsafe{
|
||||||
let p = ffi::libvlc_media_new_path(instance.ptr, cstr.as_ptr());
|
let p = ffi::libvlc_media_new_path(instance.ptr, cstr.as_ptr());
|
||||||
|
|
10
src/tools.rs
10
src/tools.rs
|
@ -2,7 +2,8 @@
|
||||||
// This file is part of vlc-rs.
|
// This file is part of vlc-rs.
|
||||||
// Licensed under the MIT license, see the LICENSE file.
|
// 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 std::borrow::Cow;
|
||||||
use libc::c_char;
|
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())
|
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)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue