devices: bus: propagate error rather than unwrap()

The Suspendable trait's restore() function allows returning a Result, so
we can use the `?` operator to return errors in case the serde_json
deserialization process fails, rather than using `unwrap()` on the
Result, which would panic if the call failed. This doesn't make much
difference for this particular case, since it is in test code, but we
should provide a good example in case this code gets copied for other
implementations of the Suspendable trait.

BUG=None
TEST=cargo test -p devices

Change-Id: I8252fef5ee8f19c73f08971352984cd91766aa0c
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/3942512
Commit-Queue: Dennis Kempin <denniskempin@google.com>
Reviewed-by: Dennis Kempin <denniskempin@google.com>
Reviewed-by: Elie Kheirallah <khei@google.com>
Auto-Submit: Daniel Verkamp <dverkamp@chromium.org>
This commit is contained in:
Daniel Verkamp 2022-10-10 11:19:09 -07:00 committed by crosvm LUCI
parent 84761cbff3
commit 67f930a70c

View file

@ -574,8 +574,7 @@ mod tests {
}
fn restore(&mut self, data: &str) -> AnyhowResult<()> {
let deser = serde_json::from_str(data).context("error deserializing");
*self = deser.unwrap();
*self = serde_json::from_str(data).context("error deserializing")?;
Ok(())
}
@ -631,8 +630,7 @@ mod tests {
}
fn restore(&mut self, data: &str) -> AnyhowResult<()> {
let deser = serde_json::from_str(data).context("error deserializing");
*self = deser.unwrap();
*self = serde_json::from_str(data).context("error deserializing")?;
Ok(())
}