#[macro_export]
macro_rules! newtype {
($(#[$meta:meta])* $vis:vis $name:ident($inner:ty)) => {
#[derive(Debug, Clone, PartialEq)]
$(#[$meta])*
$vis struct $name(pub $inner);
impl $name {
#[inline]
$vis fn new(value: $inner) -> Self {
$name(value)
}
#[inline]
$vis fn into_inner(self) -> $inner {
self.0
}
}
impl ::core::convert::From<$inner> for $name {
#[inline]
fn from(value: $inner) -> Self {
$name(value)
}
}
};
}
pub mod fs {
use std::io;
use std::path::Path;
pub fn read_to_string(path: &Path) -> io::Result<String> {
std::fs::read_to_string(path)
}
pub fn write_text(path: &Path, contents: &str) -> io::Result<()> {
std::fs::write(path, contents)
}
pub fn write_bytes(path: &Path, bytes: &[u8]) -> io::Result<()> {
std::fs::write(path, bytes)
}
pub fn create_dir_all(path: &Path) -> io::Result<()> {
std::fs::create_dir_all(path)
}
pub fn remove_file(path: &Path) -> io::Result<()> {
std::fs::remove_file(path)
}
pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
std::fs::copy(from, to)
}
pub fn rename(from: &Path, to: &Path) -> io::Result<()> {
std::fs::rename(from, to)
}
}
pub mod time {
use std::time::Duration;
pub fn duration(secs: u64, nanos: u32) -> Duration {
Duration::new(secs, nanos)
}
pub fn millis(value: u64) -> Duration {
Duration::from_millis(value)
}
}
pub mod env {
use std::ffi::OsString;
pub fn set_var(name: &str, value: &str) {
unsafe {
std::env::set_var(name, value);
}
}
pub fn var(name: &str) -> Option<OsString> {
std::env::var_os(name)
}
}
pub mod thread {
use std::time::Duration;
pub fn sleep(duration: Duration) {
std::thread::sleep(duration);
}
}
pub mod collections {
use std::collections::{BTreeMap, HashMap, HashSet};
use std::hash::Hash;
pub fn insert<K: Eq + Hash, V>(map: &mut HashMap<K, V>, key: K, value: V) -> Option<V> {
map.insert(key, value)
}
pub fn hashmap_new<K, V>() -> HashMap<K, V> {
HashMap::new()
}
pub fn hashmap_with_capacity<K, V>(capacity: usize) -> HashMap<K, V> {
HashMap::with_capacity(capacity)
}
pub fn hashmap_insert<K: Eq + Hash, V>(map: &mut HashMap<K, V>, key: K, value: V) -> Option<V> {
map.insert(key, value)
}
pub fn btreemap_new<K, V>() -> BTreeMap<K, V> {
BTreeMap::new()
}
pub fn hashset_new<T>() -> HashSet<T> {
HashSet::new()
}
}
pub mod net {
use std::io;
use std::net::{TcpListener, TcpStream, UdpSocket};
pub fn tcp_listener_bind(addr: &str) -> io::Result<TcpListener> {
TcpListener::bind(addr)
}
pub fn tcp_connect(addr: &str) -> io::Result<TcpStream> {
TcpStream::connect(addr)
}
pub fn udp_socket_bind(addr: &str) -> io::Result<UdpSocket> {
UdpSocket::bind(addr)
}
}
pub mod sync {
use std::sync::mpsc::{self, Receiver, Sender};
use std::sync::{Arc, Mutex, RwLock};
pub fn mutex_new<T>(value: T) -> Mutex<T> {
Mutex::new(value)
}
pub fn rwlock_new<T>(value: T) -> RwLock<T> {
RwLock::new(value)
}
pub fn arc_new<T>(value: T) -> Arc<T> {
Arc::new(value)
}
pub fn channel<T>() -> (Sender<T>, Receiver<T>) {
mpsc::channel()
}
}
pub mod process {
use std::process::Command;
pub fn command(program: &str) -> Command {
Command::new(program)
}
pub fn command_arg<'a>(cmd: &'a mut Command, arg: &str) -> &'a mut Command {
cmd.arg(arg)
}
pub fn command_env<'a>(cmd: &'a mut Command, key: &str, value: &str) -> &'a mut Command {
cmd.env(key, value)
}
pub fn exit(code: i32) -> ! {
std::process::exit(code)
}
}
pub mod string {
pub fn string_new() -> String {
String::new()
}
pub fn string_with_capacity(capacity: usize) -> String {
String::with_capacity(capacity)
}
}
pub mod vec {
pub fn vec_new<T>() -> Vec<T> {
Vec::new()
}
pub fn vec_with_capacity<T>(capacity: usize) -> Vec<T> {
Vec::with_capacity(capacity)
}
pub fn vec_push<T>(v: &mut Vec<T>, value: T) {
v.push(value);
}
}
#[cfg(test)]
mod tests;