#![warn(clippy::all)]
#![allow(
clippy::float_arithmetic,
clippy::implicit_return,
clippy::needless_return,
clippy::blanket_clippy_restriction_lints,
clippy::pattern_type_mismatch
)]
#![cfg_attr(feature = "safe", forbid(unsafe_code))]
#![cfg_attr(not(feature = "std"), no_std)]
#[inline(always)]
#[rustfmt::skip]
pub fn pmin<T: PartialOrd>(a: T, b: T) -> T { if a < b { a } else { b } }
#[inline(always)]
#[rustfmt::skip]
pub fn pmax<T: PartialOrd>(a: T, b: T) -> T { if a > b { a } else { b } }
#[inline(always)]
#[rustfmt::skip]
pub fn pclamp<T: PartialOrd>(value: T, min: T, max: T) -> T {
pmin(pmax(value, min), max)
}
#[cfg(feature = "std")]
pub use std_utils::*;
#[cfg(feature = "std")]
mod std_utils {
use std::{
convert::AsRef,
env, fs, io,
path::{Path, PathBuf},
};
pub fn counter_string(mut length: usize, separator: char) -> String {
let mut cstr = String::new();
while length > 0 {
let mut tmpstr = separator.to_string();
tmpstr.push_str(&length.to_string().chars().rev().collect::<String>());
if tmpstr.len() > length {
tmpstr = tmpstr[..length].to_string();
}
cstr.push_str(&tmpstr);
length -= tmpstr.len();
}
cstr.chars().rev().collect::<String>()
}
pub fn crate_root<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
let current_path = env::current_dir()?;
let mut root_path = current_path.clone();
for p in current_path.as_path().ancestors() {
let has_cargo = fs::read_dir(p)?
.into_iter()
.any(|p| p.unwrap().file_name() == *"Cargo.toml");
if has_cargo {
return Ok(root_path.join(path.as_ref()));
} else {
root_path.pop();
}
}
Err(io::Error::new(
io::ErrorKind::NotFound,
"Ran out of places to find Cargo.toml",
))
}
pub fn crate_root_string<P: AsRef<Path>>(path: P) -> String {
crate_root(Path::new(path.as_ref())).map_or("".into(), |p| p.to_str().unwrap().to_owned())
}
}
#[inline(always)]
pub fn bx<T>(v: T) -> Box<T> {
Box::new(v)
}
#[macro_export]
macro_rules! iif {
($if: expr ; $true: expr ; $false: expr) => {
if $if {
$true
} else {
$false
}
};
}
#[cfg(test)]
mod tests {
use crate::iif;
#[test]
fn iif() {
assert_eq!('a', iif!(true ; 'a' ; 'b'));
assert_eq!('b', iif!(false ; 'a' ; 'b'));
}
}