Skip to content

Commit b3d7716

Browse files
committed
Use Result<Option<User|Group>> so it is more ergonimic
Signed-off-by: Otavio Salvador <[email protected]>
1 parent 3b75439 commit b3d7716

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

src/unistd.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2463,7 +2463,7 @@ impl User {
24632463
*mut libc::c_char,
24642464
libc::size_t,
24652465
*mut *mut libc::passwd) -> libc::c_int)
2466-
-> Option<Result<Self>>
2466+
-> Result<Option<Self>>
24672467
{
24682468
let bufsize = match sysconf(SysconfVar::GETPW_R_SIZE_MAX) {
24692469
Ok(Some(n)) => n as usize,
@@ -2482,18 +2482,18 @@ impl User {
24822482

24832483
if error == 0 {
24842484
if res.is_null() {
2485-
return None;
2485+
return Ok(None);
24862486
} else {
24872487
let pwd = unsafe { pwd.assume_init() };
2488-
return Some(Ok(User::from(&pwd)));
2488+
return Ok(Some(User::from(&pwd)));
24892489
}
24902490
} else if Errno::last() == Errno::ERANGE {
24912491
// Trigger the internal buffer resizing logic of `Vec` by requiring
24922492
// more space than the current capacity.
24932493
unsafe { cbuf.set_len(cbuf.capacity()); }
24942494
cbuf.reserve(1);
24952495
} else {
2496-
return Some(Err(Error::Sys(Errno::last())));
2496+
return Err(Error::Sys(Errno::last()));
24972497
}
24982498
}
24992499
}
@@ -2507,11 +2507,11 @@ impl User {
25072507
///
25082508
/// ```
25092509
/// use nix::unistd::{Uid, User};
2510-
/// // Returns an Option<Result<User>>, thus the double unwrap.
2510+
/// // Returns an Result<Option<User>>, thus the double unwrap.
25112511
/// let res = User::from_uid(Uid::from_raw(0)).unwrap().unwrap();
25122512
/// assert!(res.name == "root");
25132513
/// ```
2514-
pub fn from_uid(uid: Uid) -> Option<Result<Self>> {
2514+
pub fn from_uid(uid: Uid) -> Result<Option<Self>> {
25152515
User::from_anything(|pwd, cbuf, cap, res| {
25162516
unsafe { libc::getpwuid_r(uid.0, pwd, cbuf, cap, res) }
25172517
})
@@ -2526,11 +2526,11 @@ impl User {
25262526
///
25272527
/// ```
25282528
/// use nix::unistd::User;
2529-
/// // Returns an Option<Result<User>>, thus the double unwrap.
2529+
/// // Returns an Result<Option<User>>, thus the double unwrap.
25302530
/// let res = User::from_name("root").unwrap().unwrap();
25312531
/// assert!(res.name == "root");
25322532
/// ```
2533-
pub fn from_name(name: &str) -> Option<Result<Self>> {
2533+
pub fn from_name(name: &str) -> Result<Option<Self>> {
25342534
let name = CString::new(name).unwrap();
25352535
User::from_anything(|pwd, cbuf, cap, res| {
25362536
unsafe { libc::getpwnam_r(name.as_ptr(), pwd, cbuf, cap, res) }
@@ -2582,7 +2582,7 @@ impl Group {
25822582
*mut libc::c_char,
25832583
libc::size_t,
25842584
*mut *mut libc::group) -> libc::c_int)
2585-
-> Option<Result<Self>>
2585+
-> Result<Option<Self>>
25862586
{
25872587
let bufsize = match sysconf(SysconfVar::GETGR_R_SIZE_MAX) {
25882588
Ok(Some(n)) => n as usize,
@@ -2601,18 +2601,18 @@ impl Group {
26012601

26022602
if error == 0 {
26032603
if res.is_null() {
2604-
return None;
2604+
return Ok(None);
26052605
} else {
26062606
let grp = unsafe { grp.assume_init() };
2607-
return Some(Ok(Group::from(&grp)));
2607+
return Ok(Some(Group::from(&grp)));
26082608
}
26092609
} else if Errno::last() == Errno::ERANGE {
26102610
// Trigger the internal buffer resizing logic of `Vec` by requiring
26112611
// more space than the current capacity.
26122612
unsafe { cbuf.set_len(cbuf.capacity()); }
26132613
cbuf.reserve(1);
26142614
} else {
2615-
return Some(Err(Error::Sys(Errno::last())));
2615+
return Err(Error::Sys(Errno::last()));
26162616
}
26172617
}
26182618
}
@@ -2628,11 +2628,11 @@ impl Group {
26282628
#[cfg_attr(not(target_os = "linux"), doc = " ```no_run")]
26292629
#[cfg_attr(target_os = "linux", doc = " ```")]
26302630
/// use nix::unistd::{Gid, Group};
2631-
/// // Returns an Option<Result<Group>>, thus the double unwrap.
2631+
/// // Returns an Result<Option<Group>>, thus the double unwrap.
26322632
/// let res = Group::from_gid(Gid::from_raw(0)).unwrap().unwrap();
26332633
/// assert!(res.name == "root");
26342634
/// ```
2635-
pub fn from_gid(gid: Gid) -> Option<Result<Self>> {
2635+
pub fn from_gid(gid: Gid) -> Result<Option<Self>> {
26362636
Group::from_anything(|grp, cbuf, cap, res| {
26372637
unsafe { libc::getgrgid_r(gid.0, grp, cbuf, cap, res) }
26382638
})
@@ -2649,11 +2649,11 @@ impl Group {
26492649
#[cfg_attr(not(target_os = "linux"), doc = " ```no_run")]
26502650
#[cfg_attr(target_os = "linux", doc = " ```")]
26512651
/// use nix::unistd::Group;
2652-
/// // Returns an Option<Result<Group>>, thus the double unwrap.
2652+
/// // Returns an Result<Option<Group>>, thus the double unwrap.
26532653
/// let res = Group::from_name("root").unwrap().unwrap();
26542654
/// assert!(res.name == "root");
26552655
/// ```
2656-
pub fn from_name(name: &str) -> Option<Result<Self>> {
2656+
pub fn from_name(name: &str) -> Result<Option<Self>> {
26572657
let name = CString::new(name).unwrap();
26582658
Group::from_anything(|grp, cbuf, cap, res| {
26592659
unsafe { libc::getgrnam_r(name.as_ptr(), grp, cbuf, cap, res) }

0 commit comments

Comments
 (0)