use std::cell::RefCell;
thread_local! {
static MESSAGE_BUFFER: RefCell<Vec<String>> = RefCell::new(Vec::with_capacity(12));
}
pub fn add_message(msg: String) {
MESSAGE_BUFFER.with(|buffer| buffer.borrow_mut().push(msg));
}
pub fn read_messages() -> Vec<String> {
MESSAGE_BUFFER.with(|buffer| buffer.borrow().clone())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_add_and_read_messages() {
MESSAGE_BUFFER.with(|buffer| buffer.borrow_mut().clear());
add_message("Message 1".to_string());
add_message("Message 2".to_string());
let messages = read_messages();
assert_eq!(
messages,
vec!["Message 1".to_string(), "Message 2".to_string()]
);
}
#[test]
fn test_empty_buffer() {
MESSAGE_BUFFER.with(|buffer| buffer.borrow_mut().clear());
let messages = read_messages();
assert!(messages.is_empty());
}
}