underhill_core/
vmbus_relay_unit.rsuse inspect::Inspect;
use pal_async::task::Spawn;
use state_unit::NameInUse;
use state_unit::SpawnedUnit;
use state_unit::StateUnit;
use state_unit::UnitBuilder;
use state_unit::run_async_unit;
use vmbus_relay::HostVmbusTransport;
use vmcore::save_restore::RestoreError;
use vmcore::save_restore::SaveError;
use vmcore::save_restore::SavedStateBlob;
pub struct VmbusRelayHandle {
unit: SpawnedUnit<VmbusRelayUnit>,
}
impl VmbusRelayHandle {
pub fn new(
spawner: &impl Spawn,
builder: UnitBuilder<'_>,
relay: HostVmbusTransport,
) -> Result<Self, NameInUse> {
let unit = builder.spawn(spawner, |recv| run_async_unit(VmbusRelayUnit(relay), recv))?;
Ok(Self { unit })
}
pub async fn teardown(self) {
self.unit.remove().await;
}
}
#[derive(Inspect)]
#[inspect(transparent)]
struct VmbusRelayUnit(HostVmbusTransport);
impl StateUnit for &'_ VmbusRelayUnit {
async fn start(&mut self) {
self.0.start();
}
async fn stop(&mut self) {
self.0.stop().await;
}
async fn reset(&mut self) -> anyhow::Result<()> {
Ok(())
}
async fn save(&mut self) -> Result<Option<SavedStateBlob>, SaveError> {
Ok(Some(SavedStateBlob::new(self.0.save().await)))
}
async fn restore(&mut self, state: SavedStateBlob) -> Result<(), RestoreError> {
self.0
.restore(state.parse()?)
.await
.map_err(RestoreError::Other)
}
async fn post_restore(&mut self) -> anyhow::Result<()> {
Ok(())
}
}