Skip to main content

tuwunel_core/log/capture/
state.rs

1//! Shared registration state for active captures.
2//!
3//! The state coordinates capture guards with the subscriber layer.
4//! Registrations are reference counted and protected for concurrent access.
5
6use std::sync::{Arc, RwLock};
7
8use super::Capture;
9
10/// Capture layer state.
11pub struct State {
12	pub(super) active: RwLock<Vec<Arc<Capture>>>,
13}
14
15impl Default for State {
16	fn default() -> Self { Self::new() }
17}
18
19impl State {
20	/// Creates empty capture registration state.
21	///
22	/// No events are captured until a `Capture` is started against the returned
23	/// state. The state can then be shared with a subscriber layer.
24	#[must_use]
25	pub fn new() -> Self { Self { active: RwLock::new(Vec::new()) } }
26
27	pub(super) fn add(&self, capture: &Arc<Capture>) {
28		self.active
29			.write()
30			.expect("locked for writing")
31			.push(capture.clone());
32	}
33
34	pub(super) fn del(&self, capture: &Arc<Capture>) {
35		let mut vec = self.active.write().expect("locked for writing");
36		if let Some(pos) = vec.iter().position(|v| Arc::ptr_eq(v, capture)) {
37			vec.swap_remove(pos);
38		}
39	}
40}