Skip to content

Commit 05b326b

Browse files
committed
refactor: impl EventFd::read/write with eventfd_read/write
1 parent 43505e0 commit 05b326b

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/sys/eventfd.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::errno::Errno;
2-
use crate::{Result,unistd};
3-
use std::os::unix::io::{FromRawFd, OwnedFd, AsRawFd, AsFd, RawFd, BorrowedFd};
2+
use crate::Result;
3+
use libc::eventfd_t;
4+
use std::mem::MaybeUninit;
5+
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, OwnedFd, RawFd};
46

57
libc_bitflags! {
68
pub struct EfdFlags: libc::c_int {
@@ -57,14 +59,20 @@ impl EventFd {
5759
/// The next `value` calls to `poll`, `select` or `epoll` will return immediately.
5860
///
5961
/// [`EventFd::write`] with `value`.
60-
pub fn write(&self, value: u64) -> Result<usize> {
61-
unistd::write(&self.0,&value.to_ne_bytes())
62+
pub fn write(&self, value: eventfd_t) -> Result<usize> {
63+
let res = unsafe { libc::eventfd_write(self.0.as_raw_fd(), value) };
64+
Errno::result(res).map(|res| res as usize)
6265
}
6366
// Reads the value from the file descriptor.
64-
pub fn read(&self) -> Result<u64> {
65-
let mut arr = [0; std::mem::size_of::<u64>()];
66-
unistd::read(self.0.as_raw_fd(),&mut arr)?;
67-
Ok(u64::from_ne_bytes(arr))
67+
pub fn read(&self) -> Result<eventfd_t> {
68+
let mut buf = MaybeUninit::uninit();
69+
let res =
70+
unsafe { libc::eventfd_read(self.0.as_raw_fd(), buf.as_mut_ptr()) };
71+
Errno::result(res)?;
72+
73+
// SAFETY:
74+
// It is guaranteed to be initialized by the `eventfd_read()` function.
75+
Ok(unsafe { buf.assume_init() })
6876
}
6977
}
7078
impl AsFd for EventFd {

0 commit comments

Comments
 (0)