use std::ops::Deref;
use interface::{Data, Read, UniqueIdentifier, Update, Write};
pub struct Once<U: UniqueIdentifier> {
data: Option<U::DataType>,
}
impl<U: UniqueIdentifier> Once<U> {
pub fn new() -> Self {
Self { data: None }
}
}
impl<U: UniqueIdentifier> Default for Once<U> {
fn default() -> Self {
Self::new()
}
}
impl<U: UniqueIdentifier> Update for Once<U> {}
impl<U> Read<U> for Once<U>
where
U: UniqueIdentifier,
<U as UniqueIdentifier>::DataType: Clone,
{
fn read(&mut self, data: Data<U>) {
self.data = Some(data.deref().clone());
}
}
impl<U, V> Write<V> for Once<U>
where
U: UniqueIdentifier,
V: UniqueIdentifier<DataType = Option<U::DataType>>,
{
fn write(&mut self) -> Option<Data<V>> {
Some(Data::new(self.data.take()))
}
}