34 lines
825 B
Rust
34 lines
825 B
Rust
use std::sync::{Arc, Mutex};
|
|
|
|
use crate::interaction::Interaction;
|
|
|
|
pub struct Buffer {
|
|
int_buffer: Arc<Mutex<Vec<String>>>,
|
|
out_buffer: Arc<Mutex<Vec<String>>>,
|
|
}
|
|
|
|
impl Buffer {
|
|
pub fn new(int_buffer: Arc<Mutex<Vec<String>>>, out_buffer: Arc<Mutex<Vec<String>>>) -> Self {
|
|
Self {
|
|
int_buffer,
|
|
out_buffer,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Interaction for Buffer {
|
|
fn input(&self) -> Result<String, String> {
|
|
let mut int_buffer = self.int_buffer.lock().unwrap();
|
|
if let Some(input_string) = int_buffer.pop() {
|
|
Ok(input_string)
|
|
} else {
|
|
Err(String::from("internal_buffer is empty"))
|
|
}
|
|
}
|
|
|
|
fn output(&self, output: String) {
|
|
let mut out_buffer = self.out_buffer.lock().unwrap();
|
|
out_buffer.push(output)
|
|
}
|
|
}
|