use std::{
io::{BufRead, Write},
ops::{Deref, DerefMut},
};
use crate::raw::WaitForEvent;
use super::WaitForIn;
pub enum ValueOrMut<'a, T> {
Value(T),
Mut(&'a mut T),
}
pub trait IoProvider: WaitForIn + WaitForEvent {
type Out: Write;
type In: BufRead + WaitForIn + WaitForEvent;
fn get_out(&mut self) -> ValueOrMut<'_, Self::Out>;
fn get_in(&mut self) -> ValueOrMut<'_, Self::In>;
fn is_out_terminal(&self) -> bool {
false
}
fn is_in_terminal(&self) -> bool {
false
}
fn is_out_raw(&self) -> bool {
false
}
}
impl<T> AsRef<T> for ValueOrMut<'_, T> {
fn as_ref(&self) -> &T {
match self {
ValueOrMut::Value(v) => v,
ValueOrMut::Mut(v) => v,
}
}
}
impl<T> AsMut<T> for ValueOrMut<'_, T> {
fn as_mut(&mut self) -> &mut T {
match self {
ValueOrMut::Value(v) => v,
ValueOrMut::Mut(v) => v,
}
}
}
impl<T> Deref for ValueOrMut<'_, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
self.as_ref()
}
}
impl<T> DerefMut for ValueOrMut<'_, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.as_mut()
}
}