1#![cfg_attr(
5 feature = "set",
6 doc = r#"
7Set and get the host name:
8```rust,no_run
9# use std::io;
10# use std::ffi::OsStr;
11# fn try_main() -> io::Result<()> {
12hostname::set("potato")?;
13let new_name = hostname::get()?;
14assert_eq!(new_name, OsStr::new("potato"));
15# Ok(())
16# }
17# fn main() {
18# try_main().unwrap();
19# }
20```
21"#
22)]
23#![cfg_attr(
24 not(feature = "set"),
25 doc = r#"
26Get the host name:
27```rust,no_run
28# use std::io;
29# use std::ffi::OsStr;
30# fn try_main() -> io::Result<()> {
31let name = hostname::get()?;
32println!("{:?}", name);
33# Ok(())
34# }
35# fn main() {
36# try_main().unwrap();
37# }
38```
39"#
40)]
41#![doc(html_root_url = "https://docs.rs/hostname/0.3.1")]
42#![deny(
43 unused,
44 unused_imports,
45 unused_features,
46 bare_trait_objects,
47 missing_debug_implementations,
48 missing_docs,
49 nonstandard_style,
50 dead_code,
51 deprecated,
52 rust_2018_idioms,
53 trivial_casts,
54 unused_import_braces,
55 unused_results
56)]
57#![allow(unknown_lints, unused_extern_crates)]
58
59#[macro_use]
60extern crate match_cfg;
61
62#[cfg(feature = "set")]
63use std::ffi::OsStr;
64use std::ffi::OsString;
65use std::io;
66
67match_cfg! {
68 #[cfg(any(unix, target_os = "redox"))] => {
69 extern crate libc;
70
71 mod nix;
72 use ::nix as sys;
73 }
74 #[cfg(target_os = "windows")] => {
75 extern crate winapi;
76
77 mod windows;
78 use ::windows as sys;
79 }
80 _ => {
81 compile_error!("Unsupported target OS! Create an issue: https://github.com/svartalf/hostname/issues/new");
82 }
83}
84
85pub fn get() -> io::Result<OsString> {
105 sys::get()
106}
107
108#[cfg_attr(
113 feature = "set",
114 doc = r#"
115## Example
116
117```rust,no_run
118# use std::io;
119# fn try_main() -> io::Result<()> {
120hostname::set("potato")?;
121# Ok(())
122# }
123# fn main() {
124# try_main().unwrap();
125# }
126```
127"#
128)]
129#[cfg(feature = "set")]
142#[cfg_attr(docsrs, doc(cfg(feature = "set")))]
143pub fn set<T>(hostname: T) -> io::Result<()>
144where
145 T: AsRef<OsStr>,
146{
147 sys::set(hostname.as_ref())
148}