Skip to content

Commit

Permalink
Upgrade rust edition and migrate dependices(cfg_if, windows_rs) (#18)
Browse files Browse the repository at this point in the history
* Upgrade rust edition to 2021
* Migrate match_cfg to cfg_if
* Migrate winapi to windows_rs
  • Loading branch information
mokeyish authored Sep 19, 2023
1 parent 8b2b83d commit 7e930e9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 30 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@ authors = [
repository = "https://github.com/svartalf/hostname"
readme = "README.md"
license = "MIT"
edition = "2021"

[features]
default = []
# Enables the `hostname::set` function
set = []

[dependencies]
match_cfg = "^0.1"
cfg-if = "^1.0"

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

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

[dev-dependencies]
version-sync = "0.8"
Expand Down
22 changes: 8 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,22 @@ println!("{:?}", name);
)]
#![allow(unknown_lints, unused_extern_crates)]

#[macro_use]
extern crate match_cfg;

use cfg_if::cfg_if;

#[cfg(feature = "set")]
use std::ffi::OsStr;
use std::ffi::OsString;
use std::io;

match_cfg! {
#[cfg(any(unix, target_os = "redox"))] => {
extern crate libc;

cfg_if! {
if #[cfg(any(unix, target_os = "redox"))] {
mod nix;
use ::nix as sys;
}
#[cfg(target_os = "windows")] => {
extern crate winapi;

use crate::nix as sys;
} else if #[cfg(target_os = "windows")] {
mod windows;
use ::windows as sys;
}
_ => {
use crate::windows as sys;
} else {
compile_error!("Unsupported target OS! Create an issue: https://github.com/svartalf/hostname/issues/new");
}
}
Expand Down
39 changes: 25 additions & 14 deletions src/windows.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,45 @@
use std::io;
use std::ptr;
#[cfg(feature = "set")]
use std::ffi::OsStr;
use std::ffi::OsString;
#[cfg(feature = "set")]
use std::os::windows::ffi::OsStrExt;
use std::os::windows::ffi::OsStringExt;

use winapi::um::sysinfoapi;


use windows::Win32::System::SystemInformation::ComputerNamePhysicalDnsHostname;



pub fn get() -> io::Result<OsString> {
use windows::core::PWSTR;
use windows::Win32::System::SystemInformation::GetComputerNameExW;

let mut size = 0;
unsafe {
// Don't care much about the result here,
// it is guaranteed to return an error,
// since we passed the NULL pointer as a buffer
let result = sysinfoapi::GetComputerNameExW(
sysinfoapi::ComputerNamePhysicalDnsHostname,
ptr::null_mut(),
let result = GetComputerNameExW(
ComputerNamePhysicalDnsHostname,
PWSTR::null(),
&mut size,
);
debug_assert_eq!(result, 0);
debug_assert_eq!(result.0, 0);
};

let mut buffer = Vec::with_capacity(size as usize);

let result = unsafe {
sysinfoapi::GetComputerNameExW(
sysinfoapi::ComputerNamePhysicalDnsHostname,
buffer.as_mut_ptr(),
GetComputerNameExW(
ComputerNamePhysicalDnsHostname,
PWSTR::from_raw(buffer.as_mut_ptr()),
&mut size,
)
};

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

#[cfg(feature = "set")]
pub fn set(hostname: &OsStr) -> io::Result<()> {
use windows::core::PCWSTR;
use windows::Win32::System::SystemInformation::SetComputerNameExW;

let buffer = hostname.encode_wide().collect::<Vec<_>>();

let result = unsafe {
sysinfoapi::SetComputerNameExW(
sysinfoapi::ComputerNamePhysicalDnsHostname,
buffer.as_ptr(),
SetComputerNameExW(
ComputerNamePhysicalDnsHostname,
PCWSTR::from_raw(buffer.as_ptr()),
)
};

if result == 0 {
if !result.as_bool() {
Err(io::Error::last_os_error())
} else {
Ok(())
Expand Down

0 comments on commit 7e930e9

Please sign in to comment.