Skip to content

Commit

Permalink
Cast to per-target major and minor return types.
Browse files Browse the repository at this point in the history
  • Loading branch information
igankevich committed Feb 24, 2025
1 parent f71815c commit 3064c91
Showing 1 changed file with 48 additions and 2 deletions.
50 changes: 48 additions & 2 deletions libc-test/test/makedev.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,50 @@
//! Compare libc's makedev, major, minor functions against the actual C macros, for various
//! inputs.
#[cfg(any(target_os = "solaris", target_os = "illumos"))]
mod ret {
pub type MajorRetType = libc::major_t;
pub type MinorRetType = libc::minor_t;
}

#[cfg(any(
target_os = "linux",
target_os = "l4re",
target_os = "emscripten",
target_os = "fuchsia",
target_os = "aix",
target_os = "nto",
target_os = "hurd",
target_os = "openbsd",
))]
mod ret {
pub type MajorRetType = libc::c_uint;
pub type MinorRetType = libc::c_uint;
}

#[cfg(any(
target_os = "android",
target_os = "dragonfly",
target_os = "netbsd",
target_os = "freebsd",
))]
mod ret {
pub type MajorRetType = libc::c_int;
pub type MinorRetType = libc::c_int;
}

#[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "watchos",
target_os = "visionos"
))]
mod ret {
pub type MajorRetType = i32;
pub type MinorRetType = i32;
}

#[cfg(any(
target_os = "android",
target_os = "dragonfly",
Expand All @@ -14,6 +58,8 @@
mod t {
use libc::{self, c_uint, dev_t};

use super::ret::*;

extern "C" {
pub fn makedev_ffi(major: c_uint, minor: c_uint) -> dev_t;
pub fn major_ffi(dev: dev_t) -> c_uint;
Expand All @@ -24,9 +70,9 @@ mod t {
let dev = unsafe { makedev_ffi(major, minor) };
assert_eq!(libc::makedev(major, minor), dev);
let major = unsafe { major_ffi(dev) };
assert_eq!(libc::major(dev) as i64, major as i64);
assert_eq!(libc::major(dev), major as MajorRetType);
let minor = unsafe { minor_ffi(dev) };
assert_eq!(libc::minor(dev) as i64, minor as i64);
assert_eq!(libc::minor(dev), minor as MinorRetType);
}

// Every OS should be able to handle 8 bit major and minor numbers
Expand Down

0 comments on commit 3064c91

Please sign in to comment.