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

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

rusty-gd: add support for struct fields in write_to()

Bug: 171749953
Tag: #gd-refactor
Test: gd/cert/run --rhost
Change-Id: I3f1a60733d2bf6bd77064523171f08dcb855ba7b
parent e6134a16
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -98,4 +98,6 @@ void StructField::GenRustGetter(std::ostream& s, Size start_offset, Size) const
  s << start_offset.bytes() + GetSize().bytes() << "]).unwrap();";
}

void StructField::GenRustWriter(std::ostream&, Size, Size) const {}
void StructField::GenRustWriter(std::ostream& s, Size, Size) const {
  s << "self." << GetName() << ".write_to(buffer);";
}
+24 −0
Original line number Diff line number Diff line
@@ -438,6 +438,30 @@ void StructDef::GenRustImpls(std::ostream& s) const {
  GenRustSizeField(s);
  s << "})}\n";

  // write_to function
  s << "fn write_to(&self, buffer: &mut BytesMut) {";
  fields = fields_.GetFieldsWithoutTypes({
      BodyField::kFieldType,
      CountField::kFieldType,
      PaddingField::kFieldType,
      ReservedField::kFieldType,
      SizeField::kFieldType,
  });

  for (auto const& field : fields) {
    auto start_field_offset = GetOffsetForField(field->GetName(), false);
    auto end_field_offset = GetOffsetForField(field->GetName(), true);

    if (start_field_offset.empty() && end_field_offset.empty()) {
      ERROR(field) << "Field location for " << field->GetName() << " is ambiguous, "
                   << "no method exists to determine field location from begin() or end().\n";
    }

    field->GenRustWriter(s, start_field_offset, end_field_offset);
  }

  s << "}\n";

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