use std::cmp::PartialEq;
use std::default::Default;
use anyhow::Context;
use serde::Deserialize;
use serde::Serialize;
use snapshot::AnySnapshot;
use vm_control::DeviceId;
use vm_control::PlatformDeviceId;
use crate::BusDevice;
use crate::Suspendable;
#[derive(Copy, Clone, Serialize, Deserialize, Eq, PartialEq, Debug)]
pub struct MockDevice {
suspendable_switch: bool,
}
impl MockDevice {
pub fn new() -> Self {
Self {
suspendable_switch: false,
}
}
pub fn toggle_suspendable_switch(&mut self) {
self.suspendable_switch ^= true
}
}
impl Default for MockDevice {
fn default() -> Self {
Self::new()
}
}
impl BusDevice for MockDevice {
fn device_id(&self) -> DeviceId {
PlatformDeviceId::Mock.into()
}
fn debug_label(&self) -> String {
"mock device".to_owned()
}
}
impl Suspendable for MockDevice {
fn snapshot(&mut self) -> anyhow::Result<AnySnapshot> {
AnySnapshot::to_any(self).context("error serializing")
}
fn restore(&mut self, data: AnySnapshot) -> anyhow::Result<()> {
*self = AnySnapshot::from_any(data).context("error deserializing")?;
Ok(())
}
fn sleep(&mut self) -> anyhow::Result<()> {
Ok(())
}
fn wake(&mut self) -> anyhow::Result<()> {
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::suspendable_tests;
suspendable_tests!(
mock_device,
MockDevice::new(),
MockDevice::toggle_suspendable_switch
);
}