Skip to content

Commit 7e930e9

Browse files
authored
Upgrade rust edition and migrate dependices(cfg_if, windows_rs) (#18)
* Upgrade rust edition to 2021 * Migrate match_cfg to cfg_if * Migrate winapi to windows_rs
1 parent 8b2b83d commit 7e930e9

File tree

3 files changed

+36
-30
lines changed

3 files changed

+36
-30
lines changed

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,21 @@ authors = [
1111
repository = "https://github.com/svartalf/hostname"
1212
readme = "README.md"
1313
license = "MIT"
14+
edition = "2021"
1415

1516
[features]
1617
default = []
1718
# Enables the `hostname::set` function
1819
set = []
1920

2021
[dependencies]
21-
match_cfg = "^0.1"
22+
cfg-if = "^1.0"
2223

2324
[target.'cfg(any(unix, target_os = "redox"))'.dependencies]
2425
libc = "^0.2"
2526

2627
[target.'cfg(target_os = "windows")'.dependencies]
27-
winapi = { version = "^0.3", features = ["sysinfoapi"] }
28+
windows = { version="^0.43", features=["Win32_Foundation", "Win32_System_SystemInformation"] }
2829

2930
[dev-dependencies]
3031
version-sync = "0.8"

src/lib.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,22 @@ println!("{:?}", name);
5656
)]
5757
#![allow(unknown_lints, unused_extern_crates)]
5858

59-
#[macro_use]
60-
extern crate match_cfg;
59+
60+
use cfg_if::cfg_if;
6161

6262
#[cfg(feature = "set")]
6363
use std::ffi::OsStr;
6464
use std::ffi::OsString;
6565
use std::io;
6666

67-
match_cfg! {
68-
#[cfg(any(unix, target_os = "redox"))] => {
69-
extern crate libc;
70-
67+
cfg_if! {
68+
if #[cfg(any(unix, target_os = "redox"))] {
7169
mod nix;
72-
use ::nix as sys;
73-
}
74-
#[cfg(target_os = "windows")] => {
75-
extern crate winapi;
76-
70+
use crate::nix as sys;
71+
} else if #[cfg(target_os = "windows")] {
7772
mod windows;
78-
use ::windows as sys;
79-
}
80-
_ => {
73+
use crate::windows as sys;
74+
} else {
8175
compile_error!("Unsupported target OS! Create an issue: https://github.com/svartalf/hostname/issues/new");
8276
}
8377
}

src/windows.rs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,45 @@
11
use std::io;
2-
use std::ptr;
32
#[cfg(feature = "set")]
43
use std::ffi::OsStr;
54
use std::ffi::OsString;
65
#[cfg(feature = "set")]
76
use std::os::windows::ffi::OsStrExt;
87
use std::os::windows::ffi::OsStringExt;
98

10-
use winapi::um::sysinfoapi;
9+
10+
11+
use windows::Win32::System::SystemInformation::ComputerNamePhysicalDnsHostname;
12+
13+
1114

1215
pub fn get() -> io::Result<OsString> {
16+
use windows::core::PWSTR;
17+
use windows::Win32::System::SystemInformation::GetComputerNameExW;
18+
1319
let mut size = 0;
1420
unsafe {
1521
// Don't care much about the result here,
1622
// it is guaranteed to return an error,
1723
// since we passed the NULL pointer as a buffer
18-
let result = sysinfoapi::GetComputerNameExW(
19-
sysinfoapi::ComputerNamePhysicalDnsHostname,
20-
ptr::null_mut(),
24+
let result = GetComputerNameExW(
25+
ComputerNamePhysicalDnsHostname,
26+
PWSTR::null(),
2127
&mut size,
2228
);
23-
debug_assert_eq!(result, 0);
29+
debug_assert_eq!(result.0, 0);
2430
};
2531

2632
let mut buffer = Vec::with_capacity(size as usize);
33+
2734
let result = unsafe {
28-
sysinfoapi::GetComputerNameExW(
29-
sysinfoapi::ComputerNamePhysicalDnsHostname,
30-
buffer.as_mut_ptr(),
35+
GetComputerNameExW(
36+
ComputerNamePhysicalDnsHostname,
37+
PWSTR::from_raw(buffer.as_mut_ptr()),
3138
&mut size,
3239
)
3340
};
3441

35-
if result == 0 {
42+
if !result.as_bool() {
3643
Err(io::Error::last_os_error())
3744
} else {
3845
unsafe {
@@ -45,15 +52,19 @@ pub fn get() -> io::Result<OsString> {
4552

4653
#[cfg(feature = "set")]
4754
pub fn set(hostname: &OsStr) -> io::Result<()> {
55+
use windows::core::PCWSTR;
56+
use windows::Win32::System::SystemInformation::SetComputerNameExW;
57+
4858
let buffer = hostname.encode_wide().collect::<Vec<_>>();
59+
4960
let result = unsafe {
50-
sysinfoapi::SetComputerNameExW(
51-
sysinfoapi::ComputerNamePhysicalDnsHostname,
52-
buffer.as_ptr(),
61+
SetComputerNameExW(
62+
ComputerNamePhysicalDnsHostname,
63+
PCWSTR::from_raw(buffer.as_ptr()),
5364
)
5465
};
5566

56-
if result == 0 {
67+
if !result.as_bool() {
5768
Err(io::Error::last_os_error())
5869
} else {
5970
Ok(())

0 commit comments

Comments
 (0)