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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
// SPDX-FileCopyrightText: Copyright (c) 2026 Mike Li/Mikewolfli/Wei Li(mikewolfli@163.com)
// SPDX-License-Identifier: MIT
//! BLUE13 Phase 3: Alloc bridge — unified imports for std and no_std.
//!
//! All crate files should import common types from here instead of `std`
//! when those types are not available in `core`/`alloc`. This keeps the
//! codebase ready for `#![cfg_attr(feature = "mini", no_std)]` without
//! `#[cfg]` in 200+ files. Note: that attribute is not yet enabled — the
//! `mini` profile currently compiles on std.
//!
//! # The rule
//!
//! Inside this crate, a name that exists in `core` or `alloc` must be imported
//! as `crate::compat::Name`, never as `std::Name`. The re-exports below resolve
//! to the same types today (this build links std regardless), so the rule buys
//! nothing at present except that it is already true at every one of the ~120
//! call sites when `no_std` is switched on. A direct `use std::...` is the one
//! thing that would have to be undone everywhere, which is why the ban is on the
//! import rather than on the capability.
//!
//! # What this module is *not*
//!
//! Switching `mini` to `no_std` is **not** simply a matter of enabling the
//! attribute: the `#[cfg(alloc_frugal)]` arms below are written for a single
//! threaded, allocation-frugal target, but several are still backed by `std`
//! types (`RwLock`, `Mutex`, `Instant`, `RwLock`-guarded `Condvar` paths, and the
//! `std::sync::mpsc` re-export). Do not read the presence of a `no_std`-shaped
//! alias here as evidence that its `alloc_frugal` arm is `no_std`-clean.
//!
//! # Reachability
//!
//! **State:** Production callers: `src/widget/base.rs:43` (`crate::compat::MiniVec<ObjectId>`), `src/widget/base.rs:47` (`crate::compat::MiniString`). 63 files reference the `alloc`/`std` boundary through this module.
// ── core re-exports (exported unconditionally, always available) ──
pub use Any;
pub use ;
pub use fmt;
pub use ;
pub use atomic;
pub use Duration;
// ── RwLock (thread-safe in both profiles) ──
/// Reader/writer lock used across the crate.
pub use RwLock;
/// Read guard returned by [`RwLock::read`].
pub use RwLockReadGuard;
/// Write guard returned by [`RwLock::write`].
pub use RwLockWriteGuard;
pub use ;
// ── alloc re-exports (available in both std and no_std) ──
pub use Box;
pub use BTreeMap;
pub use VecDeque;
pub use format;
pub use Rc;
pub use ;
pub use Arc;
// The no-std prelude does not carry `ToString`, so under `alloc_frugal` every
// `to_string()` call site — including on `&str`, which needs this trait rather
// than an inherent method — stops compiling unless it names the trait itself.
// Re-exporting it from `compat` is what lets those call sites stay
// profile-agnostic: they import the trait from the same module that already
// sorts out the profile's `String`/`Vec`/`Mutex`.
//
// Named `MiniToString` rather than re-exported as `ToString` so that a call site
// can import both names into one list — `use crate::compat::{MiniToString,
// String, ToString}` — without `E0252`. That is only a convenience on desktop,
// where `ToString` also resolves through the prelude, but it keeps a line an
// author writes once working in both profiles. (The trait is not renameable the
// other way: the call is `s.to_string()`, so the *trait* has to be in scope; the
// alias only decides how the import is spelled.)
pub use ToString as MiniToString;
pub use vec;
pub use Vec;
// ── heapless/MiniVec for compile-time fixed-size collections (BLUE13 R5.3-R5.4) ──
// Under mini, Vec and String are replaced with fixed-capacity alternatives.
// Under desktop/full, they remain dynamic (alloc::vec::Vec, alloc::string::String).
/// Fixed-capacity vector for mini builds. Falls back to `Vec<T>` on desktop.
///
/// Under `alloc_frugal` this is `heapless::Vec<T, 64>`: **pushing beyond 64
/// elements fails** rather than reallocating. Under desktop builds it is an
/// ordinary growable `alloc::vec::Vec<T>`, so code must not rely on the
/// capacity limit being enforced.
pub type MiniVec<T> = Vec;
/// Growable vector alias used on desktop builds; see the `alloc_frugal`
/// definition for the capacity-limited variant.
pub type MiniVec<T> = Vec;
/// Fixed-capacity string for mini builds. Falls back to `String` on desktop.
///
/// Under `alloc_frugal` this is `heapless::String<256>`, so at most 256 bytes
/// of UTF-8 are retained; see [`into_mini`], which silently truncates on
/// overflow. Under desktop builds it is an unbounded `alloc::string::String`.
pub type MiniString = String;
/// Growable string alias used on desktop builds; see the `alloc_frugal`
/// definition for the capacity-limited variant.
pub type MiniString = String;
/// Convert a `&str` to `MiniString`. Under mini, copies into fixed buffer.
/// Under desktop, creates an owned `String`.
///
/// Truncation is **silent** under `alloc_frugal`: the `heapless` push fails on
/// the byte that would cross 256, so the result is the longest whole-prefix of
/// `s` that fits, cut at a UTF-8 boundary, with no error reported. A caller that
/// must know the text survived intact has to compare lengths.
/// Convert a `String` to `MiniString` (consumes the String).
/// Under mini, copies into fixed buffer. Under desktop, zero-cost.
///
/// Under `alloc_frugal` the original heap `String` is dropped after being copied
/// into the fixed buffer, so this is not a move — the allocation is released and
/// the same silent 256-byte truncation as [`into_mini`] applies. Under desktop
/// it is a genuine no-op move.
// ── std→alloc bridge (conditional: BTreeMap stands in for HashMap under mini) ──
/// Map type used across the crate, so call sites do not name a concrete map.
///
/// Resolves to `std::collections::HashMap` on desktop builds and to
/// `alloc::collections::BTreeMap` under `alloc_frugal`. The two are only
/// interchangeable through this alias: they do not share an implementation, and
/// the difference is observable in iteration order (hashed vs sorted by key) and
/// in the key requirements — `BTreeMap` needs `Ord` where `HashMap` needs `Hash`.
/// Code that relies on either property through this alias will not compile, or
/// will silently change behaviour, on the other profile; treat it as an
/// unordered map.
pub use BTreeMap as HashMap;
pub use HashMap;
// ── Mutex (thread-safe in both profiles) ──
// Under `alloc_frugal` (mini) the crate is no_std, so the `std::sync` locks are
// unavailable and `spin`'s stand in. Both provide the same contract; the
// difference is that `spin` has no poison state, which is what [`lock`],
// [`read_lock`] and [`write_lock`] below normalise so call sites need no `#[cfg]`.
// A RefCell-backed "Mutex" was rejected: it panics on re-entrant/concurrent
// access — not a real mutual-exclusion primitive.
/// Mutual-exclusion lock used across the crate.
pub use Mutex;
/// Guard returned by [`Mutex::lock`]. Its lifetime ties the guard to the lock, so
/// it cannot outlive the mutex it came from.
pub use MutexGuard;
pub use Mutex;
pub use MutexGuard;
/// Acquire a [`Mutex`], recovering from poisoning if there is any.
///
/// The two profiles disagree about what `lock()` returns: `std::sync::Mutex`
/// returns `LockResult<MutexGuard<T>>` (poisoning is a real state), while
/// `spin::Mutex` returns a bare `MutexGuard<T>` (a spin lock has no poison
/// state to report). Normalising that difference here is what lets call sites
/// be identical in both profiles instead of carrying a `#[cfg]` each.
///
/// Poison recovery is the same policy the crate already applied by hand:
/// `unwrap_or_else(|e| e.into_inner())` takes the guard rather than propagating
/// a panic, because a poisoned lock still holds usable data.
/// Acquire a [`RwLock`] for reading, recovering from poisoning if there is any.
///
/// The counterpart of [`lock`] for the reader side: same profile split (`std`
/// returns `LockResult`, `spin` returns the guard directly), same policy of
/// taking the guard rather than propagating the poison.
/// Acquire a [`RwLock`] for writing, recovering from poisoning if there is any.
///
/// The write-side counterpart of [`read_lock`].
// ── Bump arena allocator (BLUE13 R5.5) ──
// Under mini, a pre-allocated bump arena replaces the global heap allocator.
// This eliminates the need for a full `alloc` runtime while keeping Box-like
// allocation via `arena_box()`. The arena is reset on each frame cycle.
/// Bump arena allocator. On mini, backed by a single-threaded `bumpalo::Bump`.
/// On desktop, this is a no-op wrapper (allocation goes through the global allocator).
///
/// # Invariants
///
/// * **Single-threaded.** The safety of the aliasing rules below rests on `mini`
/// never running the arena from two threads; there is no synchronisation
/// inside it.
/// * **Nothing is freed until [`MiniArena::reset`].** A dropped arena value, a
/// dropped allocation, a dropped box — none of them release the backing block.
/// `reset` frees *everything* at once, so it invalidates every reference the
/// arena has handed out.
/// * **The returned references are not tied to a borrow.** See
/// [`MiniArena::alloc`].
///
/// Under `not(alloc_frugal)` this is a zero-sized type; see the desktop
/// definition below.
crateimpl_default_via_new!;
/// No-op arena used on desktop builds.
///
/// Allocation is delegated to the global allocator, and [`MiniArena::reset`]
/// does nothing because there is nothing arena-owned to free. It exists so the
/// arena call sites compile unchanged on both profiles.
///
/// It is zero-sized, so this type costs nothing to pass around, and the methods
/// below deliberately differ in signature from the `alloc_frugal` ones — see
/// [`MiniArena::alloc`] — because a global-heap allocation *is* owned by its
/// caller rather than borrowed from a shared arena.
;
/// Get the global frame arena. Under mini, allocations live until `reset_frame_arena()`.
/// Under desktop, this is a no-op (uses `Box::new` directly).
///
/// The arena is process-global, so every caller shares one allocation pool and a
/// [`reset_frame_arena`] from any of them invalidates all of them. Under
/// `alloc_frugal` it is initialised on first use through [`OnceLock`], so the
/// first call allocates and later ones do not.
/// Reset the global frame arena. Under mini, frees all arena allocations.
/// Under desktop, this is a no-op.
///
/// The word "frame" is a convention, not a clock: nothing calls this
/// automatically, so a build that never calls it never reclaims arena memory.
/// Under `alloc_frugal` it must be called at a point where no arena reference is
/// still live (see [`MiniArena::reset`]).
// ── OnceLock compat (thread-safe static init for both std and no_std) ──
/// Thread-safe once-cell for static initialization.
/// Under mini (no_std), backed by a spin-based atomic flag + UnsafeCell.
/// Under desktop, re-exports `std::sync::OnceLock`.
///
/// # Why not `std::sync::OnceLock`
///
/// `std`'s version is the default whenever the symlink below resolves to it; this
/// type exists for the `alloc_frugal` arm, where the cell has to be constructible
/// in a `static` (`const fn new`) and usable without the std runtime.
///
/// # Thread safety
///
/// `Send` and `Sync` are asserted unconditionally below even though the type
/// holds a `UnsafeCell<MaybeUninit<T>>` and never synchronises a *second*
/// initialiser: the initialisation path is a plain load, a write, and a store,
/// with no compare-exchange, so two threads racing [`OnceLock::get_or_init`] on
/// the same cell would both write. That is sound only because `mini` is
/// single-threaded. [`OnceLock::set`] is the exception — it does use a
/// compare-exchange, so it reports the loser rather than overwriting.
// SAFETY: Under mini (no_std, single-threaded), no concurrent access is possible.
unsafe
unsafe
pub use OnceLock;
// ── Instant (real clock in both profiles) ──
// Both arms must be a *real* monotonic clock. A zero-valued stub was rejected:
// it would silently break every timing-based subsystem (timers, FPS counters,
// animation frames) rather than fail visibly.
//
// No arm is a bare `pub use std::time::Instant`. The `alloc_frugal` arm is a
// wrapper whose methods delegate to `std::time::Instant`, so a caller sees one
// type with one behaviour in both profiles:
//
// * `spin`, the profile's lock crate, carries no clock at all — a spin lock has
// nothing to read time from — so it cannot supply one;
// * `core::time` provides only `Duration`, with no way to read a clock;
// * and the `std` the crate does link under `mini` (see the
// `#[macro_use] extern crate std` in `lib.rs`) *does* have a real monotonic
// clock. Reaching it from here, rather than from `compat`'s callers, is what
// keeps the `std` dependency confined to the one module whose job is to
// reconcile the two profiles.
/// Monotonic clock reading used across the crate.
pub use Instant;
/// Monotonic clock reading used across the crate.
///
/// The `alloc_frugal` spelling of `std::time::Instant`. It is the same clock with
/// the same semantics — `now`, `duration_since`, `elapsed` and the `Sub`/`Add`
/// arithmetic — so timing code behaves identically in both profiles. The wrapper
/// exists so that `compat`, not each call site, answers which `std` items this
/// profile is allowed to name.
;
// ── mpsc compat (single-threaded channel for mini builds) ──
/// Single-threaded channel for mini (no_std) builds.
/// Wraps a `VecDeque` behind `RefCell` + `Arc`.
///
/// # Not a real channel
///
/// `std::sync::mpsc` blocks in `Receiver::recv`; this one **cannot**, because
/// there is no second thread to wake it. `recv` is defined as a non-blocking poll
/// that reports `Err(())` — the same shape an empty `try_recv` has — so waiters
/// written for the std API must poll instead. `mini`'s event loop does exactly
/// that: it drains with `dequeue()` rather than `dequeue_blocking()`. A caller
/// that reaches for `recv` expecting it to wait will spin or give up early, not
/// block.
///
/// A side effect of the std-shaped surface is that the exact failure reason is
/// lost: there is no `TryRecvError` here, so "empty" and "disconnected" are both
/// `Err(())`. Sharing the queue through `Arc<RefCell<..>>` also means a re-entrant
/// `send` from inside a `recv` would panic on the borrow, and the pair is `!Send`
/// and `!Sync` — deliberately, since it is single-threaded by construction.
pub use mpsc;
// ── Condvar compat (no_std stub for mini builds) ──
/// A condition variable for thread synchronization.
/// Under mini, all operations are no-ops (single-threaded).
/// Under desktop, re-exports `std::sync::Condvar`.
///
/// # This one is a genuine stub
///
/// The `alloc_frugal` version below is **not** a usable condition variable: it has
/// no `wait` and no `wait_timeout`, so it cannot park a caller, and both notify
/// methods do nothing. Code that needs to wait cannot be written against this
/// type at all — which is why the crate's waiting queues are gated the other way
/// around (see [`crate::event::queue`], whose blocking paths are
/// `not(alloc_frugal)`). The desktop alias is the real thing.
;
crateimpl_default_via_new!;
/// A condition variable for thread synchronization.
///
/// The real `std::sync::Condvar`, re-exported so call sites need no profile
/// branch. Unlike the `alloc_frugal` stub it has `wait` / `wait_timeout`, which is
/// what makes the blocking paths of `crate::event::queue` possible on this
/// profile.
pub use Condvar;