1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//! A crate with utilities to get and set the system's host name.
//!
//! ## Examples
//!
//! Set and get the host name:
//!
//! ```rust,no_run
//! # use std::io;
//! # use std::ffi::OsStr;
//! # fn try_main() -> io::Result<()> {
//! hostname::set("potato")?;
//! let new_name = hostname::get()?;
//! assert_eq!(new_name, OsStr::new("potato"));
//! # Ok(())
//! # }
//! # fn main() {
//! # try_main().unwrap();
//! # }
//! ```
extern crate match_cfg;
use OsStr;
use OsString;
use io;
match_cfg!
/// Return the system hostname.
///
/// ## Example
///
/// ```rust
/// # use std::io;
/// # fn try_main() -> io::Result<()> {
/// let name = hostname::get()?;
/// # Ok(())
/// # }
/// # fn main() {
/// # try_main().unwrap();
/// # }
/// ```
///
/// ## Errors
///
/// If this function encounters any form of error, an error
/// variant will be returned; in practice it is rare to be happen.
/// Set the system hostname.
///
/// This function is available only with `set` feature enabled (**disabled** by
/// default).
///
/// ## Example
///
/// ```rust,no_run
/// # use std::io;
/// # fn try_main() -> io::Result<()> {
/// hostname::set("potato")?;
/// # Ok(())
/// # }
/// # fn main() {
/// # try_main().unwrap();
/// # }
/// ```
///
/// ## Errors
///
/// In order to set new hostname, caller might need
/// to have the corresponding privilege
/// (`CAP_SYS_ADMIN` capability for Linux, administrator privileges for Windows,
/// and so on).\
/// An error variant will be returned if this function
/// will encounter a permission error or any other form of error.
///
/// ## Compatibility
///
/// * Will fail with a linkage error for Android API < 23 (see [#9](https://github.com/svartalf/hostname/issues/9#issuecomment-563991112))