@@ -158,6 +158,38 @@ impl VmData {
158158 inner : Arc :: downgrade ( & self . inner ) ,
159159 }
160160 }
161+
162+ pub ( crate ) fn with_machine_running < F , R > ( & self , f : F ) -> Result < R , RunError >
163+ where
164+ F : FnOnce ( & VmMachineRunning ) -> R ,
165+ {
166+ let status = self . machine . read ( ) ;
167+ let running = match & * status {
168+ VmMachineState :: Running ( running) => running,
169+ _ => {
170+ return Err ( RunError :: ExitWithError ( anyhow ! (
171+ "VM is not in Running state"
172+ ) ) ) ;
173+ }
174+ } ;
175+ Ok ( f ( running) )
176+ }
177+
178+ pub ( crate ) fn with_machine_running_mut < F , R > ( & self , f : F ) -> Result < R , RunError >
179+ where
180+ F : FnOnce ( & mut VmMachineRunning ) -> R ,
181+ {
182+ let mut status = self . machine . write ( ) ;
183+ let running = match & mut * status {
184+ VmMachineState :: Running ( running) => running,
185+ _ => {
186+ return Err ( RunError :: ExitWithError ( anyhow ! (
187+ "VM is not in Running state"
188+ ) ) ) ;
189+ }
190+ } ;
191+ Ok ( f ( running) )
192+ }
161193}
162194
163195impl From < Arc < VmDataInner > > for VmData {
@@ -207,23 +239,6 @@ impl VmDataWeak {
207239 inner. status . store ( VMStatus :: Stopped ) ;
208240 }
209241 }
210-
211- pub ( crate ) fn with_machine_running < F , R > ( & self , f : F ) -> Result < R , RunError >
212- where
213- F : FnOnce ( & VmMachineRunning ) -> R ,
214- {
215- let vmdata = self . try_upgrade ( ) ?;
216- let status = vmdata. machine . read ( ) ;
217- let running = match & * status {
218- VmMachineState :: Running ( running) => running,
219- _ => {
220- return Err ( RunError :: ExitWithError ( anyhow ! (
221- "VM is not in Running state"
222- ) ) ) ;
223- }
224- } ;
225- Ok ( f ( running) )
226- }
227242}
228243
229244impl Debug for VmDataWeak {
0 commit comments