solar_interface/diagnostics/emitter/
mem.rs

1use crate::diagnostics::{Diag, Emitter};
2use solar_data_structures::sync::RwLock;
3use std::sync::Arc;
4
5/// An in-memory diagnostics emitter.
6///
7/// Diagnostics are pushed to a shared buffer as-is.
8///
9/// # Warning
10///
11/// Do **NOT** hold a read lock on the buffer across compiler passes as this will prevent the
12/// compiler from pushing diagnostics.
13pub struct InMemoryEmitter {
14    buffer: Arc<RwLock<Vec<Diag>>>,
15}
16
17impl InMemoryEmitter {
18    /// Creates a new emitter, returning the emitter itself and the buffer.
19    pub fn new() -> (Self, Arc<RwLock<Vec<Diag>>>) {
20        let buffer = Default::default();
21        (Self { buffer: Arc::clone(&buffer) }, buffer)
22    }
23}
24
25impl Emitter for InMemoryEmitter {
26    fn emit_diagnostic(&mut self, diagnostic: &mut crate::diagnostics::Diag) {
27        self.buffer.write().push(diagnostic.clone());
28    }
29}