#![deny(unsafe_code)]
use crate::bindings as C;
use std::error::Error;
use std::fmt;
use std::io;
use std::str::FromStr;
#[allow(clippy::doc_markdown)]
#[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 {
let idx = Self::VALUE_TABLE.iter().position(|v| v == self).unwrap();
write!(f, "Resource::{}", Self::IDENT_TABLE[idx])
}
}
impl FromStr for Resource {
type Err = ParseResourceError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let pos = Self::NAME_TABLE.iter().position(|&name| s == name);
match pos {
Some(idx) => Ok(Self::VALUE_TABLE[idx]),
None => Err(ParseResourceError { _priv: () }),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseResourceError {
_priv: (),
}
impl fmt::Display for ParseResourceError {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "failed to parse Resource")
}
}
impl Error for ParseResourceError {}
macro_rules! declare_resource {
{$($(#[$attr:meta])* $id:ident = $tag:expr => $c_enum:ident,)+} => {
impl Resource{
$(
$(#[$attr])*
pub const $id: Self = Self{ tag: $tag, value: C::$c_enum as u8 };
)+
}
#[allow(unused_doc_comments)]
impl Resource{
const NAME_TABLE: &'static [&'static str] = &[
$(
$(#[$attr])*
{
stringify!($c_enum)
},
)+
];
const VALUE_TABLE: &'static [Self] = &[
$(
$(#[$attr])*
{
Self::$id
},
)+
];
const IDENT_TABLE: &'static [&'static str] = &[
$(
$(#[$attr])*
{
stringify!($id)
},
)+
];
}
#[cfg(test)]
mod tests{
use super::*;
#[allow(unused_comparisons)]
#[allow(unused_doc_comments)]
#[test]
fn name_value(){
$(
$(#[$attr])*
{
assert_eq!(Resource::$id.as_name(), stringify!($c_enum));
assert_eq!(Resource::from_str(stringify!($c_enum)).unwrap(), Resource::$id);
}
)+
}
#[allow(unused_doc_comments)]
#[test]
fn unique_tag(){
use std::collections::HashSet;
let tags = [
$(
$(#[$attr])*
{ $tag },
)+
];
let s: HashSet<u16> = tags.iter().copied().collect();
assert_eq!(s.len(), Resource::NAME_TABLE.len());
}
#[allow(unused_doc_comments)]
#[test]
fn raw_eq(){
$(
$(#[$attr])*
{
assert_eq!(Resource::$id.as_raw(), C::$c_enum);
}
)+
}
#[allow(unused_doc_comments)]
#[test]
fn from_str(){
$(
$(#[$attr])*
{
assert_eq!(Resource::from_str(stringify!($c_enum)), Ok(Resource::$id));
}
)+
assert!(Resource::from_str("asdqwe").is_err());
}
}
};
}
impl Resource {
#[inline]
pub fn set(self, soft: u64, hard: u64) -> io::Result<()> {
super::setrlimit(self, soft, hard)
}
#[inline]
pub fn get(self) -> io::Result<(u64, u64)> {
super::getrlimit(self)
}
#[must_use]
#[allow(clippy::missing_panics_doc)] pub fn as_name(self) -> &'static str {
let idx = Self::VALUE_TABLE.iter().position(|&v| v == self).unwrap();
Self::NAME_TABLE[idx]
}
#[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
}
}
declare_resource! {
AS = 1 => RLIMIT_AS,
CORE = 2 => RLIMIT_CORE,
CPU = 3 => RLIMIT_CPU,
DATA = 4 => RLIMIT_DATA,
FSIZE = 5 => RLIMIT_FSIZE,
KQUEUES = 6 => RLIMIT_KQUEUES,
LOCKS = 7 => RLIMIT_LOCKS,
MEMLOCK = 8 => RLIMIT_MEMLOCK,
MSGQUEUE = 9 => RLIMIT_MSGQUEUE,
NICE = 10 => RLIMIT_NICE,
NOFILE = 11 => RLIMIT_NOFILE,
NOVMON = 12 => RLIMIT_NOVMON,
NPROC = 13 => RLIMIT_NPROC,
NPTS = 14 => RLIMIT_NPTS,
NTHR = 15 => RLIMIT_NTHR,
POSIXLOCKS = 16 => RLIMIT_POSIXLOCKS,
RSS = 17 => RLIMIT_RSS,
RTPRIO = 18 => RLIMIT_RTPRIO,
RTTIME = 19 => RLIMIT_RTTIME,
SBSIZE = 20 => RLIMIT_SBSIZE,
SIGPENDING = 21 => RLIMIT_SIGPENDING,
STACK = 22 => RLIMIT_STACK,
SWAP = 23 => RLIMIT_SWAP,
UMTXP = 24 => RLIMIT_UMTXP,
VMEM = 25 => RLIMIT_VMEM,
}