Skip to content
Snippets Groups Projects
Verified Commit 1e34b0fc authored by Louis's avatar Louis :fire:
Browse files

Import from advent / derelict code

parents
No related branches found
No related tags found
No related merge requests found
/target
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/weirdboi_utils.iml" filepath="$PROJECT_DIR$/.idea/weirdboi_utils.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="EMPTY_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "weirdboi_utils"
version = "0.1.0"
[package]
name = "weirdboi_utils"
version = "0.1.0"
edition = "2024"
description = "A collection of macro utilities for common operations"
[features]
default = [
"collections",
"control_flow",
]
collections = []
control_flow = []
[dependencies]
#[macro_export]
macro_rules! hashmap {
// Empty hashmap
() => {
std::collections::HashMap::new()
};
// From key-value pairs with comma at the end
($($key:expr => $value:expr,)+) => {
$crate::hashmap!($($key => $value),+)
};
// From key-value pairs without trailing comma
($($key:expr => $value:expr),*) => {
{
let capacity = $crate::count!($($key),*);
let mut map = std::collections::HashMap::with_capacity(capacity);
$(
map.insert($key, $value);
)*
map
}
};
// From key-value pairs with explicit capacity hint (overriding automatic calculation)
($($key:expr => $value:expr),* ; capacity: $capacity:expr) => {
{
let mut map = std::collections::HashMap::with_capacity($capacity);
$(
map.insert($key, $value);
)*
map
}
};
}
#[cfg(test)]
mod tests {
use crate::*;
#[test]
fn test_count_macro() {
assert_eq!(count!(), 0);
assert_eq!(count!(1), 1);
assert_eq!(count!(1, 2), 2);
assert_eq!(count!(1, 2, 3), 3);
assert_eq!(count!(1, 2, 3,), 3);
}
#[test]
fn test_hashmap_macro() {
let empty: std::collections::HashMap<i32, i32> = hashmap!();
assert_eq!(empty.len(), 0);
let map = hashmap!(
"a" => 1,
"b" => 2,
"c" => 3
);
assert_eq!(map.len(), 3);
assert_eq!(map.capacity(), 3);
assert_eq!(map.get("a"), Some(&1));
assert_eq!(map.get("b"), Some(&2));
assert_eq!(map.get("c"), Some(&3));
let map_trailing_comma = hashmap!(
"a" => 1,
"b" => 2,
"c" => 3,
);
assert_eq!(map_trailing_comma.len(), 3);
let map_explicit_capacity = hashmap!(
"a" => 1,
"b" => 2;
capacity: 10
);
assert_eq!(map_explicit_capacity.len(), 2);
}
}
#[macro_export]
macro_rules! some {
($expr: expr) => {
some!($expr, ())
};
($expr: expr; continue) => {
if let Some(v) = $expr {
v
} else {
continue;
}
};
($expr: expr, $or_else: expr) => {
match $expr {
Some(v) => v,
None => return $or_else,
}
};
}
#[macro_export]
macro_rules! ok {
($expr: expr) => {
ok!($expr, ())
};
($expr: expr; continue) => {
if let Ok(v) = $expr {
v
} else {
continue;
}
};
($expr: expr, $or_else: expr) => {
match $expr {
Ok(v) => v,
Err(_) => return $or_else,
}
};
}
#[cfg(feature = "collections")]
mod collections;
#[cfg(feature = "control_flow")]
mod control_flow;
mod meta;
#[macro_export]
macro_rules! count {
() => (0usize);
($head:expr $(, $tail:expr)*) => (1usize + $crate::count!($($tail),*));
($head:expr, $($tail:expr,)*) => ($crate::count!($head $(, $tail)*));
}
use std::collections::HashMap;
use weirdboi_utils::hashmap;
#[test]
fn it_creates_empty_hashmap() {
let empty: HashMap<(), ()> = hashmap!();
assert!(empty.is_empty());
assert_eq!(empty.capacity(), 0);
}
#[test]
fn it_infers_hashmap_types() {
let map = hashmap! {
"ffoo" => 123usize,
};
assert_eq!(map.len(), 1);
assert_eq!(map.get("ffoo"), Some(&123));
}
#[test]
fn it_creates_hashmap_with_capacity() {
let map = hashmap! {
"foo" => 123,
"bar" => 456,
"baz" => 789;
capacity: 32
};
assert_eq!(map.len(), 3);
assert!(map.capacity() >= 32);
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment