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

Commit cf43afcc authored by Zach Johnson's avatar Zach Johnson
Browse files

rusty-gd: take vector sizes into account

Bug: 171749953
Tag: #gd-refactor
Test: gd/cert/run --rhost
Change-Id: I6705aa1924d927a51c8bced0eea7c0f105a2c1bc
parent 49363e4d
Loading
Loading
Loading
Loading
+2 −17
Original line number Original line Diff line number Diff line
@@ -866,21 +866,6 @@ void PacketDef::GenRustStructFieldNames(std::ostream& s) const {
  }
  }
}
}


void PacketDef::GenRustStructSizeField(std::ostream& s) const {
  int size = 0;
  auto fields = fields_.GetFieldsWithoutTypes({
      BodyField::kFieldType,
      CountField::kFieldType,
  });
  for (int i = 0; i < fields.size(); i++) {
    size += fields[i]->GetSize().bits();
  }
  if (size % 8 != 0) {
    ERROR() << "Packet size is not a multiple of 8!\n";
  }
  s << size / 8;
}

void PacketDef::GenRustStructImpls(std::ostream& s) const {
void PacketDef::GenRustStructImpls(std::ostream& s) const {
  s << "impl " << name_ << "Data {";
  s << "impl " << name_ << "Data {";


@@ -1026,8 +1011,8 @@ void PacketDef::GenRustStructImpls(std::ostream& s) const {
  }
  }
  s << "}\n";
  s << "}\n";


  s << "pub fn get_size(&self) -> usize {";
  s << "fn get_size(&self) -> usize {";
  GenRustStructSizeField(s);
  GenSizeRetVal(s);
  s << "}\n";
  s << "}\n";
  s << "}\n";
  s << "}\n";
}
}
+0 −2
Original line number Original line Diff line number Diff line
@@ -73,8 +73,6 @@ class PacketDef : public ParentDef {


  void GenRustStructFieldNames(std::ostream& s) const;
  void GenRustStructFieldNames(std::ostream& s) const;


  void GenRustStructSizeField(std::ostream& s) const;

  void GenRustStructImpls(std::ostream& s) const;
  void GenRustStructImpls(std::ostream& s) const;


  void GenRustAccessStructImpls(std::ostream& s) const;
  void GenRustAccessStructImpls(std::ostream& s) const;
+29 −1
Original line number Original line Diff line number Diff line
@@ -609,7 +609,8 @@ void ParentDef::GenRustWriteToFields(std::ostream& s) const {
        const auto& vector_name = field_name + "_bytes";
        const auto& vector_name = field_name + "_bytes";
        const VectorField* vector = (VectorField*)sized_field;
        const VectorField* vector = (VectorField*)sized_field;
        if (vector->element_size_.empty() || vector->element_size_.has_dynamic()) {
        if (vector->element_size_.empty() || vector->element_size_.has_dynamic()) {
          s << "let " << vector_name + " = self." << field_name << ".iter().fold(0, |acc, x| acc + x.get_size());";
          s << "let " << vector_name + " = self." << field_name
            << ".iter().fold(0, |acc, x| acc + x.get_total_size());";
        } else {
        } else {
          s << "let " << vector_name + " = self." << field_name << ".len() * ((" << vector->element_size_ << ") / 8);";
          s << "let " << vector_name + " = self." << field_name << ".len() * ((" << vector->element_size_ << ") / 8);";
        }
        }
@@ -628,3 +629,30 @@ void ParentDef::GenRustWriteToFields(std::ostream& s) const {
    field->GenRustWriter(s, start_field_offset, end_field_offset);
    field->GenRustWriter(s, start_field_offset, end_field_offset);
  }
  }
}
}

void ParentDef::GenSizeRetVal(std::ostream& s) const {
  int size = 0;
  auto fields = fields_.GetFieldsWithoutTypes({
      BodyField::kFieldType,
      CountField::kFieldType,
  });
  for (int i = 0; i < fields.size(); i++) {
    size += fields[i]->GetSize().bits();
  }
  if (size % 8 != 0) {
    ERROR() << "size is not a multiple of 8!\n";
  }
  s << size / 8;

  fields = fields_.GetFieldsWithTypes({
      VectorField::kFieldType,
  });
  for (int i = 0; i < fields.size(); i++) {
    const VectorField* vector = (VectorField*)fields[i];
    if (vector->element_size_.empty() || vector->element_size_.has_dynamic()) {
      s << " + self." << vector->GetName() << ".iter().fold(0, |acc, x| acc + x.get_total_size())";
    } else {
      s << " + (self." << vector->GetName() << ".len() * ((" << vector->element_size_ << ") / 8))";
    }
  }
}
+2 −0
Original line number Original line Diff line number Diff line
@@ -89,4 +89,6 @@ class ParentDef : public TypeDef {
  bool HasChildEnums() const;
  bool HasChildEnums() const;


  void GenRustWriteToFields(std::ostream& s) const;
  void GenRustWriteToFields(std::ostream& s) const;

  void GenSizeRetVal(std::ostream& s) const;
};
};
+3 −20
Original line number Original line Diff line number Diff line
@@ -342,21 +342,6 @@ void StructDef::GenRustFieldNames(std::ostream& s) const {
  }
  }
}
}


void StructDef::GenRustSizeField(std::ostream& s) const {
  int size = 0;
  auto fields = fields_.GetFieldsWithoutTypes({
      BodyField::kFieldType,
      CountField::kFieldType,
      SizeField::kFieldType,
  });
  for (const auto& field : fields) {
    size += field->GetSize().bytes();
  }
  if (fields.size() > 0) {
    s << size;
  }
}

void StructDef::GenRustDeclarations(std::ostream& s) const {
void StructDef::GenRustDeclarations(std::ostream& s) const {
  s << "#[derive(Debug, Clone)] ";
  s << "#[derive(Debug, Clone)] ";
  s << "pub struct " << name_ << "{";
  s << "pub struct " << name_ << "{";
@@ -421,11 +406,9 @@ void StructDef::GenRustImpls(std::ostream& s) const {
  GenRustWriteToFields(s);
  GenRustWriteToFields(s);
  s << "}\n";
  s << "}\n";


  if (fields.size() > 0) {
  s << "fn get_total_size(&self) -> usize {";
    s << "pub fn get_size(&self) -> usize {";
  GenSizeRetVal(s);
    GenRustSizeField(s);
  s << "}";
  s << "}";
  }
  s << "}\n";
  s << "}\n";
}
}


Loading