use std::sync::Arc;
pub trait KeyLogSink: Send + Sync + std::fmt::Debug {
fn write_line(&self, line: &str);
}
impl<T: KeyLogSink + ?Sized> KeyLogSink for Arc<T> {
#[inline]
fn write_line(&self, line: &str) {
(**self).write_line(line);
}
}
#[derive(Debug, Default, Clone, Copy)]
pub struct NoopKeyLogSink;
impl KeyLogSink for NoopKeyLogSink {
#[inline]
fn write_line(&self, _line: &str) {}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn noop_sink_drops_lines() {
let s = NoopKeyLogSink;
s.write_line("CLIENT_RANDOM aaa bbb\n");
}
#[test]
fn arc_blanket_forwards_to_inner() {
use parking_lot::Mutex;
#[derive(Debug, Default)]
struct Capture(Mutex<Vec<String>>);
impl KeyLogSink for Capture {
fn write_line(&self, line: &str) {
self.0.lock().push(line.to_owned());
}
}
let inner = Arc::new(Capture::default());
let sink: Arc<dyn KeyLogSink> = inner.clone();
sink.write_line("LINE_A\n");
sink.write_line("LINE_B\n");
assert_eq!(inner.0.lock().as_slice(), &["LINE_A\n", "LINE_B\n"]);
}
}