[fidl][rust] Explain Result decoding optimization
While it is not impossible to understand, I think a comment helps.
I also believe that moving the code responsible for constructing default
value into the corresponding match branch makes the intent a bit easier
to see as well.
Change-Id: I1e6dbe457130aa1971577bfa3412d1952433fc8c
diff --git a/garnet/public/lib/fidl/rust/fidl/src/encoding.rs b/garnet/public/lib/fidl/rust/fidl/src/encoding.rs
index dca3b14..6cbc732 100644
--- a/garnet/public/lib/fidl/rust/fidl/src/encoding.rs
+++ b/garnet/public/lib/fidl/rust/fidl/src/encoding.rs
@@ -1870,9 +1870,12 @@
fidl_decode!(val, decoder)?;
break;
}
- _ => {}
+ Err(_) => {
+ // Only construct an empty instance of the nested type if we have an
+ // Err(T) value. Now `loop` to go into the first branch.
+ *self = Ok(<O as Decodable>::new_empty())
+ }
}
- *self = Ok(<O as Decodable>::new_empty())
}
Ok(())
}
@@ -1884,9 +1887,12 @@
fidl_decode!(val, decoder)?;
break;
}
- _ => {}
+ Ok(_) => {
+ // Only construct an empty instance of the nested type if we have an
+ // Ok(T) value. Now `loop` to go into the first branch.
+ *self = Err(<E as Decodable>::new_empty())
+ }
}
- *self = Err(<E as Decodable>::new_empty())
}
Ok(())
}