Skip to content

Instantly share code, notes, and snippets.

@colelawrence
Last active May 28, 2024 14:39
Show Gist options
  • Save colelawrence/f54ba263ba35d47a8b1e71b542d2fc91 to your computer and use it in GitHub Desktop.
Save colelawrence/f54ba263ba35d47a8b1e71b542d2fc91 to your computer and use it in GitHub Desktop.
Rust application prelude
pub mod prelude {
// some custom proc macros
pub use i_app_types_proc::shared;
// a "concepts" module that is purely for documenting through doc links
pub use crate::concepts;
// other common shared modules re-exported...
pub use crate::runtime;
pub use crate::testing;
pub use crate::core;
pub use crate::core::PermanentID;
// if used often, it might have a short-hand
pub use crate::model0 as m0;
// general helpers which work better with rust-analyzer for auto completion than vec![]
pub use std::collections::HashMap;
pub fn s(v: impl ToString) -> String {
v.to_string()
}
pub fn vec0<T>() -> Vec<T> {
Vec::new()
}
pub fn vec1<T>(t: T) -> Vec<T> {
vec![t]
}
pub fn vec2<T>(t0: T, t1: T) -> Vec<T> {
vec![t0, t1]
}
pub fn vec3<T>(t0: T, t1: T, t2: T) -> Vec<T> {
vec![t0, t1, t2]
}
pub fn vec4<T>(t0: T, t1: T, t2: T, t3: T) -> Vec<T> {
vec![t0, t1, t2, t3]
}
pub fn vec5<T>(t0: T, t1: T, t2: T, t3: T, t4: T) -> Vec<T> {
vec![t0, t1, t2, t3, t4]
}
pub fn map0<K, V>() -> HashMap<K, V> {
HashMap::new()
}
pub fn map1<K, V>(k: K, v: V) -> HashMap<K, V>
where
K: std::hash::Hash + Eq,
{
let mut map = HashMap::new();
map.insert(k, v);
map
}
pub fn map2<K, V>((k0, v0): (K, V), (k1, v1): (K, V)) -> HashMap<K, V>
where
K: std::hash::Hash + Eq,
{
let mut map = HashMap::new();
map.insert(k0, v0);
map.insert(k1, v1);
map
}
pub fn map3<K, V>((k0, v0): (K, V), (k1, v1): (K, V), (k2, v2): (K, V)) -> HashMap<K, V>
where
K: std::hash::Hash + Eq,
{
let mut map = HashMap::new();
map.insert(k0, v0);
map.insert(k1, v1);
map.insert(k2, v2);
map
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment