v_utils 2.15.41

My utils crate
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
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
// Lifecycle state machine for system components.
// Originally from nautilus_trader::common::component.

//#![allow(unsafe_code)]

use std::{
	cell::{RefCell, UnsafeCell},
	collections::HashSet,
	fmt::{self, Debug, Display, Formatter},
	hash::Hash,
	rc::Rc,
};

pub use ustr::Ustr;

/// Components have state and lifecycle management capabilities.
pub trait Component: Debug {
	/// Returns the unique identifier for this component.
	fn component_id(&self) -> ComponentId;

	fn state(&self) -> ComponentState;

	/// Transition the component with the state trigger.
	///
	/// # Panics
	///
	/// Panics if `trigger` is invalid for the current state.
	fn transition_state(&mut self, trigger: ComponentTrigger);

	fn is_ready(&self) -> bool {
		self.state() == ComponentState::Ready
	}

	fn is_running(&self) -> bool {
		self.state() == ComponentState::Running
	}

	fn is_stopped(&self) -> bool {
		self.state() == ComponentState::Stopped
	}

	fn is_degraded(&self) -> bool {
		self.state() == ComponentState::Degraded
	}

	fn is_faulted(&self) -> bool {
		self.state() == ComponentState::Faulted
	}

	fn is_disposed(&self) -> bool {
		self.state() == ComponentState::Disposed
	}

	fn initialize(&mut self) {
		self.transition_state(ComponentTrigger::Initialize);
	}

	fn start(&mut self) -> eyre::Result<()> {
		self.transition_state(ComponentTrigger::Start);

		if let Err(e) = self.on_start() {
			tracing::error!("{e}");
			return Err(e);
		}

		self.transition_state(ComponentTrigger::StartCompleted);
		Ok(())
	}

	fn stop(&mut self) -> eyre::Result<()> {
		self.transition_state(ComponentTrigger::Stop);

		if let Err(e) = self.on_stop() {
			tracing::error!("{e}");
			return Err(e);
		}

		self.transition_state(ComponentTrigger::StopCompleted);
		Ok(())
	}

	fn resume(&mut self) -> eyre::Result<()> {
		self.transition_state(ComponentTrigger::Resume);

		if let Err(e) = self.on_resume() {
			tracing::error!("{e}");
			return Err(e);
		}

		self.transition_state(ComponentTrigger::ResumeCompleted);
		Ok(())
	}

	fn reset(&mut self) -> eyre::Result<()> {
		self.transition_state(ComponentTrigger::Reset);

		if let Err(e) = self.on_reset() {
			tracing::error!("{e}");
			return Err(e);
		}

		self.transition_state(ComponentTrigger::ResetCompleted);
		Ok(())
	}

	fn degrade(&mut self) -> eyre::Result<()> {
		self.transition_state(ComponentTrigger::Degrade);

		if let Err(e) = self.on_degrade() {
			tracing::error!("{e}");
			return Err(e);
		}

		self.transition_state(ComponentTrigger::DegradeCompleted);
		Ok(())
	}

	fn fault(&mut self) -> eyre::Result<()> {
		self.transition_state(ComponentTrigger::Fault);

		if let Err(e) = self.on_fault() {
			tracing::error!("{e}");
			return Err(e);
		}

		self.transition_state(ComponentTrigger::FaultCompleted);
		Ok(())
	}

	fn dispose(&mut self) -> eyre::Result<()> {
		self.transition_state(ComponentTrigger::Dispose);

		if let Err(e) = self.on_dispose() {
			tracing::error!("{e}");
			return Err(e);
		}

		self.transition_state(ComponentTrigger::DisposeCompleted);
		Ok(())
	}

	fn on_start(&mut self) -> eyre::Result<()> {
		Ok(())
	}
	fn on_stop(&mut self) -> eyre::Result<()> {
		Ok(())
	}
	fn on_resume(&mut self) -> eyre::Result<()> {
		Ok(())
	}
	fn on_reset(&mut self) -> eyre::Result<()> {
		Ok(())
	}
	fn on_degrade(&mut self) -> eyre::Result<()> {
		Ok(())
	}
	fn on_fault(&mut self) -> eyre::Result<()> {
		Ok(())
	}
	fn on_dispose(&mut self) -> eyre::Result<()> {
		Ok(())
	}
}
/// Represents a valid component ID.
///
/// Backed by [`Ustr`] for O(1) cloning and comparison.
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct ComponentId(Ustr);

impl ComponentId {
	pub fn new(value: &str) -> Self {
		assert!(!value.is_empty() && value.is_ascii(), "ComponentId must be non-empty ASCII, got: {value:?}");
		Self(Ustr::from(value))
	}

	pub fn inner(&self) -> Ustr {
		self.0
	}

	pub fn as_str(&self) -> &str {
		self.0.as_str()
	}
}

impl Debug for ComponentId {
	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
		write!(f, "{:?}", self.0)
	}
}

impl Display for ComponentId {
	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
		write!(f, "{}", self.0)
	}
}

impl From<&str> for ComponentId {
	fn from(value: &str) -> Self {
		Self::new(value)
	}
}

/// The state of a component within the system.
#[derive(Clone, Copy, Debug, Default, derive_more::Display, Eq, Hash, PartialEq)]
pub enum ComponentState {
	/// When a component is instantiated, but not yet ready to fulfill its specification.
	#[default]
	PreInitialized,
	/// When a component is able to be started.
	Ready,
	/// When a component is executing its actions on `start`.
	Starting,
	/// When a component is operating normally and can fulfill its specification.
	Running,
	/// When a component is executing its actions on `stop`.
	Stopping,
	/// When a component has successfully stopped.
	Stopped,
	/// When a component is started again after its initial start.
	Resuming,
	/// When a component is executing its actions on `reset`.
	Resetting,
	/// When a component is executing its actions on `dispose`.
	Disposing,
	/// When a component has successfully shut down and released all of its resources.
	Disposed,
	/// When a component is executing its actions on `degrade`.
	Degrading,
	/// When a component has successfully degraded and may not meet its full specification.
	Degraded,
	/// When a component is executing its actions on `fault`.
	Faulting,
	/// When a component has successfully shut down due to a detected fault.
	Faulted,
}

#[rustfmt::skip]
impl ComponentState {
	/// Transition the state machine with the given `trigger`.
	///
	/// # Panics
	///
	/// Panics if `trigger` is invalid for the current state.
	pub fn transition(&mut self, trigger: ComponentTrigger) -> Self {
		let new_state = match (&self, trigger) {
			(Self::PreInitialized, ComponentTrigger::Initialize) => Self::Ready,
			(Self::Ready, ComponentTrigger::Reset) => Self::Resetting,
			(Self::Ready, ComponentTrigger::Start) => Self::Starting,
			(Self::Ready, ComponentTrigger::Dispose) => Self::Disposing,
			(Self::Resetting, ComponentTrigger::ResetCompleted) => Self::Ready,
			(Self::Starting, ComponentTrigger::StartCompleted) => Self::Running,
			(Self::Starting, ComponentTrigger::Stop) => Self::Stopping,
			(Self::Starting, ComponentTrigger::Fault) => Self::Faulting,
			(Self::Running, ComponentTrigger::Stop) => Self::Stopping,
			(Self::Running, ComponentTrigger::Degrade) => Self::Degrading,
			(Self::Running, ComponentTrigger::Fault) => Self::Faulting,
			(Self::Resuming, ComponentTrigger::Stop) => Self::Stopping,
			(Self::Resuming, ComponentTrigger::ResumeCompleted) => Self::Running,
			(Self::Resuming, ComponentTrigger::Fault) => Self::Faulting,
			(Self::Stopping, ComponentTrigger::StopCompleted) => Self::Stopped,
			(Self::Stopping, ComponentTrigger::Fault) => Self::Faulting,
			(Self::Stopped, ComponentTrigger::Reset) => Self::Resetting,
			(Self::Stopped, ComponentTrigger::Resume) => Self::Resuming,
			(Self::Stopped, ComponentTrigger::Dispose) => Self::Disposing,
			(Self::Stopped, ComponentTrigger::Fault) => Self::Faulting,
			(Self::Degrading, ComponentTrigger::DegradeCompleted) => Self::Degraded,
			(Self::Degraded, ComponentTrigger::Resume) => Self::Resuming,
			(Self::Degraded, ComponentTrigger::Stop) => Self::Stopping,
			(Self::Degraded, ComponentTrigger::Fault) => Self::Faulting,
			(Self::Disposing, ComponentTrigger::DisposeCompleted) => Self::Disposed,
			(Self::Faulting, ComponentTrigger::FaultCompleted) => Self::Faulted,
			_ => panic!("Invalid state transition: {self} -> {trigger}"),
		};
		*self = new_state;
		new_state
	}
}

/// A trigger condition for a component state transition.
#[derive(Clone, Copy, Debug, derive_more::Display, Eq, Hash, PartialEq)]
pub enum ComponentTrigger {
	Initialize,
	Start,
	StartCompleted,
	Stop,
	StopCompleted,
	Resume,
	ResumeCompleted,
	Reset,
	ResetCompleted,
	Dispose,
	DisposeCompleted,
	Degrade,
	DegradeCompleted,
	Fault,
	FaultCompleted,
}

// ── Registry ──────────────────────────────────────────────────────────

thread_local! {
	static COMPONENT_REGISTRY: ComponentRegistry = ComponentRegistry::default();
}

/// Registry for storing components with runtime borrow tracking.
///
/// The registry tracks which components are currently mutably borrowed to prevent
/// multiple simultaneous mutable borrows (which would be undefined behavior).
pub struct ComponentRegistry {
	components: RefCell<ustr::UstrMap<Rc<UnsafeCell<dyn Component>>>>,
	borrows: RefCell<HashSet<Ustr>>,
}
impl ComponentRegistry {
	pub fn insert(&self, id: Ustr, component: Rc<UnsafeCell<dyn Component>>) {
		self.components.borrow_mut().insert(id, component);
	}

	pub fn get(&self, id: &Ustr) -> Option<Rc<UnsafeCell<dyn Component>>> {
		self.components.borrow().get(id).cloned()
	}

	/// Checks if a component is currently borrowed.
	pub fn is_borrowed(&self, id: &Ustr) -> bool {
		self.borrows.borrow().contains(id)
	}

	/// Marks a component as borrowed. Returns false if already borrowed.
	fn try_borrow(&self, id: Ustr) -> bool {
		let mut borrows = self.borrows.borrow_mut();
		if borrows.contains(&id) {
			false
		} else {
			borrows.insert(id);
			true
		}
	}

	/// Releases a borrow on a component.
	fn release_borrow(&self, id: &Ustr) {
		self.borrows.borrow_mut().remove(id);
	}
}

impl Default for ComponentRegistry {
	fn default() -> Self {
		Self {
			components: RefCell::new(ustr::UstrMap::default()),
			borrows: RefCell::new(HashSet::new()),
		}
	}
}

impl Debug for ComponentRegistry {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		let components_ref = self.components.borrow();
		let keys: Vec<&Ustr> = components_ref.keys().collect();
		f.debug_struct("ComponentRegistry")
			.field("components", &keys)
			.field("active_borrows", &self.borrows.borrow().len())
			.finish()
	}
}

/// Returns a reference to the global component registry.
pub fn get_component_registry() -> &'static ComponentRegistry {
	COMPONENT_REGISTRY.with(|registry|
		// SAFETY: We return a static reference that lives for the lifetime of the thread.
		// Since this is thread_local storage, each thread has its own instance.
		unsafe { std::mem::transmute::<&ComponentRegistry, &'static ComponentRegistry>(registry) })
}
/// Registers a component in the global registry.
pub fn register_component<T>(component: T) -> Rc<UnsafeCell<T>>
where
	T: Component + 'static, {
	let component_id = component.component_id().inner();
	let component_ref = Rc::new(UnsafeCell::new(component));

	let component_trait_ref: Rc<UnsafeCell<dyn Component>> = component_ref.clone();
	get_component_registry().insert(component_id, component_trait_ref);

	component_ref
}
/// Returns a component from the global registry by ID.
pub fn get_component(id: &ComponentId) -> Option<Rc<UnsafeCell<dyn Component>>> {
	get_component_registry().get(&id.inner())
}
#[cfg(test)]
/// Clears the component registry (for test isolation).
pub fn clear_component_registry() {
	let registry = get_component_registry();
	registry.components.borrow_mut().clear();
	registry.borrows.borrow_mut().clear();
}
/// Guard that releases a component borrow when dropped.
///
/// This ensures borrows are released even if the code panics during
/// a lifecycle method call.
struct BorrowGuard {
	id: Ustr,
}

