deki 0.4.0

A base for most of my rust projects (tailored to myself)!
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# deki-rs

[![GitHub](https://img.shields.io/badge/github-dekirisu/deki-ee6677)](https://github.com/dekirisu/deki-rs/)
[![crates.io](https://img.shields.io/badge/crates.io-deki-orange)](https://crates.io/crates/deki)
[![CI](https://github.com/dekirisu/deki-rs/actions/workflows/ci.yml/badge.svg)](https://github.com/dekirisu/deki-rs/actions/workflows/ci.yml)

A personal Rust utility crate — helper types, traits, macros, and re-exports that reduce boilerplate.

---

## Quick Start

```toml
[dependencies]
deki = { version = "0.4", features = ["random", "lerp"] }
```

```rust
use deki::lerp::Lerpable;

let val: f32 = 0.0.lerp(10.0, 0.5);  // 5.0
```

---

## Features

| Feature | Default | What it adds |
|---|---|---|
| `random` || `fastrand` re-exports, `f32r()`, `Vec::random()` |
| `approx` || Fast bit-hack math via `DekiExtApprox``sqrt_fast`, `exp_fast`, `pow_fast`, `log2_fast` |
| `lerp` || Linear / gated / cyclic / step interpolation traits and methods |
| `derive_more` || Full `derive_more` re-export (Debug, Clone, PartialEq, etc.) |
| `proc` || Proc-macro support (string→ident, token stream helpers) |

---

## deki_core — Runtime Utilities

### Core Types

#### `StackMap<K, V>` — Insertion-Ordered Key-Value Map

Preserves insertion order and allows duplicate keys. Keys stored in a `Vec` — linear search.

```rust
let mut map: StackMap<&str, i32> = Default::default();
*map.entry("count") = 42;
assert_eq!(*map.entry("count"), 42);

for (key, value) in map.iter() { ... } // insertion order
```

#### `Syncable``'static + Send + Sync` in One Word

```rust
fn spawn<T: Syncable>(val: T) { ... }
```

#### `DefaultClear` — Reset Any Default Type

Adds `.clear()` to any `Default` type (resets to `Self::default()`).

```rust
let mut state = MyState::default();
state.clear();  // resets to MyState::default()
```

#### Aliases

| Name | Is |
|---|---|
| `Ghost` | `PhantomData` |
| `Str` | `&'static str` |
| `New` | `derive_new::new` |
| `ext` | `extension_traits::extension` |

Plus re-exports of `buns`, `type_cell`, and `maflow`.

---

### Math

#### Approximate Math (`approx` feature)

Fast bit-hack math (~1% error, bit-hack based):

```rust
use deki_core::math::DekiExtApprox;

let x = 1.5f32;
x.exp_fast();      // ~1% error, 2.2× faster than std
x.pow_fast(3.0);   // ~1% error, 1.5× faster than std
x.log2_fast();     // ~1% error
x.sqrt_fast();     // ~0.1% error, 1.4× faster than std
```

#### Interpolation (`lerp` feature)

**Linear Interpolation**

```rust
use deki::lerp::Lerpable;

let val: f32 = 0.0.lerp(10.0, 0.5);       // 5.0

// For integers (uses mul_f32 rounding)
use deki::lerp::LerpableF32;
let val: i32 = 0i32.lerp(100, 0.3);       // 30
```

**Gated Lerp — Snap When Close**

```rust
use deki::lerp::Glerpable;
let mut val = 0.0f32;
let arrived = val.glerp(10.0, 0.1, 0.5);  // snaps when within 0.5 of target
```

**Cyclic Lerp — Auto-Choose Shortest Direction**

```rust
use deki::lerp::Clerpable;

let val: f32 = 0.0.lerp_qucy(0.9, 0.1, 0.0, 1.0);
// Result: 0.99 (not 0.09) — picks the shorter path

// Gated cyclic variant
let mut val = 0.99f32;
let arrived = val.glerp_qucy(0.0, 0.5, 0.05, 0.0, 1.0);  // snapped
```

**Step Interpolation**

```rust
use deki::lerp::Stepable;
let mut val = 0.0f32;
let arrived = val.sterp(10.0, 2.0);  // moves 2.0 per call, true when arrived
```

**Cyclic Step Interpolation**

```rust
use deki::lerp::CycleStapable;
let mut val = 0.0f32;
let arrived = val.sterp_qucy(0.9, 0.1, 0.0, 1.0);  // cyclic, moves 0.1 per call
```

**Easing**

```rust
let t = 0.5f32;
let eased = t.smooth();  // smoothstep: t*t*(3-2*t)
```

#### Cycling Math

**`add_qucy` / `sub_qucy` — Modular Arithmetic**

Fast modular arithmetic (assumes current value is in range):

```rust
// Circular buffer: advance index, wrap to 0 at capacity
let idx: usize = 99usize.add_qucy(1, 0, 100);  // 0

// Move backwards through a 10-slot inventory, wrapping to the end
let slot: i32 = 2i32.sub_qucy(3, 0, 10);  // 9
```

**`mul_f32` — Multiply Integer by f32 (rounded)**

```rust
let count: i32 = 5.mul_f32(2.5);  // 13 (rounded)
```

#### `#[derive(Cycle)]` — Auto-Cycling Enums

Generates `cycle_next()` and `cycle_prev()` for unit-variant enums:

```rust
use deki_macros::Cycle;

#[derive(Cycle)]
enum Direction { North, East, South, West }

let mut dir = Direction::North;
dir = dir.cycle_next();  // East
dir = dir.cycle_prev();  // North
```

---

### Randomness (`random` feature)

```rust
use deki::random::*;

let val: f32 = f32r(0.0..1.0);
let my_vec = vec![1, 2, 3];
let random_item = my_vec.random();  // picks a random element
```

---

### Helper Macros

#### `qonst!` — Named Constants

Creates a `pub const` named after the type, without requiring `Default`:

```rust
qonst!(Vec3: x: 1.0, y: 2.0, z: 3.0);
// => pub const VEC3: Vec3 = Vec3 { x: 1.0, y: 2.0, z: 3.0 };

qonst!(Direction::North);
// => pub const DIRECTION: Direction = Direction::North;
```

#### `trait_alias!` — Trait Aliases

```rust
deki_core::trait_alias!(MyTrait: Clone + Send + Sync);
// => pub trait MyTrait: Clone + Send + Sync {}
// => impl<C: Clone + Send + Sync> MyTrait for C {}
```

#### `default!` — Shorthand Default

```rust
deki_core::default!(MyStruct = Self { a: 0, b: Default::default() });
// => impl Default for MyStruct { fn default() -> Self { Self { a: 0, b: Default::default() } } }
```

---

## deki_macros — Proc Macros

### Derive Macros

#### `#[derive(Cycle)]`

Generates `cycle_next()` and `cycle_prev()` for unit-variant enums.

```rust
#[derive(Cycle)]
enum Color { Red, Green, Blue }
```

#### `#[derive(ForceDefault)]`

Generates `Default` by calling `.default()` on each field:

```rust
#[derive(ForceDefault)]
struct Config { timeout: u32, name: String }
// => Default produces Config { timeout: 0, name: String::default() }
```

#### `#[derive(EnumFieldCount)]`

Generates `fn field_count(&self) -> usize` for enums:

```rust
use deki_macros::EnumFieldCount;

#[derive(EnumFieldCount)]
enum Color { Red, Green(Rgb), Blue(u8, u8, u8) }

assert_eq!(Color::Red.field_count(), 0);
assert_eq!(Color::Green(Rgb { r: 0, g: 0, b: 0 }).field_count(), 1);
assert_eq!(Color::Blue(0, 0, 0).field_count(), 3);
```

---

### Procedural Macros

#### `xoxo!` — Bool Pattern Matching

```rust
deki_macros::xoxo!{match [true, false, true] {
    [O, O, O] => "all false",
    [X, O, X] => "first and last true",
    [_, _, _] => "default",
}}
```

#### `quimp!` — Quick Trait Implementation

For traits with a single required method:

```rust
dekimacros::quimp!{MyWrapper
    fn clone(&self) -> Self { Self::new(self.0.clone()) };
    fn default() -> Self { Self::new(42) };
}
```

#### `match_fns!` — Declarative Enum Methods

```rust
enum Object { RedSphere, GreenCube }

deki_macros::match_fns!{
    [Object]
    shape() -> &'static str;
    color(brightness: f32) -> &'static str;

    [::RedSphere]
    shape: "sphere";
    color: if brightness > 0.5 { "bright-red" } else { "red" };

    [::GreenCube]
    shape: "cube";
    color: "just-green";
}
```

#### `derive_from!` — Generate `From` Impl

Generate `impl From<A> for B` from function signatures:

```rust
struct Wrapper(i32);
impl Wrapper {
    fn new(v: i32) -> Self { Self(v) }
    fn from_str(s: &str) -> Self { Self(s.len() as i32) }
}

deki_macros::derive_from!{
    Wrapper
    i32 -> new;
    String -> from_str;
}

let w: Wrapper = (42).into();
assert_eq!(w.0, 42);
```

#### `derive_math!` — Generate Arithmetic Impl

Generate `impl Add/Sub/Mul/Div<A> for T` from function signatures:

```rust
#[derive(Debug, PartialEq)]
struct Vec2 { x: f32, y: f32 }

deki_macros::derive_math!{
    Vec2
    Add: Vec2 -> Vec2 -> self.x + rhs.x, self.y + rhs.y -> Vec2;
    Add: f32 -> f32 -> self.x + rhs, self.y + rhs -> Vec2;
    Mul: f32 -> f32 -> self.x * rhs, self.y * rhs -> Vec2;
}

let a = Vec2 { x: 1.0, y: 2.0 };
let b = Vec2 { x: 3.0, y: 4.0 };
assert!((a + b).x - 4.0 < 1e-5);
```

#### `foname!` — Name Mangling

Converts identifiers to different cases:

```rust
deki_macros::foname!{ myFunctionName@snake }   // my_function_name
deki_macros::foname!{ my_function_name@camel }  // myFunctionName
deki_macros::foname!{ my_function_name@scream } // MY_FUNCTION_NAME
deki_macros::foname!{ my_function_name@flat }   // myfunctionname
deki_macros::foname!{ my_function_name@upper }  // MYFUNCTIONNAME
```

---

### Attribute Macros

#### `#[imp(...)]` — Attach Methods to Types or Impl Blocks

**Function-level syntax:**

```rust
#[imp(MyStruct)]
fn new(value: i32) -> Self { Self { value } }

#[imp(MyStruct|MyTrait)]
fn clone(&self) -> Self { Self::new(self.value) }
```

**Foreign types (auto-generates a trait):**

```rust
#[imp(String|*)]
fn trim_all(&self) -> Self { self.trim().to_string() }
// Generates: trait StringTrimAllExt { fn trim_all(&self) -> Self; }
// and impls it for String
```

**Impl block syntax:**

```rust
#[imp(TraitName)]
impl String {
    fn greet(&self) -> Self { self.clone() }
}

#[imp(*NewTraitName)]
impl String {
    fn new_method(&self) -> Self { self.clone() }
}

#[imp(*)]
impl String {
    fn auto_method(&self) -> Self { self.clone() }
}
// Auto-generates: StringAutoMethodExt
```

#### `#[derived(...)]` — Batch Apply Derives

Batch-apply derive macros by name:

```rust
use deki_macros::derived;

#[derived(_Hashable)]
struct Point { x: i32, y: i32 }
// => #[derive(PartialEq, Eq, Hash, Clone, Copy)]
```

---

## License

Dual licensed under [MIT](LICENSE-MIT) or [Apache-2.0](LICENSE-APACHE).