Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9b96303d authored by Zach Johnson's avatar Zach Johnson
Browse files

rusty-gd: add packet accessors

accessors for all ancestor fields too
stubbed against accessor functions (until inner child is ready)

Bug: 171749953
Tag: #gd-refactor
Test: gd/cert/run --rhost SimpleHalTest
Change-Id: I8dea0a1380b70a8b050e277dc3a388751000f7bf
parent bce97313
Loading
Loading
Loading
Loading
+30 −6
Original line number Diff line number Diff line
@@ -922,16 +922,40 @@ void PacketDef::GenRustAccessStructImpls(std::ostream& s) const {
    s << " fn specialize(self) -> " << name_ << "Child { unimplemented!(); }";
  }
  s << " fn new(root: Rc<" << GetRootDef()->name_ << "Data>) -> Self { unimplemented!(); }";

  auto lineage = GetAncestors();
  lineage.push_back(this);
  for (auto it = lineage.begin(); it != lineage.end(); it++) {
    auto def = *it;
    auto accessor_name = util::CamelCaseToUnderScore(def->name_);
    s << " fn access_" << accessor_name << "(&self) -> &" << def->name_ << "Data { unimplemented!(); }";

    auto fields = def->fields_.GetFieldsWithoutTypes({
        BodyField::kFieldType,
        CountField::kFieldType,
        PaddingField::kFieldType,
        ReservedField::kFieldType,
        SizeField::kFieldType,
        PayloadField::kFieldType,
    });

    for (auto const& field : fields) {
      s << "pub fn get_" << field->GetName() << "(&self) -> &" << field->GetRustDataType() << "{";
      s << " &self.access_" << accessor_name << "()." << field->GetName();
      s << "}\n";
    }
  }

  s << "}\n";

  auto parent = parent_;
  while (parent != nullptr) {
    s << "impl Into<" << parent->name_ << "Packet> for " << name_ << "Packet {";
    s << " fn into(self) -> " << parent->name_ << "Packet {";
    s << parent->name_ << "Packet::new(self.root)";
  lineage = GetAncestors();
  for (auto it = lineage.begin(); it != lineage.end(); it++) {
    auto def = *it;
    s << "impl Into<" << def->name_ << "Packet> for " << name_ << "Packet {";
    s << " fn into(self) -> " << def->name_ << "Packet {";
    s << def->name_ << "Packet::new(self.root)";
    s << " }";
    s << "}\n";
    parent = parent->parent_;
  }
}

+10 −0
Original line number Diff line number Diff line
@@ -490,3 +490,13 @@ const ParentDef* ParentDef::GetRootDef() const {

  return parent_->GetRootDef();
}

std::vector<const ParentDef*> ParentDef::GetAncestors() const {
  std::vector<const ParentDef*> res;
  auto parent = parent_;
  while (parent != nullptr) {
    res.push_back(parent);
    parent = parent->parent_;
  }
  return res;
}
+2 −0
Original line number Diff line number Diff line
@@ -63,6 +63,8 @@ class ParentDef : public TypeDef {

  const ParentDef* GetRootDef() const;

  std::vector<const ParentDef*> GetAncestors() const;

  FieldList fields_;

  ParentDef* parent_{nullptr};