OSDN Git Service

rusty-gd: add intos for builders, so we can skip the formal build call
authorZach Johnson <zachoverflow@google.com>
Sun, 13 Dec 2020 11:22:22 +0000 (03:22 -0800)
committerZach Johnson <zachoverflow@google.com>
Fri, 18 Dec 2020 19:33:57 +0000 (11:33 -0800)
Bug: 171749953
Tag: #gd-refactor
Test: gd/cert/run --rhost SimpleHalTest
Change-Id: Ie68b9124b0d6f7b4c52ce882a6966995088d17f6

gd/packet/parser/packet_def.cc
gd/rust/hci/src/controller.rs
gd/rust/hci/src/lib.rs

index 4bdc81e..ef7660b 100644 (file)
@@ -1047,6 +1047,17 @@ void PacketDef::GenRustAccessStructImpls(std::ostream& s) const {
 }
 
 void PacketDef::GenRustBuilderStructImpls(std::ostream& s) const {
+  if (complement_ != nullptr) {
+    auto complement_root = complement_->GetRootDef();
+    auto complement_root_accessor = util::CamelCaseToUnderScore(complement_root->name_);
+    s << "impl CommandExpectations for " << name_ << "Builder {";
+    s << " type ResponseType = " << complement_->name_ << "Packet;";
+    s << " fn _to_response_type(pkt: EventPacket) -> Self::ResponseType { ";
+    s << complement_->name_ << "Packet::new(pkt." << complement_root_accessor << ".clone())";
+    s << " }";
+    s << "}";
+  }
+
   s << "impl " << name_ << "Builder {";
   s << "pub fn build(self) -> " << name_ << "Packet {";
   auto lineage = GetAncestors();
@@ -1104,6 +1115,13 @@ void PacketDef::GenRustBuilderStructImpls(std::ostream& s) const {
   s << "}\n";
 
   s << "}\n";
+  lineage = GetAncestors();
+  for (auto it = lineage.begin(); it != lineage.end(); it++) {
+    auto def = *it;
+    s << "impl Into<" << def->name_ << "Packet> for " << name_ << "Builder {";
+    s << " fn into(self) -> " << def->name_ << "Packet { self.build().into() }";
+    s << "}\n";
+  }
 }
 
 void PacketDef::GenRustDef(std::ostream& s) const {
index d3b5866..84a7f66 100644 (file)
@@ -15,7 +15,7 @@ module! {
 
 macro_rules! assert_success {
     ($hci:ident.send($builder:expr)) => {
-        assert!($hci.send($builder.build()).await.get_status() == ErrorCode::Success);
+        assert!($hci.send($builder).await.get_status() == ErrorCode::Success);
     };
 }
 
index da7d325..28a72fd 100644 (file)
@@ -1,11 +1,11 @@
 //! Host Controller Interface (HCI)
 
+/// HCI controller info
+pub mod controller;
 /// HCI errors
 pub mod error;
 /// HCI layer facade service
 pub mod facade;
-/// HCI controller info
-pub mod controller;
 
 use bt_common::time::Alarm;
 use bt_hal::HalExports;
@@ -61,7 +61,7 @@ async fn provide_hci(hal_exports: HalExports, rt: Arc<Runtime>) -> HciExports {
     };
 
     assert!(
-        exports.send(ResetBuilder {}.build()).await.get_status() == ErrorCode::Success,
+        exports.send(ResetBuilder {}).await.get_status() == ErrorCode::Success,
         "reset did not complete successfully"
     );