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 main() -> io::Result<()> {
12hostname::set("potato")?;
13let new_name = hostname::get()?;
14assert_eq!(new_name, OsStr::new("potato"));
15# Ok(())
16# }
17```
18"#
19)]
20#![cfg_attr(
21 not(feature = "set"),
22 doc = r#"
23Get the host name:
24```rust,no_run
25# use std::io;
26# use std::ffi::OsStr;
27# fn main() -> io::Result<()> {
28let name = hostname::get()?;
29println!("{:?}", name);
30# Ok(())
31# }
32```
33"#
34)]
35#![doc(html_root_url = "https://docs.rs/hostname/0.4.1")]
36#![deny(
37 unused,
38 unused_imports,
39 unused_features,
40 bare_trait_objects,
41 missing_debug_implementations,
42 missing_docs,
43 nonstandard_style,
44 dead_code,
45 deprecated,
46 rust_2018_idioms,
47 trivial_casts,
48 unused_import_braces,
49 unused_results
50)]
51#![cfg_attr(docsrs, feature(doc_cfg))]
52
53use cfg_if::cfg_if;
54
55#[cfg(feature = "set")]
56use std::ffi::OsStr;
57use std::ffi::OsString;
58use std::io;
59
60cfg_if! {
61 if #[cfg(any(unix, target_os = "redox"))] {
62 mod nix;
63 use crate::nix as sys;
64 } else if #[cfg(target_os = "windows")] {
65 mod windows;
66 use crate::windows as sys;
67 } else {
68 compile_error!("Unsupported target OS! Create an issue: https://github.com/svartalf/hostname/issues/new");
69 }
70}
71
72pub fn get() -> io::Result<OsString> {
89 sys::get()
90}
91
92#[cfg_attr(
97 feature = "set",
98 doc = r#"
99## Example
100
101```rust,no_run
102# use std::io;
103# fn main() -> io::Result<()> {
104hostname::set("potato")?;
105# Ok(())
106# }
107```
108"#
109)]
110#[cfg(feature = "set")]
123#[cfg_attr(docsrs, doc(cfg(feature = "set")))]
124pub fn set<T>(hostname: T) -> io::Result<()>
125where
126 T: AsRef<OsStr>,
127{
128 sys::set(hostname.as_ref())
129}