diff --git a/cros_async/src/io_ext.rs b/cros_async/src/io_ext.rs index 8c41a6e331..5eeaa3bd56 100644 --- a/cros_async/src/io_ext.rs +++ b/cros_async/src/io_ext.rs @@ -16,7 +16,8 @@ //! `URingExecutor` documentation for an explaination of why. use std::fs::File; -use std::os::unix::io::AsRawFd; +use std::ops::{Deref, DerefMut}; +use std::os::unix::io::{AsRawFd, RawFd}; use std::sync::Arc; use async_trait::async_trait; @@ -119,6 +120,43 @@ impl IntoAsync for File {} impl IntoAsync for UnixSeqpacket {} impl IntoAsync for &UnixSeqpacket {} +/// Simple wrapper struct to implement IntoAsync on foreign types. +pub struct AsyncWrapper(T); + +impl AsyncWrapper { + /// Create a new `AsyncWrapper` that wraps `val`. + pub fn new(val: T) -> Self { + AsyncWrapper(val) + } + + /// Consumes the `AsyncWrapper`, returning the inner struct. + pub fn into_inner(self) -> T { + self.0 + } +} + +impl Deref for AsyncWrapper { + type Target = T; + + fn deref(&self) -> &T { + &self.0 + } +} + +impl DerefMut for AsyncWrapper { + fn deref_mut(&mut self) -> &mut T { + &mut self.0 + } +} + +impl AsRawFd for AsyncWrapper { + fn as_raw_fd(&self) -> RawFd { + self.0.as_raw_fd() + } +} + +impl IntoAsync for AsyncWrapper {} + #[cfg(test)] mod tests { use std::fs::{File, OpenOptions}; diff --git a/cros_async/src/lib.rs b/cros_async/src/lib.rs index 271115bd4f..cfe4baae02 100644 --- a/cros_async/src/lib.rs +++ b/cros_async/src/lib.rs @@ -77,7 +77,8 @@ pub use event::EventAsync; pub use executor::Executor; pub use fd_executor::FdExecutor; pub use io_ext::{ - Error as AsyncError, IntoAsync, IoSourceExt, ReadAsync, Result as AsyncResult, WriteAsync, + AsyncWrapper, Error as AsyncError, IntoAsync, IoSourceExt, ReadAsync, Result as AsyncResult, + WriteAsync, }; pub use mem::{BackingMemory, MemRegion}; pub use poll_source::PollSource;