From 67f930a70c3bcac3366cfe3509245f54c90aa43b Mon Sep 17 00:00:00 2001 From: Daniel Verkamp Date: Mon, 10 Oct 2022 11:19:09 -0700 Subject: [PATCH] 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 Reviewed-by: Dennis Kempin Reviewed-by: Elie Kheirallah Auto-Submit: Daniel Verkamp --- devices/src/bus.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/devices/src/bus.rs b/devices/src/bus.rs index d08800946d..408d94dfa3 100644 --- a/devices/src/bus.rs +++ b/devices/src/bus.rs @@ -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(()) }