Skip to content

Commit 97e2d3f

Browse files
authored
Merge pull request #946 from mulkieran/cfg-if
Use cfg_if! macro where possible
2 parents 597f258 + c8e1839 commit 97e2d3f

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ exclude = [".clippy.toml", ".githooks/*", ".gitignore", ".github/*", "Makefile"]
1515

1616
[dependencies]
1717
bitflags = "2.3.3"
18+
cfg-if = "1.0.0"
1819
nix = {version = "0.29.0", features=["fs", "ioctl", "mount"]}
1920
env_logger="0.11.0"
2021
semver = "1.0.0"

src/core/device.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

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

7+
use cfg_if::cfg_if;
78
use nix::libc::{dev_t, major, makedev, minor};
89
use nix::sys::stat::{self, SFlag};
910

@@ -71,20 +72,22 @@ impl From<dev_t> for Device {
7172

7273
impl From<Device> for dev_t {
7374
fn from(dev: Device) -> dev_t {
74-
#[cfg(target_os = "android")]
75-
#[allow(clippy::useless_conversion)] // Param types u32 in libc 0.2.133
76-
{
77-
makedev(
78-
dev.major
79-
.try_into()
80-
.expect("value is smaller than max positive i32"),
81-
dev.minor
82-
.try_into()
83-
.expect("value is smaller than max positive i32"),
84-
)
75+
cfg_if! {
76+
if #[cfg(target_os = "android")] {
77+
// dev.major, dev.minor u32 in libc 0.2.133
78+
#[allow(clippy::useless_conversion)]
79+
makedev(
80+
dev.major
81+
.try_into()
82+
.expect("value is smaller than max positive i32"),
83+
dev.minor
84+
.try_into()
85+
.expect("value is smaller than max positive i32"),
86+
)
87+
} else {
88+
makedev(dev.major, dev.minor)
89+
}
8590
}
86-
#[cfg(not(target_os = "android"))]
87-
makedev(dev.major, dev.minor)
8891
}
8992
}
9093

src/core/dm_udev_sync.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// License, v. 2.0. If a copy of the MPL was not distributed with this
33
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
44

5+
use cfg_if::cfg_if;
6+
57
use crate::{core::dm_ioctl as dmi, result::DmResult};
68

79
pub trait UdevSyncAction {
@@ -527,7 +529,10 @@ pub mod sync_noop {
527529
}
528530
}
529531

530-
#[cfg(target_os = "android")]
531-
pub use self::sync_noop::UdevSync;
532-
#[cfg(not(target_os = "android"))]
533-
pub use self::sync_semaphore::UdevSync;
532+
cfg_if! {
533+
if #[cfg(target_os = "android")] {
534+
pub use self::sync_noop::UdevSync;
535+
} else {
536+
pub use self::sync_semaphore::UdevSync;
537+
}
538+
}

0 commit comments

Comments
 (0)