Skip to main content

tuwunel_core/log/capture/
state.rs

1use std::sync::{Arc, RwLock};
2
3use super::Capture;
4
5/// Capture layer state.
6pub struct State {
7	pub(super) active: RwLock<Vec<Arc<Capture>>>,
8}
9
10impl Default for State {
11	fn default() -> Self { Self::new() }
12}
13
14impl State {
15	#[must_use]
16	pub fn new() -> Self { Self { active: RwLock::new(Vec::new()) } }
17
18	pub(super) fn add(&self, capture: &Arc<Capture>) {
19		self.active
20			.write()
21			.expect("locked for writing")
22			.push(capture.clone());
23	}
24
25	pub(super) fn del(&self, capture: &Arc<Capture>) {
26		let mut vec = self.active.write().expect("locked for writing");
27		if let Some(pos) = vec.iter().position(|v| Arc::ptr_eq(v, capture)) {
28			vec.swap_remove(pos);
29		}
30	}
31}