#![cfg_attr(not(feature = "std"), no_std)]
macro_rules! doctest {
($x:expr) => {
#[doc = $x]
extern {}
};
}
doctest!(include_str!("../README.md"));
#[cfg_attr(unix, path = "./unix.rs")]
mod imp;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Priority(imp::Priority);
impl Priority {
pub fn normal() -> Self {
Self(imp::Priority::normal())
}
pub fn higher(&self) -> impl Iterator<Item = Self> {
self.0.higher().map(Self)
}
pub fn lower(&self) -> impl Iterator<Item = Self> {
self.0.lower().map(Self)
}
}
#[derive(Debug)]
pub struct Process<'a>(imp::Process<'a>);
impl Process<'_> {
pub fn current() -> Process<'static> {
Process(imp::Process::current())
}
pub fn set_priority(&mut self, priority: Priority) -> Result<(), Unchanged> {
self.0.set_priority(priority.0)
}
pub fn priority(&self) -> Result<Priority, NotFound> {
self.0.priority().map(Priority)
}
}
#[cfg(feature = "std")]
impl<'a> From<&'a mut std::process::Child> for Process<'a> {
fn from(child: &'a mut std::process::Child) -> Self {
Self(child.into())
}
}
#[derive(Debug)]
pub struct NotFound;
#[derive(Debug)]
pub enum Unchanged {
NotFound(NotFound),
PermissionDenied,
}
impl From<NotFound> for Unchanged {
fn from(n: NotFound) -> Self {
Self::NotFound(n)
}
}
impl core::fmt::Display for NotFound {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
f.write_str("couldn't set priority of missing process")
}
}
impl core::fmt::Display for Unchanged {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
match self {
Self::NotFound(n) => core::fmt::Display::fmt(n, f),
Self::PermissionDenied => f.write_str("missing permissions to set priority"),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for NotFound {}
#[cfg(feature = "std")]
impl std::error::Error for Unchanged {}