Debian Codegraph @

debian/rust-vendor/hostname/src/lib.rs

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