[i2c] Avoid using expect()

Be consistent with the use of expect() and unwrap() and use only one of
them, if required. Also add a comment on why usage of unwrap() is safe
in main().

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
This commit is contained in:
Viresh Kumar 2021-10-27 12:14:11 +05:30
parent d021564ba3
commit 16a028f35d
2 changed files with 13 additions and 8 deletions

View File

@ -182,6 +182,14 @@ fn start_backend<D: 'static + I2cDevice + Send + Sync>(config: I2cConfiguration)
let i2c_map = i2c_map.clone();
let handle = spawn(move || loop {
// A separate thread is spawned for each socket and can connect to a separate guest.
// These are run in an infinite loop to not require the daemon to be restarted once a
// guest exits.
//
// There isn't much value in complicating code here to return an error from the
// threads, and so the code uses unwrap() instead. The panic on a thread won't cause
// trouble to other threads/guests or the main() function and should be safe for the
// daemon.
let backend = Arc::new(RwLock::new(
VhostUserI2cBackend::new(i2c_map.clone()).unwrap(),
));
@ -211,12 +219,7 @@ fn start_backend<D: 'static + I2cDevice + Send + Sync>(config: I2cConfiguration)
}
// No matter the result, we need to shut down the worker thread.
backend
.read()
.unwrap()
.exit_event
.write(1)
.expect("Shutting down worker thread");
backend.read().unwrap().exit_event.write(1).unwrap();
});
handles.push(handle);

View File

@ -51,6 +51,8 @@ pub enum Error {
DescriptorWriteFailed,
#[error("Failed to send notification")]
NotificationFailed,
#[error("Failed to create new EventFd: {0:?}")]
EventFdFailed(std::io::Error),
}
impl convert::From<Error> for io::Error {
@ -94,7 +96,7 @@ impl<D: I2cDevice> VhostUserI2cBackend<D> {
i2c_map,
event_idx: false,
mem: None,
exit_event: EventFd::new(EFD_NONBLOCK).expect("Creating exit eventfd"),
exit_event: EventFd::new(EFD_NONBLOCK).map_err(Error::EventFdFailed)?,
})
}
@ -295,7 +297,7 @@ impl<D: 'static + I2cDevice + Sync + Send> VhostUserBackendMut<VringRwLock, ()>
}
fn exit_event(&self, _thread_index: usize) -> Option<EventFd> {
Some(self.exit_event.try_clone().expect("Cloning exit eventfd"))
self.exit_event.try_clone().ok()
}
}