4
4
//!
5
5
//! ```test_harness,no_run
6
6
//! extern crate open;
7
- //!
7
+ //!
8
8
//! # #[test]
9
9
//! # fn doit() {
10
10
//! open::that("/path/to/file/with.fancy-extension");
11
- //! if open::that("https://google.de ").is_ok() {
11
+ //! if open::that("https://rust-lang.org ").is_ok() {
12
12
//! println!("Look at your browser !");
13
13
//! }
14
14
//! # }
15
15
//! ```
16
16
//!
17
17
//! # Notes
18
18
//! As an operating system program is used, chances are that the open operation fails.
19
- //! Therfore, you are advised to at least check the result with `.is_err()` and
19
+ //! Therfore, you are advised to at least check the result with `.is_err()` and
20
20
//! behave accordingly, e.g. by letting the user know what you tried to open, and failed.
21
21
use std:: io;
22
22
use std:: process:: { Command , ExitStatus } ;
23
+ use std:: ffi:: OsStr ;
23
24
24
25
#[ cfg( not( any( target_os = "windows" , target_os = "macos" ) ) ) ]
25
- pub fn that ( path : & str ) -> io:: Result < ExitStatus > {
26
+ pub fn that < T : AsRef < OsStr > + Sized > ( path : T ) -> io:: Result < ExitStatus > {
26
27
let mut last_err: io:: Result < ExitStatus > = Err ( io:: Error :: from_raw_os_error ( 0 ) ) ;
27
28
for program in & [ "xdg-open" , "gnome-open" , "kde-open" ] {
28
- match Command :: new ( program) . arg ( path) . spawn ( ) {
29
+ match Command :: new ( program) . arg ( path. as_ref ( ) ) . spawn ( ) {
29
30
Ok ( mut child) => return child. wait ( ) ,
30
31
Err ( err) => {
31
32
last_err = Err ( err) ;
@@ -37,11 +38,11 @@ pub fn that(path: &str) -> io::Result<ExitStatus> {
37
38
}
38
39
39
40
#[ cfg( target_os = "windows" ) ]
40
- pub fn that ( path : & str ) -> io:: Result < ExitStatus > {
41
- try!( Command :: new ( "cmd" ) . arg ( "/C" ) . arg ( "start" ) . arg ( path) . spawn ( ) ) . wait ( )
41
+ pub fn that < T : AsRef < OsStr > + Sized > ( path : T ) -> io:: Result < ExitStatus > {
42
+ try!( Command :: new ( "cmd" ) . arg ( "/C" ) . arg ( "start" ) . arg ( path. as_ref ( ) ) . spawn ( ) ) . wait ( )
42
43
}
43
44
44
45
#[ cfg( target_os = "macos" ) ]
45
- pub fn that ( path : & str ) -> io:: Result < ExitStatus > {
46
- try!( Command :: new ( "open" ) . arg ( path) . spawn ( ) ) . wait ( )
46
+ pub fn that < T : AsRef < OsStr > + Sized > ( path : T ) -> io:: Result < ExitStatus > {
47
+ try!( Command :: new ( "open" ) . arg ( path. as_ref ( ) ) . spawn ( ) ) . wait ( )
47
48
}
0 commit comments