Skip to main content

nil/
lib.rs

1#![doc = include_str!("../README.md")]
2
3pub use smart_default;
4pub use once_cell;
5pub use parking_lot;
6
7/// Extra std imports that i use a lot.
8pub mod std_prelude {
9	pub use std::path::{Path, PathBuf};
10	pub use std::fs;
11	pub use std::io;
12	pub use std::thread;
13	pub use std::collections::{HashMap, BTreeMap, HashSet, BTreeSet};
14	pub use std::sync::Arc;
15	pub use std::sync::atomic::*;
16	pub use std::error::Error;
17	pub use std::borrow::Cow;
18	pub use std::time::{Duration, Instant};
19	pub use std::mem;
20	pub use std::fmt;
21	pub use std::env;
22	pub use std::process;
23	pub use std::any::*;
24	pub use std::f32::consts::{
25		PI,
26		FRAC_PI_2,
27		FRAC_PI_3,
28		FRAC_PI_4,
29		FRAC_PI_6,
30		FRAC_PI_8,
31	};
32	pub use std::f64::consts::{
33		PI as PI_F64,
34		FRAC_PI_2 as FRAC_PI_2_F64,
35		FRAC_PI_3 as FRAC_PI_3_F64,
36		FRAC_PI_4 as FRAC_PI_4_F64,
37		FRAC_PI_6 as FRAC_PI_6_F64,
38		FRAC_PI_8 as FRAC_PI_8_F64,
39	};
40}
41
42pub mod prelude {
43	pub use crate::flat;
44	pub use crate::io_add_msg;
45	pub use crate::ShortToString;
46
47	pub use once_cell::sync::Lazy;
48	pub use parking_lot::{Mutex, MutexGuard, MappedMutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard, MappedRwLockReadGuard};
49	pub use smart_default::*;
50}
51
52/// Makes defining a flat module (e.g. foo::Baz instead of foo::bar::Baz) easier.
53///
54/// Instead of
55/// ```ignore
56/// pub mod foo;
57/// pub use foo::*;
58/// pub mod bar;
59/// pub use bar::*;
60/// ```
61/// You could use
62/// ```ignore
63/// use nil::*;
64///
65/// flat! {
66///     foo;
67///     bar;
68/// }
69/// ```
70#[macro_export]
71macro_rules! flat {
72	{$($(#[$attr:meta])* $name:ident ;)*} => {
73		$( $(#[$attr])* mod $name; $(#[$attr])* pub use self::$name::*; )*
74	};
75}
76
77/// Expands to a function that prepends a message to an io error, to be used with `Result::map_err`.
78#[macro_export]
79macro_rules! io_add_msg {
80	($($msg:tt)+) => {
81		|err| std::io::Error::new(err.kind(), format!("{} {err}", format!($($msg)+)))
82	};
83}
84
85/// Extension trait that shortens `.to_owned()` or `.to_string_lossy().to_string()` into just `.s()` to get a [String].
86/// 
87/// # Examples
88/// ```
89/// use nil::*;
90/// 
91/// let string: String = "foo".s();
92/// let owned_str: String = "foo".to_owned();
93/// 
94/// assert_eq!(string, owned_str);
95/// ```
96pub trait ShortToString {
97	/// Shorthand for getting a string representation
98	fn s(&self) -> String;
99}
100
101impl ShortToString for str {
102	#[inline]
103	fn s(&self) -> String {
104		self.to_owned()
105	}
106}
107
108impl ShortToString for std::ffi::OsStr {
109	#[inline]
110	fn s(&self) -> String {
111		self.to_string_lossy().to_string()
112	}
113}
114
115impl ShortToString for std::path::Path {
116	#[inline]
117	fn s(&self) -> String {
118		self.to_string_lossy().to_string()
119	}
120}
121
122impl ShortToString for std::ffi::CStr {
123	#[inline]
124	fn s(&self) -> String {
125		self.to_string_lossy().to_string()
126	}
127}
128
129/// Shorthand for `T::default()` or `Default::default()`, good for structure initialization. Inspired by a function of the same name and purpose in `bevy_utils`.
130#[inline]
131pub fn default<T: Default>() -> T {
132	T::default()
133}