Remove use of MaybeUninit
in ucred.rs
We can simply init the struct directly. There is no real need to use uninit memory here.
This commit is contained in:
parent
ed20eff92b
commit
a9ec61db17
2 changed files with 3 additions and 5 deletions
|
@ -433,6 +433,7 @@ impl UnixStream {
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
|
/// #![feature(peer_credentials_unix_socket)]
|
||||||
/// use std::os::unix::net::UnixStream;
|
/// use std::os::unix::net::UnixStream;
|
||||||
///
|
///
|
||||||
/// fn main() -> std::io::Result<()> {
|
/// fn main() -> std::io::Result<()> {
|
||||||
|
|
|
@ -31,7 +31,6 @@ pub use self::impl_bsd::peer_cred;
|
||||||
#[cfg(any(target_os = "linux", target_os = "android"))]
|
#[cfg(any(target_os = "linux", target_os = "android"))]
|
||||||
pub mod impl_linux {
|
pub mod impl_linux {
|
||||||
use super::UCred;
|
use super::UCred;
|
||||||
use crate::mem::MaybeUninit;
|
|
||||||
use crate::os::unix::io::AsRawFd;
|
use crate::os::unix::io::AsRawFd;
|
||||||
use crate::os::unix::net::UnixStream;
|
use crate::os::unix::net::UnixStream;
|
||||||
use crate::{io, mem};
|
use crate::{io, mem};
|
||||||
|
@ -46,9 +45,9 @@ pub mod impl_linux {
|
||||||
assert!(ucred_size <= u32::max_value() as usize);
|
assert!(ucred_size <= u32::max_value() as usize);
|
||||||
|
|
||||||
let mut ucred_size = ucred_size as u32;
|
let mut ucred_size = ucred_size as u32;
|
||||||
|
let mut ucred: ucred = ucred { pid: 1, uid: 1, gid: 1 };
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let mut ucred: ucred = MaybeUninit::uninit().assume_init();
|
|
||||||
let ret = libc::getsockopt(
|
let ret = libc::getsockopt(
|
||||||
socket.as_raw_fd(),
|
socket.as_raw_fd(),
|
||||||
libc::SOL_SOCKET,
|
libc::SOL_SOCKET,
|
||||||
|
@ -76,14 +75,12 @@ pub mod impl_linux {
|
||||||
pub mod impl_bsd {
|
pub mod impl_bsd {
|
||||||
use super::UCred;
|
use super::UCred;
|
||||||
use crate::io;
|
use crate::io;
|
||||||
use crate::mem::MaybeUninit;
|
|
||||||
use crate::os::unix::io::AsRawFd;
|
use crate::os::unix::io::AsRawFd;
|
||||||
use crate::os::unix::net::UnixStream;
|
use crate::os::unix::net::UnixStream;
|
||||||
|
|
||||||
pub fn peer_cred(socket: &UnixStream) -> io::Result<UCred> {
|
pub fn peer_cred(socket: &UnixStream) -> io::Result<UCred> {
|
||||||
|
let mut cred = UCred { uid: 1, gid: 1 };
|
||||||
unsafe {
|
unsafe {
|
||||||
// Create `cred` and attempt to populate it.
|
|
||||||
let mut cred: UCred = MaybeUninit::uninit().assume_init();
|
|
||||||
let ret = libc::getpeereid(socket.as_raw_fd(), &mut cred.uid, &mut cred.gid);
|
let ret = libc::getpeereid(socket.as_raw_fd(), &mut cred.uid, &mut cred.gid);
|
||||||
|
|
||||||
if ret == 0 { Ok(cred) } else { Err(io::Error::last_os_error()) }
|
if ret == 0 { Ok(cred) } else { Err(io::Error::last_os_error()) }
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue