Skip to content

Commit

Permalink
Use cfg_if! macro where possible
Browse files Browse the repository at this point in the history
Signed-off-by: mulhern <[email protected]>
  • Loading branch information
mulkieran committed Nov 25, 2024
1 parent 597f258 commit 2daa2e1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ exclude = [".clippy.toml", ".githooks/*", ".gitignore", ".github/*", "Makefile"]

[dependencies]
bitflags = "2.3.3"
cfg-if = "1.0.0"
nix = {version = "0.29.0", features=["fs", "ioctl", "mount"]}
env_logger="0.11.0"
semver = "1.0.0"
Expand Down
28 changes: 15 additions & 13 deletions src/core/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use std::{fmt, path::Path, str::FromStr};

use cfg_if::cfg_if;
use nix::libc::{dev_t, major, makedev, minor};
use nix::sys::stat::{self, SFlag};

Expand Down Expand Up @@ -71,20 +72,21 @@ impl From<dev_t> for Device {

impl From<Device> for dev_t {
fn from(dev: Device) -> dev_t {
#[cfg(target_os = "android")]
#[allow(clippy::useless_conversion)] // Param types u32 in libc 0.2.133
{
makedev(
dev.major
.try_into()
.expect("value is smaller than max positive i32"),
dev.minor
.try_into()
.expect("value is smaller than max positive i32"),
)
cfg_if! {
if #[cfg(target_os = "android")] {
#[allow(clippy::useless_conversion)] // Param types u32 in libc 0.2.133
makedev(
dev.major
.try_into()
.expect("value is smaller than max positive i32"),
dev.minor
.try_into()
.expect("value is smaller than max positive i32"),
)
} else {
makedev(dev.major, dev.minor)
}
}
#[cfg(not(target_os = "android"))]
makedev(dev.major, dev.minor)
}
}

Expand Down
13 changes: 9 additions & 4 deletions src/core/dm_udev_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use cfg_if::cfg_if;

use crate::{core::dm_ioctl as dmi, result::DmResult};

pub trait UdevSyncAction {
Expand Down Expand Up @@ -527,7 +529,10 @@ pub mod sync_noop {
}
}

#[cfg(target_os = "android")]
pub use self::sync_noop::UdevSync;
#[cfg(not(target_os = "android"))]
pub use self::sync_semaphore::UdevSync;
cfg_if! {
if #[cfg(target_os = "android")] {
pub use self::sync_noop::UdevSync;
} else {
pub use self::sync_semaphore::UdevSync;
}
}

0 comments on commit 2daa2e1

Please sign in to comment.