impl BorrowGuard {
	fn new(id: Ustr) -> Self {
		Self { id }
	}
}

impl Drop for BorrowGuard {
	fn drop(&mut self) {
		get_component_registry().release_borrow(&self.id);
	}
}

macro_rules! registry_lifecycle_fn {
	($fn_name:ident, $method:ident, $action:expr) => {
		/// Safely calls
		#[doc = $action]
		/// on a component in the global registry.
		///
		/// # Panics
		///
		/// Panics if the component is not found or is already borrowed.
		pub fn $fn_name(id: &Ustr) -> eyre::Result<()> {
			let registry = get_component_registry();
			let component_ref = registry.get(id).unwrap_or_else(|| panic!("Component '{id}' not found in global registry"));

			assert!(registry.try_borrow(*id), "Component '{id}' is already mutably borrowed — aliasing mutable references is UB",);

			let _guard = BorrowGuard::new(*id);

			// SAFETY: Borrow tracking ensures exclusive access
			unsafe {
				let component = &mut *component_ref.get();
				component.$method()
			}
		}
	};
}

registry_lifecycle_fn!(start_component, start, "start()");
registry_lifecycle_fn!(stop_component, stop, "stop()");
registry_lifecycle_fn!(reset_component, reset, "reset()");
registry_lifecycle_fn!(dispose_component, dispose, "dispose()");

#[cfg(test)]
mod tests {
	use std::sync::atomic::{AtomicBool, Ordering};

	use super::*;

	#[derive(Debug)]
	struct TestComponent {
		id: ComponentId,
		state: ComponentState,
		should_panic: &'static AtomicBool,
	}

	impl TestComponent {
		fn new(name: &str, should_panic: &'static AtomicBool) -> Self {
			Self {
				id: ComponentId::new(name),
				state: ComponentState::Ready,
				should_panic,
			}
		}
	}

	impl Component for TestComponent {
		fn component_id(&self) -> ComponentId {
			self.id
		}

		fn state(&self) -> ComponentState {
			self.state
		}

		fn transition_state(&mut self, trigger: ComponentTrigger) {
			self.state.transition(trigger);
		}

		#[allow(clippy::panic_in_result_fn)]
		fn on_start(&mut self) -> eyre::Result<()> {
			if self.should_panic.load(Ordering::SeqCst) {
				panic!("Intentional panic for testing");
			}
			Ok(())
		}
	}

	static NO_PANIC: AtomicBool = AtomicBool::new(false);
	static DO_PANIC: AtomicBool = AtomicBool::new(true);

	#[test]
	fn borrow_tracking_prevents_double_borrow() {
		clear_component_registry();

		let component = TestComponent::new("test-1", &NO_PANIC);
		let id = component.id.inner();

		let component_ref = Rc::new(UnsafeCell::new(component));
		get_component_registry().insert(id, component_ref);

		let result1 = start_component(&id);
		assert!(result1.is_ok());

		// Component should now be borrowable again (guard released)
		let result2 = stop_component(&id);
		assert!(result2.is_ok());
	}

	#[test]
	fn borrow_released_after_lifecycle_call() {
		clear_component_registry();

		let component = TestComponent::new("test-2", &NO_PANIC);
		let id = component.id.inner();

		let component_ref = Rc::new(UnsafeCell::new(component));
		get_component_registry().insert(id, component_ref);

		let _ = start_component(&id);
		assert!(!get_component_registry().is_borrowed(&id));
	}

	#[test]
	fn borrow_released_on_panic() {
		clear_component_registry();

		let component = TestComponent::new("test-panic", &DO_PANIC);
		let id = component.id.inner();

		let component_ref = Rc::new(UnsafeCell::new(component));
		get_component_registry().insert(id, component_ref);

		let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
			let _ = start_component(&id);
		}));
		assert!(result.is_err(), "Expected panic from on_start");

		assert!(!get_component_registry().is_borrowed(&id), "Borrow was not released after panic");
	}
}