Skip to content

Commit 05edb2f

Browse files
committed
unistd::getgroups: Simplify pointer coercion
1 parent 83d19e2 commit 05edb2f

File tree

1 file changed

+6
-13
lines changed

1 file changed

+6
-13
lines changed

src/unistd.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -954,21 +954,14 @@ pub fn getgroups() -> Result<Vec<Gid>> {
954954
let size = try!(Errno::result(ret));
955955

956956
// Now actually get the groups
957-
let mut groups = Vec::with_capacity(size as usize);
958-
let ret = unsafe { libc::getgroups(size, groups.as_mut_ptr()) };
957+
let mut groups = Vec::<Gid>::with_capacity(size as usize);
958+
// We can coerce a pointer to some `Gid`s as a pointer to some `gid_t`s as
959+
// they have the same representation in memory.
960+
let ret = unsafe { libc::getgroups(size, groups.as_mut_ptr() as *mut gid_t) };
959961

960962
Errno::result(ret).map(|s| {
961-
// We can coerce a pointer to some `gid_t`s as a pointer to some `Gid`s
962-
// as they have the same representation in memory.
963-
// https://doc.rust-lang.org/1.19.0/std/mem/fn.transmute.html#alternatives
964-
let gids = unsafe {
965-
Vec::from_raw_parts(
966-
groups.as_mut_ptr() as *mut Gid,
967-
s as usize,
968-
groups.capacity())
969-
};
970-
mem::forget(groups);
971-
gids
963+
unsafe { groups.set_len(s as usize) };
964+
groups
972965
})
973966
}
974967

0 commit comments

Comments
 (0)