mod generated;
use crate::{getrlimit, setrlimit};
use std::fmt;
use std::io;
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Resource {
tag: u8,
value: u8,
}
impl fmt::Debug for Resource {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match Self::find_ident_by_tag(self.tag) {
Some(ident) => write!(f, "Resource::{ident}"),
None => unreachable!(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseResourceError(());
impl fmt::Display for ParseResourceError {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Failed to parse Resource")
}
}
impl std::error::Error for ParseResourceError {}
impl Resource {
#[inline]
pub fn set(self, soft: u64, hard: u64) -> io::Result<()> {
setrlimit(self, soft, hard)
}
#[inline]
pub fn get(self) -> io::Result<(u64, u64)> {
getrlimit(self)
}
pub fn get_soft(self) -> io::Result<u64> {
self.get().map(|(soft, _)| soft)
}
pub fn get_hard(self) -> io::Result<u64> {
self.get().map(|(_, hard)| hard)
}
#[must_use]
pub fn as_name(self) -> &'static str {
match Self::find_name_by_tag(self.tag) {
Some(name) => name,
None => unreachable!(),
}
}
#[must_use]
pub const fn is_supported(self) -> bool {
self.value != u8::MAX
}
#[inline]
#[must_use]
pub(crate) const fn as_raw(self) -> u8 {
self.value
}
}