Skip to content

Commit

Permalink
Rename SockAddr::init to try_init
Browse files Browse the repository at this point in the history
To match the function signature, which returns a result. Also allows to
add an init function that never fails, if ever needed.
  • Loading branch information
Thomasdezeeuw committed Aug 14, 2022
1 parent 14be1bc commit 057f6c2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/sockaddr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl SockAddr {
///
/// // Initialise a `SocketAddr` byte calling `getsockname(2)`.
/// let (_, address) = unsafe {
/// SockAddr::init(|addr_storage, len| {
/// SockAddr::try_init(|addr_storage, len| {
/// // The `getsockname(2)` system call will intiliase `storage` for
/// // us, setting `len` to the correct length.
/// if libc::getsockname(socket.as_raw_fd(), addr_storage.cast(), len) == -1 {
Expand All @@ -116,7 +116,7 @@ impl SockAddr {
/// # Ok(())
/// # }
/// ```
pub unsafe fn init<F, T>(init: F) -> io::Result<(T, SockAddr)>
pub unsafe fn try_init<F, T>(init: F) -> io::Result<(T, SockAddr)>
where
F: FnOnce(*mut sockaddr_storage, *mut socklen_t) -> io::Result<T>,
{
Expand Down
28 changes: 14 additions & 14 deletions src/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,9 @@ impl SockAddr {
P: AsRef<Path>,
{
unsafe {
SockAddr::init(|storage, len| {
// Safety: `SockAddr::init` zeros the address, which is a valid
// representation.
SockAddr::try_init(|storage, len| {
// Safety: `SockAddr::try_init` zeros the address, which is a
// valid representation.
let storage: &mut libc::sockaddr_un = unsafe { &mut *storage.cast() };
let len: &mut socklen_t = unsafe { &mut *len };

Expand All @@ -498,8 +498,8 @@ impl SockAddr {
storage.sun_family = libc::AF_UNIX as sa_family_t;
// Safety: `bytes` and `addr.sun_path` are not overlapping and
// both point to valid memory.
// `SockAddr::init` zeroes the memory, so the path is already
// null terminated.
// `SockAddr::try_init` zeroes the memory, so the path is
// already null terminated.
unsafe {
ptr::copy_nonoverlapping(
bytes.as_ptr(),
Expand Down Expand Up @@ -539,9 +539,9 @@ impl SockAddr {
)]
pub fn vsock(cid: u32, port: u32) -> io::Result<SockAddr> {
unsafe {
SockAddr::init(|storage, len| {
// Safety: `SockAddr::init` zeros the address, which is a valid
// representation.
SockAddr::try_init(|storage, len| {
// Safety: `SockAddr::try_init` zeros the address, which is a
// valid representation.
let storage: &mut libc::sockaddr_vm = unsafe { &mut *storage.cast() };
let len: &mut socklen_t = unsafe { &mut *len };

Expand Down Expand Up @@ -670,18 +670,18 @@ pub(crate) fn listen(fd: Socket, backlog: c_int) -> io::Result<()> {

pub(crate) fn accept(fd: Socket) -> io::Result<(Socket, SockAddr)> {
// Safety: `accept` initialises the `SockAddr` for us.
unsafe { SockAddr::init(|storage, len| syscall!(accept(fd, storage.cast(), len))) }
unsafe { SockAddr::try_init(|storage, len| syscall!(accept(fd, storage.cast(), len))) }
}

pub(crate) fn getsockname(fd: Socket) -> io::Result<SockAddr> {
// Safety: `accept` initialises the `SockAddr` for us.
unsafe { SockAddr::init(|storage, len| syscall!(getsockname(fd, storage.cast(), len))) }
unsafe { SockAddr::try_init(|storage, len| syscall!(getsockname(fd, storage.cast(), len))) }
.map(|(_, addr)| addr)
}

pub(crate) fn getpeername(fd: Socket) -> io::Result<SockAddr> {
// Safety: `accept` initialises the `SockAddr` for us.
unsafe { SockAddr::init(|storage, len| syscall!(getpeername(fd, storage.cast(), len))) }
unsafe { SockAddr::try_init(|storage, len| syscall!(getpeername(fd, storage.cast(), len))) }
.map(|(_, addr)| addr)
}

Expand Down Expand Up @@ -723,7 +723,7 @@ pub(crate) fn recv_from(
) -> io::Result<(usize, SockAddr)> {
// Safety: `recvfrom` initialises the `SockAddr` for us.
unsafe {
SockAddr::init(|addr, addrlen| {
SockAddr::try_init(|addr, addrlen| {
syscall!(recvfrom(
fd,
buf.as_mut_ptr().cast(),
Expand Down Expand Up @@ -755,7 +755,7 @@ pub(crate) fn recv_from_vectored(
// Safety: `recvmsg` initialises the address storage and we set the length
// manually.
unsafe {
SockAddr::init(|storage, len| {
SockAddr::try_init(|storage, len| {
recvmsg(fd, storage, bufs, flags).map(|(n, addrlen, recv_flags)| {
// Set the correct address length.
*len = addrlen;
Expand Down Expand Up @@ -1100,7 +1100,7 @@ impl crate::Socket {
pub(crate) fn _accept4(&self, flags: c_int) -> io::Result<(crate::Socket, SockAddr)> {
// Safety: `accept4` initialises the `SockAddr` for us.
unsafe {
SockAddr::init(|storage, len| {
SockAddr::try_init(|storage, len| {
syscall!(accept4(self.as_raw(), storage.cast(), len, flags))
.map(crate::Socket::from_raw)
})
Expand Down
10 changes: 5 additions & 5 deletions src/sys/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ pub(crate) fn listen(socket: Socket, backlog: c_int) -> io::Result<()> {
pub(crate) fn accept(socket: Socket) -> io::Result<(Socket, SockAddr)> {
// Safety: `accept` initialises the `SockAddr` for us.
unsafe {
SockAddr::init(|storage, len| {
SockAddr::try_init(|storage, len| {
syscall!(
accept(socket, storage.cast(), len),
PartialEq::eq,
Expand All @@ -324,7 +324,7 @@ pub(crate) fn accept(socket: Socket) -> io::Result<(Socket, SockAddr)> {
pub(crate) fn getsockname(socket: Socket) -> io::Result<SockAddr> {
// Safety: `getsockname` initialises the `SockAddr` for us.
unsafe {
SockAddr::init(|storage, len| {
SockAddr::try_init(|storage, len| {
syscall!(
getsockname(socket, storage.cast(), len),
PartialEq::eq,
Expand All @@ -338,7 +338,7 @@ pub(crate) fn getsockname(socket: Socket) -> io::Result<SockAddr> {
pub(crate) fn getpeername(socket: Socket) -> io::Result<SockAddr> {
// Safety: `getpeername` initialises the `SockAddr` for us.
unsafe {
SockAddr::init(|storage, len| {
SockAddr::try_init(|storage, len| {
syscall!(
getpeername(socket, storage.cast(), len),
PartialEq::eq,
Expand Down Expand Up @@ -443,7 +443,7 @@ pub(crate) fn recv_from(
) -> io::Result<(usize, SockAddr)> {
// Safety: `recvfrom` initialises the `SockAddr` for us.
unsafe {
SockAddr::init(|storage, addrlen| {
SockAddr::try_init(|storage, addrlen| {
let res = syscall!(
recvfrom(
socket,
Expand Down Expand Up @@ -472,7 +472,7 @@ pub(crate) fn recv_from_vectored(
) -> io::Result<(usize, RecvFlags, SockAddr)> {
// Safety: `recvfrom` initialises the `SockAddr` for us.
unsafe {
SockAddr::init(|storage, addrlen| {
SockAddr::try_init(|storage, addrlen| {
let mut nread = 0;
let mut flags = flags as u32;
let res = syscall!(
Expand Down

0 comments on commit 057f6c2

Please sign in to comment.