From d0f339daf2422e4ff5a114294b5710ae82741c8e Mon Sep 17 00:00:00 2001 From: svartalf Date: Sat, 15 Feb 2020 16:12:03 +0300 Subject: [PATCH] Starting full async/await rewrite --- heim-disk/src/sys/unix/usage.rs | 39 ++- heim-disk/src/sys/windows/usage.rs | 40 ++- heim-disk/src/usage.rs | 4 +- heim-host/src/sys/windows/platform.rs | 80 ++--- heim-memory/src/memory.rs | 4 +- heim-memory/src/swap.rs | 4 +- heim-memory/src/sys/linux/memory.rs | 4 +- heim-memory/src/sys/linux/swap.rs | 20 +- heim-memory/src/sys/macos/bindings.rs | 3 + heim-memory/src/sys/macos/memory.rs | 57 ++-- heim-memory/src/sys/macos/swap.rs | 36 +-- heim-memory/src/sys/windows/mod.rs | 14 +- heim-net/Cargo.toml | 1 + heim-net/examples/counters.rs | 3 +- heim-net/examples/nic.rs | 3 +- heim-net/src/sys/linux/counters.rs | 4 +- heim-net/tests/smoke.rs | 6 +- heim-process/Cargo.toml | 1 + heim-process/examples/pids.rs | 3 +- heim-process/examples/processes.rs | 3 +- heim-process/src/os/linux/mod.rs | 10 +- heim-process/src/pids.rs | 10 +- heim-process/src/process/mod.rs | 90 +++--- heim-process/src/sys/linux/pids.rs | 10 +- heim-process/src/sys/linux/process/mod.rs | 194 ++++++------ .../src/sys/linux/process/procfs/command.rs | 17 +- .../src/sys/linux/process/procfs/io.rs | 15 +- .../src/sys/linux/process/procfs/stat.rs | 33 +- .../src/sys/linux/process/procfs/statm.rs | 6 +- heim-process/src/sys/macos/pids.rs | 10 +- heim-process/src/sys/macos/process/command.rs | 37 +-- heim-process/src/sys/macos/process/mod.rs | 109 ++++--- heim-process/src/sys/windows/pids.rs | 70 ++--- .../src/sys/windows/process/command.rs | 7 +- .../src/sys/windows/process/create_time.rs | 23 +- heim-process/src/sys/windows/process/mod.rs | 295 ++++++++---------- heim-process/tests/smoke.rs | 6 +- 37 files changed, 596 insertions(+), 675 deletions(-) diff --git a/heim-disk/src/sys/unix/usage.rs b/heim-disk/src/sys/unix/usage.rs index a880ed47..6a6e0846 100644 --- a/heim-disk/src/sys/unix/usage.rs +++ b/heim-disk/src/sys/unix/usage.rs @@ -56,24 +56,23 @@ impl fmt::Debug for Usage { } } -pub fn usage>(path: T) -> impl Future> { - future::lazy(move |_| { - let path = path - .as_ref() - .to_str() - .ok_or_else(|| io::Error::from(io::ErrorKind::InvalidInput)) - .and_then(|string| { - CString::new(string).map_err(|_| io::Error::from(io::ErrorKind::InvalidInput)) - })?; - - let mut vfs = mem::MaybeUninit::::uninit(); - let result = unsafe { libc::statvfs(path.as_ptr(), vfs.as_mut_ptr()) }; - - if result == 0 { - let vfs = unsafe { vfs.assume_init() }; - Ok(Usage(vfs)) - } else { - Err(Error::last_os_error()) - } - }) +// TODO: It is an internal function, we could monomorphize it and accept `path: &Path` +pub async fn usage>(path: T) -> Result { + let path = path + .as_ref() + .to_str() + .ok_or_else(|| io::Error::from(io::ErrorKind::InvalidInput)) + .and_then(|string| { + CString::new(string).map_err(|_| io::Error::from(io::ErrorKind::InvalidInput)) + })?; + + let mut vfs = mem::MaybeUninit::::uninit(); + let result = unsafe { libc::statvfs(path.as_ptr(), vfs.as_mut_ptr()) }; + + if result == 0 { + let vfs = unsafe { vfs.assume_init() }; + Ok(Usage(vfs)) + } else { + Err(Error::last_os_error()) + } } diff --git a/heim-disk/src/sys/windows/usage.rs b/heim-disk/src/sys/windows/usage.rs index 67248414..c1edec11 100644 --- a/heim-disk/src/sys/windows/usage.rs +++ b/heim-disk/src/sys/windows/usage.rs @@ -47,27 +47,25 @@ impl fmt::Debug for Usage { } } -pub fn usage>(path: T) -> impl Future> { - future::lazy(move |_| { - let path = match widestring::U16CString::from_os_str(path.as_ref()) { - Ok(path) => path, - Err(_) => return Err(io::Error::from(io::ErrorKind::InvalidInput).into()), - }; +pub async fn usage>(path: T) -> Result { + let path = match widestring::U16CString::from_os_str(path.as_ref()) { + Ok(path) => path, + Err(_) => return Err(io::Error::from(io::ErrorKind::InvalidInput).into()), + }; - let mut usage = Usage::default(); - let result = unsafe { - fileapi::GetDiskFreeSpaceExW( - path.as_ptr(), - &mut usage.available, - &mut usage.total, - &mut usage.free, - ) - }; + let mut usage = Usage::default(); + let result = unsafe { + fileapi::GetDiskFreeSpaceExW( + path.as_ptr(), + &mut usage.available, + &mut usage.total, + &mut usage.free, + ) + }; - if result != 0 { - Ok(usage) - } else { - Err(Error::last_os_error()) - } - }) + if result != 0 { + Ok(usage) + } else { + Err(Error::last_os_error()) + } } diff --git a/heim-disk/src/usage.rs b/heim-disk/src/usage.rs index 926b627c..b22a2085 100644 --- a/heim-disk/src/usage.rs +++ b/heim-disk/src/usage.rs @@ -53,9 +53,9 @@ impl fmt::Debug for Usage { /// Returns disk [Usage] statistics about the partition which contains the given `path`. /// /// [Usage]: ./struct.Usage.html -pub fn usage(path: T) -> impl Future> +pub async fn usage(path: T) -> Result where T: AsRef, { - sys::usage(path).map(|res| res.map(Into::into)) + sys::usage(path).await.map(Into::into) } diff --git a/heim-host/src/sys/windows/platform.rs b/heim-host/src/sys/windows/platform.rs index 8eefa8b9..94c260a7 100644 --- a/heim-host/src/sys/windows/platform.rs +++ b/heim-host/src/sys/windows/platform.rs @@ -113,67 +113,67 @@ impl fmt::Debug for Platform { } } -unsafe fn get_native_system_info() -> impl Future> { +fn get_native_system_info() -> SystemInfo { let mut info = mem::MaybeUninit::::uninit(); - sysinfoapi::GetNativeSystemInfo(info.as_mut_ptr()); - let info = info.assume_init(); - - future::ok(info.into()) + unsafe { + sysinfoapi::GetNativeSystemInfo(info.as_mut_ptr()); + info.assume_init().into() + } } -unsafe fn rtl_get_version() -> impl Future> { - // Based on the `platform-info` crate source: - // https://github.com/uutils/platform-info/blob/8fa071f764d55bd8e41a96cf42009da9ae20a650/src/windows.rs - let module = match get_ntdll() { - Ok(module) => module, - Err(e) => return future::err(e), - }; - - let funcname = CStr::from_bytes_with_nul_unchecked(b"RtlGetVersion\0"); - let func = libloaderapi::GetProcAddress(module, funcname.as_ptr()); - if !func.is_null() { - let func: extern "stdcall" fn(*mut winnt::RTL_OSVERSIONINFOEXW) -> ntdef::NTSTATUS = - mem::transmute(func as *const ()); - - let mut osinfo = mem::MaybeUninit::::uninit(); - (*osinfo.as_mut_ptr()).dwOSVersionInfoSize = - mem::size_of::() as minwindef::DWORD; - if func(osinfo.as_mut_ptr()) == ntstatus::STATUS_SUCCESS { - future::ok(osinfo.assume_init()) +fn rtl_get_version() -> Result { + unsafe { + // Based on the `platform-info` crate source: + // https://github.com/uutils/platform-info/blob/8fa071f764d55bd8e41a96cf42009da9ae20a650/src/windows.rs + // TODO: Use `ntapi` crate instead + let module = get_ntdll()?; + + let funcname = CStr::from_bytes_with_nul_unchecked(b"RtlGetVersion\0"); + let func = libloaderapi::GetProcAddress(module, funcname.as_ptr()); + if !func.is_null() { + let func: extern "stdcall" fn(*mut winnt::RTL_OSVERSIONINFOEXW) -> ntdef::NTSTATUS = + mem::transmute(func as *const ()); + + let mut osinfo = mem::MaybeUninit::::uninit(); + (*osinfo.as_mut_ptr()).dwOSVersionInfoSize = + mem::size_of::() as minwindef::DWORD; + if func(osinfo.as_mut_ptr()) == ntstatus::STATUS_SUCCESS { + Ok(osinfo.assume_init()) + } else { + // https://docs.microsoft.com/en-us/windows/desktop/devnotes/rtlgetversion#return-value + unreachable!("RtlGetVersion should just work"); + } } else { - // https://docs.microsoft.com/en-us/windows/desktop/devnotes/rtlgetversion#return-value - unreachable!("RtlGetVersion should just work"); + Err(io::Error::last_os_error().into()) } - } else { - future::err(io::Error::last_os_error().into()) } } -unsafe fn get_computer_name() -> impl Future> { +fn get_computer_name() -> Result { let mut buffer: Vec = Vec::with_capacity((MAX_COMPUTERNAME_LENGTH + 1) as usize); let mut size: minwindef::DWORD = MAX_COMPUTERNAME_LENGTH + 1; - let result = winbase::GetComputerNameW(buffer.as_mut_ptr(), &mut size); + let result = unsafe { winbase::GetComputerNameW(buffer.as_mut_ptr(), &mut size) }; if result == 0 { - future::err(Error::last_os_error()) + Err(Error::last_os_error()) } else { - buffer.set_len(size as usize + 1); + unsafe { + buffer.set_len(size as usize + 1); + } let str = OsString::from_wide(&buffer[..(size as usize)]) .to_string_lossy() .to_string(); - future::ok(str) + Ok(str) } } -pub fn platform() -> impl Future> { - let sysinfo = unsafe { get_native_system_info() }; - let version = unsafe { rtl_get_version() }; - let hostname = unsafe { get_computer_name() }; +pub async fn platform() -> Result { + let version = rtl_get_version()?; - future::try_join3(sysinfo, version, hostname).map_ok(|(sysinfo, version, hostname)| Platform { - sysinfo, + Ok(Platform { + sysinfo: get_native_system_info(), version, - hostname, + hostname: get_computer_name()?, build: format!("{}", version.dwBuildNumber), }) } diff --git a/heim-memory/src/memory.rs b/heim-memory/src/memory.rs index 7984acd5..7e76ea49 100644 --- a/heim-memory/src/memory.rs +++ b/heim-memory/src/memory.rs @@ -47,6 +47,6 @@ impl fmt::Debug for Memory { /// Returns future which will resolve into [Memory] struct. /// /// [Memory]: ./struct.Memory.html -pub fn memory() -> impl future::Future> { - sys::memory().map(|res| res.map(Into::into)) +pub async fn memory() -> Result { + sys::memory().await.map(Into::into) } diff --git a/heim-memory/src/swap.rs b/heim-memory/src/swap.rs index a657d4ee..7e4ff87d 100644 --- a/heim-memory/src/swap.rs +++ b/heim-memory/src/swap.rs @@ -45,6 +45,6 @@ impl fmt::Debug for Swap { /// Returns future which will resolve into [Swap] struct. /// /// [Swap]: ./struct.Swap.html -pub fn swap() -> impl Future> { - sys::swap().map(|res| res.map(Into::into)) +pub async fn swap() -> Result { + sys::swap().await.map(Into::into) } diff --git a/heim-memory/src/sys/linux/memory.rs b/heim-memory/src/sys/linux/memory.rs index b1a7bae5..53a17f91 100644 --- a/heim-memory/src/sys/linux/memory.rs +++ b/heim-memory/src/sys/linux/memory.rs @@ -101,6 +101,6 @@ impl FromStr for Memory { } } -pub fn memory() -> impl Future> { - fs::read_into("/proc/meminfo") +pub async fn memory() -> Result { + fs::read_into("/proc/meminfo").await } diff --git a/heim-memory/src/sys/linux/swap.rs b/heim-memory/src/sys/linux/swap.rs index f347b11a..f6102756 100644 --- a/heim-memory/src/sys/linux/swap.rs +++ b/heim-memory/src/sys/linux/swap.rs @@ -134,16 +134,16 @@ impl Swap { } } -fn vm_stat() -> impl Future> { - fs::read_into(PROC_VMSTAT) +async fn vm_stat() -> Result { + fs::read_into(PROC_VMSTAT).await } -pub fn swap() -> impl Future> { - let meminfo = fs::read_to_string(PROC_MEMINFO); - // TODO: Replace with `try_join` - future::join(meminfo, vm_stat()).then(|result| match result { - (Ok(string), Ok(vm_stat)) => future::ready(Swap::parse_str(&string, vm_stat)), - (Err(e), _) => future::err(e.into()), - (_, Err(e)) => future::err(e), - }) +pub async fn swap() -> Result { + let (meminfo, vm_stat) = future::try_join( + fs::read_to_string(PROC_MEMINFO).map_err(Into::into), + vm_stat(), + ) + .await?; + + Swap::parse_str(&meminfo, vm_stat) } diff --git a/heim-memory/src/sys/macos/bindings.rs b/heim-memory/src/sys/macos/bindings.rs index 869c166a..df29d33f 100644 --- a/heim-memory/src/sys/macos/bindings.rs +++ b/heim-memory/src/sys/macos/bindings.rs @@ -51,6 +51,7 @@ pub struct vm_statistics64 { pub total_uncompressed_pages_in_compressor: u64, } +// TODO: Function should not be marked as `unsafe` itself #[allow(trivial_casts)] pub unsafe fn host_vm_info() -> Result { let port = macos::mach_host_self(); @@ -81,6 +82,7 @@ pub unsafe fn host_vm_info() -> Result { } } +// TODO: Function should not be marked as `unsafe` itself #[allow(trivial_casts)] pub unsafe fn hw_memsize() -> Result { let mut name: [i32; 2] = [CTL_HW, HW_MEMSIZE]; @@ -103,6 +105,7 @@ pub unsafe fn hw_memsize() -> Result { } } +// TODO: Function should not be marked as `unsafe` itself pub unsafe fn vm_swapusage() -> Result { let mut name: [i32; 2] = [CTL_VM, VM_SWAPUSAGE]; let mut value = mem::MaybeUninit::::uninit(); diff --git a/heim-memory/src/sys/macos/memory.rs b/heim-memory/src/sys/macos/memory.rs index 9644cbbc..f176c544 100644 --- a/heim-memory/src/sys/macos/memory.rs +++ b/heim-memory/src/sys/macos/memory.rs @@ -44,37 +44,34 @@ impl Memory { } } -pub fn memory() -> impl Future> { - future::lazy(|_| { - let total = unsafe { bindings::hw_memsize()? }; - let vm_stats = unsafe { bindings::host_vm_info()? }; - let page_size = *PAGE_SIZE; +pub async fn memory() -> Result { + let total = unsafe { bindings::hw_memsize()? }; + let vm_stats = unsafe { bindings::host_vm_info()? }; + let page_size = *PAGE_SIZE; - let total = Information::new::(total); - let available = Information::new::( - u64::from(vm_stats.active_count + vm_stats.free_count) * page_size, - ); - let free = Information::new::( - u64::from(vm_stats.free_count - vm_stats.speculative_count) * page_size, - ); - let used = Information::new::( - u64::from(vm_stats.active_count + vm_stats.wire_count) * page_size, - ); - let active = - Information::new::(u64::from(vm_stats.active_count) * page_size); - let inactive = - Information::new::(u64::from(vm_stats.inactive_count) * page_size); - let wire = - Information::new::(u64::from(vm_stats.wire_count) * page_size); + let total = Information::new::(total); + let available = Information::new::( + u64::from(vm_stats.active_count + vm_stats.free_count) * page_size, + ); + let free = Information::new::( + u64::from(vm_stats.free_count - vm_stats.speculative_count) * page_size, + ); + let used = Information::new::( + u64::from(vm_stats.active_count + vm_stats.wire_count) * page_size, + ); + let active = + Information::new::(u64::from(vm_stats.active_count) * page_size); + let inactive = + Information::new::(u64::from(vm_stats.inactive_count) * page_size); + let wire = Information::new::(u64::from(vm_stats.wire_count) * page_size); - Ok(Memory { - total, - available, - free, - used, - active, - inactive, - wire, - }) + Ok(Memory { + total, + available, + free, + used, + active, + inactive, + wire, }) } diff --git a/heim-memory/src/sys/macos/swap.rs b/heim-memory/src/sys/macos/swap.rs index 3a2506aa..a5654428 100644 --- a/heim-memory/src/sys/macos/swap.rs +++ b/heim-memory/src/sys/macos/swap.rs @@ -35,24 +35,22 @@ impl Swap { } #[allow(clippy::identity_conversion)] -pub fn swap() -> impl Future> { - future::lazy(|_| { - let xsw_usage = unsafe { bindings::vm_swapusage()? }; - let vm_stats = unsafe { bindings::host_vm_info()? }; - let page_size = *PAGE_SIZE; - - let total = Information::new::(u64::from(xsw_usage.xsu_total)); - let used = Information::new::(u64::from(xsw_usage.xsu_used)); - let free = Information::new::(u64::from(xsw_usage.xsu_avail)); - let sin = Information::new::(u64::from(vm_stats.pageins) * page_size); - let sout = Information::new::(u64::from(vm_stats.pageouts) * page_size); - - Ok(Swap { - total, - free, - used, - sin, - sout, - }) +pub async fn swap() -> Result { + let xsw_usage = unsafe { bindings::vm_swapusage()? }; + let vm_stats = unsafe { bindings::host_vm_info()? }; + let page_size = *PAGE_SIZE; + + let total = Information::new::(u64::from(xsw_usage.xsu_total)); + let used = Information::new::(u64::from(xsw_usage.xsu_used)); + let free = Information::new::(u64::from(xsw_usage.xsu_avail)); + let sin = Information::new::(u64::from(vm_stats.pageins) * page_size); + let sout = Information::new::(u64::from(vm_stats.pageouts) * page_size); + + Ok(Swap { + total, + free, + used, + sin, + sout, }) } diff --git a/heim-memory/src/sys/windows/mod.rs b/heim-memory/src/sys/windows/mod.rs index ea969354..b7669ede 100644 --- a/heim-memory/src/sys/windows/mod.rs +++ b/heim-memory/src/sys/windows/mod.rs @@ -62,8 +62,8 @@ impl fmt::Debug for Swap { } } -fn memory_status() -> impl Future> { - future::lazy(|_| unsafe { +async fn memory_status() -> Result { + unsafe { let mut mem_status = mem::MaybeUninit::::uninit(); let length = mem::size_of::() as minwindef::DWORD; (*mem_status.as_mut_ptr()).dwLength = length; @@ -74,13 +74,13 @@ fn memory_status() -> impl Future> { } else { Ok(mem_status.assume_init()) } - }) + } } -pub fn swap() -> impl Future> { - memory_status().map_ok(Swap) +pub async fn swap() -> Result { + memory_status().await.map(Swap) } -pub fn memory() -> impl Future> { - memory_status().map_ok(Memory) +pub async fn memory() -> Result { + memory_status().await.map(Memory) } diff --git a/heim-net/Cargo.toml b/heim-net/Cargo.toml index 1d3c1c6f..b413a9a5 100644 --- a/heim-net/Cargo.toml +++ b/heim-net/Cargo.toml @@ -25,6 +25,7 @@ libc = "~0.2" [dev-dependencies] heim-derive = { version = "0.0.10", path = "../heim-derive" } futures-executor = "^0.3" +pin-utils = "0.1.0-alpha.4" version-sync = "0.8" [target.'cfg(unix)'.dependencies] diff --git a/heim-net/examples/counters.rs b/heim-net/examples/counters.rs index f965a1ca..f3c528b5 100644 --- a/heim-net/examples/counters.rs +++ b/heim-net/examples/counters.rs @@ -3,7 +3,8 @@ use heim_net as net; #[heim_derive::main] async fn main() -> Result<()> { - let mut counters = net::io_counters(); + let counters = net::io_counters(); + pin_utils::pin_mut!(counters); while let Some(counter) = counters.next().await { dbg!(counter?); } diff --git a/heim-net/examples/nic.rs b/heim-net/examples/nic.rs index f9095f3e..c02b262d 100644 --- a/heim-net/examples/nic.rs +++ b/heim-net/examples/nic.rs @@ -3,7 +3,8 @@ use heim_net as net; #[heim_derive::main] async fn main() -> Result<()> { - let mut nic = net::nic(); + let nic = net::nic(); + pin_utils::pin_mut!(nic); while let Some(iface) = nic.next().await { dbg!(iface?); } diff --git a/heim-net/src/sys/linux/counters.rs b/heim-net/src/sys/linux/counters.rs index 8302fbf4..5894b308 100644 --- a/heim-net/src/sys/linux/counters.rs +++ b/heim-net/src/sys/linux/counters.rs @@ -109,12 +109,12 @@ pub fn io_counters() -> impl Stream> { fs::read_lines("/proc/net/dev") .skip(2) .map_err(Error::from) - .and_then(|line| future::ready(IoCounters::from_str(&line))) + .and_then(|line| async move { IoCounters::from_str(&line) }) } pub fn io_counters_for_pid(pid: Pid) -> impl Stream> { fs::read_lines(format!("/proc/{}/net/dev", pid)) .skip(2) .map_err(Error::from) - .and_then(|line| future::ready(IoCounters::from_str(&line))) + .and_then(|line| async move { IoCounters::from_str(&line) }) } diff --git a/heim-net/tests/smoke.rs b/heim-net/tests/smoke.rs index a2573d34..e702b4f8 100644 --- a/heim-net/tests/smoke.rs +++ b/heim-net/tests/smoke.rs @@ -9,7 +9,8 @@ use heim_net::os::windows::IoCountersExt; #[heim_derive::test] async fn smoke_io_counters() { - let mut counters = net::io_counters(); + let counters = net::io_counters(); + pin_utils::pin_mut!(counters); while let Some(counter) = counters.next().await { let counter = counter.unwrap(); @@ -29,7 +30,8 @@ async fn smoke_io_counters() { #[heim_derive::test] async fn smoke_nic() { - let mut nic = net::nic(); + let nic = net::nic(); + pin_utils::pin_mut!(nic); while let Some(iface) = nic.next().await { let iface = iface.unwrap(); diff --git a/heim-process/Cargo.toml b/heim-process/Cargo.toml index 5d65b7b5..f117353c 100644 --- a/heim-process/Cargo.toml +++ b/heim-process/Cargo.toml @@ -23,6 +23,7 @@ libc = "~0.2" lazy_static = "1.3.0" ordered-float = { version = "~1.0", default-features = false } memchr = "~2.3" +async-trait = "~0.1" [target.'cfg(target_os = "linux")'.dependencies] heim-net = { version = "0.0.10", path = "../heim-net" } diff --git a/heim-process/examples/pids.rs b/heim-process/examples/pids.rs index ac886062..f9c78732 100644 --- a/heim-process/examples/pids.rs +++ b/heim-process/examples/pids.rs @@ -3,7 +3,8 @@ use heim_process as process; #[heim_derive::main] async fn main() -> Result<(), process::ProcessError> { - let mut pids = process::pids(); + let pids = process::pids(); + pin_utils::pin_mut!(pids); while let Some(pid) = pids.next().await { let pid = pid?; dbg!(pid); diff --git a/heim-process/examples/processes.rs b/heim-process/examples/processes.rs index 52b8ae76..da2857a1 100644 --- a/heim-process/examples/processes.rs +++ b/heim-process/examples/processes.rs @@ -5,7 +5,8 @@ use heim_process as process; #[heim_derive::main] async fn main() -> Result<(), process::ProcessError> { - let mut processes = process::processes(); + let processes = process::processes(); + pin_utils::pin_mut!(processes); println!( "| {:6} | {:6} | {:10} | {:40} | {:50} |", "pid", "ppid", "status", "name", "exe" diff --git a/heim-process/src/os/linux/mod.rs b/heim-process/src/os/linux/mod.rs index 82dc542b..2f677869 100644 --- a/heim-process/src/os/linux/mod.rs +++ b/heim-process/src/os/linux/mod.rs @@ -1,6 +1,6 @@ //! Linux-specific extensions. -use heim_common::prelude::{BoxFuture, BoxStream}; +use heim_common::prelude::BoxStream; use crate::ProcessResult; @@ -13,12 +13,13 @@ pub use self::memory::MemoryExt; /// Linux-specific extension to [Process] /// /// [Process]: ../../struct.Process.html +#[async_trait::async_trait] pub trait ProcessExt { /// Returns future which resolves into process IO counters. /// /// Since `-> impl Trait` is not allowed yet in the trait methods, /// this method returns boxed `Future`. This behavior will change later. - fn io_counters(&self) -> BoxFuture>; + async fn io_counters(&self) -> ProcessResult; /// Returns stream which yield this process [IO counters] for each network interface. /// @@ -30,9 +31,10 @@ pub trait ProcessExt { } #[cfg(target_os = "linux")] +#[async_trait::async_trait] impl ProcessExt for crate::Process { - fn io_counters(&self) -> BoxFuture> { - self.as_ref().io_counters() + async fn io_counters(&self) -> ProcessResult { + self.as_ref().io_counters().await } fn net_io_counters(&self) -> BoxStream> { diff --git a/heim-process/src/pids.rs b/heim-process/src/pids.rs index 8503d8ca..bdd8fd2e 100644 --- a/heim-process/src/pids.rs +++ b/heim-process/src/pids.rs @@ -1,17 +1,17 @@ -use heim_common::prelude::{Future, Stream}; +use heim_common::prelude::Stream; -use crate::{sys, Pid, ProcessError}; +use crate::{sys, Pid, ProcessResult}; /// Returns stream which yields [Pid]s of processes currently running in the system. /// /// Consequent calls are not guaranteed to return pids in the same order. /// /// [Pid]: type.Pid.html -pub fn pids() -> impl Stream> { +pub fn pids() -> impl Stream> { sys::pids() } /// Returns future which checks if process with passed `pid` is exists. -pub fn pid_exists(pid: Pid) -> impl Future> { - sys::pid_exists(pid) +pub async fn pid_exists(pid: Pid) -> ProcessResult { + sys::pid_exists(pid).await } diff --git a/heim-process/src/process/mod.rs b/heim-process/src/process/mod.rs index b2cd935f..3ece0429 100644 --- a/heim-process/src/process/mod.rs +++ b/heim-process/src/process/mod.rs @@ -34,25 +34,27 @@ impl Process { } /// Returns future which resolves into the process parent pid. - pub fn parent_pid(&self) -> impl Future> { - self.as_ref().parent_pid() + pub async fn parent_pid(&self) -> ProcessResult { + self.as_ref().parent_pid().await } /// Returns future which resolves into the parent [Process]. /// /// [Process]: ./struct.Process.html - pub fn parent(&self) -> impl Future> { - self.parent_pid().and_then(get) + pub async fn parent(&self) -> ProcessResult { + let ppid = self.parent_pid().await?; + + get(ppid).await } /// Returns future which resolves into the process name. - pub fn name(&self) -> impl Future> { - self.as_ref().name() + pub async fn name(&self) -> ProcessResult { + self.as_ref().name().await } /// Returns future which resolves into the process executable as an absolute path. - pub fn exe(&self) -> impl Future> { - self.as_ref().exe() + pub async fn exe(&self) -> ProcessResult { + self.as_ref().exe().await } /// Returns future which resolves into the process command line. @@ -74,8 +76,8 @@ impl Process { /// # Ok(()) /// # } /// ``` - pub fn command(&self) -> impl Future> { - self.as_ref().command().map_ok(Into::into) + pub async fn command(&self) -> ProcessResult { + self.as_ref().command().await.map(Into::into) } /// Returns future which resolves into the process current working directory. @@ -84,26 +86,26 @@ impl Process { /// /// For Windows this method is not implemented yet and will always return an error, /// see [#105](https://github.com/heim-rs/heim/issues/105). - pub fn cwd(&self) -> impl Future> { - self.as_ref().cwd() + pub async fn cwd(&self) -> ProcessResult { + self.as_ref().cwd().await } /// Returns future which resolves into the current process status. - pub fn status(&self) -> impl Future> { - self.as_ref().status() + pub async fn status(&self) -> ProcessResult { + self.as_ref().status().await } /// Returns future which resolves into the process creation time, /// expressed as a [Time] amount since the UNIX epoch. /// /// [Time]: ../units/type.Time.html - pub fn create_time(&self) -> impl Future> { - self.as_ref().create_time() + pub async fn create_time(&self) -> ProcessResult