Skip to content

Commit daf29df

Browse files
ctrlcctrlvdario23
authored andcommitted
Second attempt to cut down on #[cfg] duplication
This time by exiling everything to an inline module. Kudos to Xion_ in IRC for the idea.
1 parent 0fc9338 commit daf29df

File tree

1 file changed

+127
-118
lines changed

1 file changed

+127
-118
lines changed

src/unistd.rs

Lines changed: 127 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ pub use self::pivot_root::*;
2020
#[cfg(any(target_os = "android", target_os = "freebsd",
2121
target_os = "linux", target_os = "openbsd"))]
2222
pub use self::setres::*;
23+
#[cfg(not(any(target_os = "android",
24+
target_os = "ios",
25+
target_os = "macos",
26+
target_env = "musl",
27+
target_arch = "mips",
28+
target_arch = "mips64",
29+
target_arch = "mips64el",
30+
target_arch = "mipsel",
31+
target_arch = "s390x")))]
32+
pub use self::usergroupiter::*;
2333

2434
/// User identifier
2535
///
@@ -2555,145 +2565,144 @@ impl Queryable<GroupQuery> for Group {
25552565
}
25562566
}
25572567

2558-
#[cfg_attr(any(target_os = "android",
2568+
#[cfg(not(any(target_os = "android",
25592569
target_os = "ios",
25602570
target_os = "macos",
25612571
target_env = "musl",
25622572
target_arch = "mips",
25632573
target_arch = "mips64",
25642574
target_arch = "mips64el",
25652575
target_arch = "mipsel",
2566-
target_arch = "s390x",
2567-
), no_getpwent_r)]
2568-
2569-
#[cfg(not(no_getpwent_r))]
2570-
/// This iterator can be used to get all of the users on the system. For example:
2571-
///
2572-
/// ```
2573-
/// use nix::unistd::Users;
2574-
/// eprintln!("Users on this system:\nname\tuid");
2575-
/// Users::new()
2576-
/// .map(|e|e.map(
2577-
/// |pw| println!("{}\t{}",
2578-
/// pw.name,
2579-
/// pw.uid)))
2580-
/// .collect::<Vec<_>>();
2581-
///
2582-
/// ```
2583-
///
2584-
/// This iterator should not be used in different threads without synchronization; while doing so
2585-
/// will not cause undefined behavior, because modern systems lack re-entrant versions of
2586-
/// `setpwent` and `endpwent`, it is very likely that iterators running in different threads will
2587-
/// yield different numbers of items.
2588-
#[derive(Debug)]
2589-
pub struct Users(pub usize);
2576+
target_arch = "s390x")))]
2577+
mod usergroupiter {
2578+
use libc;
2579+
use libc::{c_char};
2580+
use Result;
2581+
use errno::Errno;
2582+
use super::{Error, User, Group, PWGRP_BUFSIZE};
2583+
use std::{mem, ptr};
25902584

2591-
#[cfg(not(no_getpwent_r))]
2592-
impl Users {
2593-
pub fn new() -> Self {
2594-
unsafe { libc::setpwent(); }
2595-
Users(PWGRP_BUFSIZE)
2596-
}
2585+
/// This iterator can be used to get all of the users on the system. For example:
2586+
///
2587+
/// ```
2588+
/// use nix::unistd::Users;
2589+
/// eprintln!("Users on this system:\nname\tuid");
2590+
/// Users::new()
2591+
/// .map(|e|e.map(
2592+
/// |pw| println!("{}\t{}",
2593+
/// pw.name,
2594+
/// pw.uid)))
2595+
/// .collect::<Vec<_>>();
2596+
///
2597+
/// ```
2598+
///
2599+
/// This iterator should not be used in different threads without synchronization; while doing so
2600+
/// will not cause undefined behavior, because modern systems lack re-entrant versions of
2601+
/// `setpwent` and `endpwent`, it is very likely that iterators running in different threads will
2602+
/// yield different numbers of items.
2603+
#[derive(Debug)]
2604+
pub struct Users(pub usize);
2605+
2606+
impl Users {
2607+
pub fn new() -> Self {
2608+
unsafe { libc::setpwent(); }
2609+
Users(PWGRP_BUFSIZE)
2610+
}
25972611

2598-
pub fn with_bufsize(bufsize: usize) -> Self {
2599-
unsafe { libc::setpwent(); }
2600-
Users(bufsize)
2612+
pub fn with_bufsize(bufsize: usize) -> Self {
2613+
unsafe { libc::setpwent(); }
2614+
Users(bufsize)
2615+
}
26012616
}
2602-
}
26032617

2604-
#[cfg(not(no_getpwent_r))]
2605-
impl Iterator for Users {
2606-
type Item = Result<User>;
2607-
fn next(&mut self) -> Option<Result<User>> {
2608-
2609-
let mut cbuf = vec![0 as c_char; self.0];
2610-
let mut pwd: libc::passwd = unsafe { mem::zeroed() };
2611-
let mut res = ptr::null_mut();
2612-
2613-
let i = unsafe {
2614-
Errno::clear();
2615-
libc::getpwent_r(&mut pwd, cbuf.as_mut_ptr(), self.0, &mut res)
2616-
};
2617-
2618-
match i {
2619-
0 if !res.is_null() => {
2620-
unsafe { Some(Ok(User::from(*res))) }
2621-
},
2622-
libc::ERANGE => { Some(Err(Error::Sys(Errno::last()))) },
2623-
_ => None
2618+
impl Iterator for Users {
2619+
type Item = Result<User>;
2620+
fn next(&mut self) -> Option<Result<User>> {
2621+
2622+
let mut cbuf = vec![0 as c_char; self.0];
2623+
let mut pwd: libc::passwd = unsafe { mem::zeroed() };
2624+
let mut res = ptr::null_mut();
2625+
2626+
let i = unsafe {
2627+
Errno::clear();
2628+
libc::getpwent_r(&mut pwd, cbuf.as_mut_ptr(), self.0, &mut res)
2629+
};
2630+
2631+
match i {
2632+
0 if !res.is_null() => {
2633+
unsafe { Some(Ok(User::from(*res))) }
2634+
},
2635+
libc::ERANGE => { Some(Err(Error::Sys(Errno::last()))) },
2636+
_ => None
2637+
}
26242638
}
26252639
}
2626-
}
26272640

2628-
#[cfg(not(no_getpwent_r))]
2629-
impl Drop for Users {
2630-
fn drop(&mut self) {
2631-
unsafe { libc::endpwent() };
2641+
impl Drop for Users {
2642+
fn drop(&mut self) {
2643+
unsafe { libc::endpwent() };
2644+
}
26322645
}
2633-
}
26342646

2635-
#[cfg(not(no_getpwent_r))]
2636-
/// This iterator can be used to get all of the groups on the system. For example:
2637-
///
2638-
/// ```
2639-
/// use nix::unistd::Groups;
2640-
/// eprintln!("Groups on this system:\nname\tgid");
2641-
/// Groups::new()
2642-
/// .map(|e|e.map(
2643-
/// |gr| println!("{}\t{}",
2644-
/// gr.name,
2645-
/// gr.gid)))
2646-
/// .collect::<Vec<_>>();
2647-
///
2648-
/// ```
2649-
///
2650-
/// This iterator should not be used in different threads without synchronization; while doing so
2651-
/// will not cause undefined behavior, because modern systems lack re-entrant versions of
2652-
/// `setgrent` and `endgrent`, it is very likely that iterators running in different threads will
2653-
/// yield different numbers of items.
2654-
#[derive(Debug)]
2655-
pub struct Groups(pub usize);
2656-
2657-
#[cfg(not(no_getpwent_r))]
2658-
impl Groups {
2659-
pub fn new() -> Self {
2660-
unsafe { libc::setgrent(); }
2661-
Groups(PWGRP_BUFSIZE)
2662-
}
2647+
/// This iterator can be used to get all of the groups on the system. For example:
2648+
///
2649+
/// ```
2650+
/// use nix::unistd::Groups;
2651+
/// eprintln!("Groups on this system:\nname\tgid");
2652+
/// Groups::new()
2653+
/// .map(|e|e.map(
2654+
/// |gr| println!("{}\t{}",
2655+
/// gr.name,
2656+
/// gr.gid)))
2657+
/// .collect::<Vec<_>>();
2658+
///
2659+
/// ```
2660+
///
2661+
/// This iterator should not be used in different threads without synchronization; while doing so
2662+
/// will not cause undefined behavior, because modern systems lack re-entrant versions of
2663+
/// `setgrent` and `endgrent`, it is very likely that iterators running in different threads will
2664+
/// yield different numbers of items.
2665+
#[derive(Debug)]
2666+
pub struct Groups(pub usize);
2667+
2668+
impl Groups {
2669+
pub fn new() -> Self {
2670+
unsafe { libc::setgrent(); }
2671+
Groups(PWGRP_BUFSIZE)
2672+
}
26632673

2664-
pub fn with_bufsize(bufsize: usize) -> Self {
2665-
unsafe { libc::setgrent(); }
2666-
Groups(bufsize)
2674+
pub fn with_bufsize(bufsize: usize) -> Self {
2675+
unsafe { libc::setgrent(); }
2676+
Groups(bufsize)
2677+
}
26672678
}
2668-
}
2669-
2670-
#[cfg(not(no_getpwent_r))]
2671-
impl Iterator for Groups {
2672-
type Item = Result<Group>;
2673-
fn next(&mut self) -> Option<Result<Group>> {
2674-
2675-
let mut cbuf = vec![0 as c_char; self.0];
2676-
let mut grp: libc::group = unsafe { mem::zeroed() };
2677-
let mut res = ptr::null_mut();
2678-
2679-
let i = unsafe {
2680-
Errno::clear();
2681-
libc::getgrent_r(&mut grp, cbuf.as_mut_ptr(), self.0, &mut res)
2682-
};
26832679

2684-
match i {
2685-
0 if !res.is_null() => {
2686-
unsafe { Some(Ok(Group::from(*res))) }
2687-
},
2688-
libc::ERANGE => { Some(Err(Error::Sys(Errno::last()))) },
2689-
_ => None
2680+
impl Iterator for Groups {
2681+
type Item = Result<Group>;
2682+
fn next(&mut self) -> Option<Result<Group>> {
2683+
2684+
let mut cbuf = vec![0 as c_char; self.0];
2685+
let mut grp: libc::group = unsafe { mem::zeroed() };
2686+
let mut res = ptr::null_mut();
2687+
2688+
let i = unsafe {
2689+
Errno::clear();
2690+
libc::getgrent_r(&mut grp, cbuf.as_mut_ptr(), self.0, &mut res)
2691+
};
2692+
2693+
match i {
2694+
0 if !res.is_null() => {
2695+
unsafe { Some(Ok(Group::from(*res))) }
2696+
},
2697+
libc::ERANGE => { Some(Err(Error::Sys(Errno::last()))) },
2698+
_ => None
2699+
}
26902700
}
26912701
}
2692-
}
26932702

2694-
#[cfg(not(no_getpwent_r))]
2695-
impl Drop for Groups {
2696-
fn drop(&mut self) {
2697-
unsafe { libc::endgrent() };
2703+
impl Drop for Groups {
2704+
fn drop(&mut self) {
2705+
unsafe { libc::endgrent() };
2706+
}
26982707
}
26992708
}

0 commit comments

Comments
 (0)