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

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

rusty-gd: start writing scalar fields in write_to()

Bug: 171749953
Tag: #gd-refactor
Test: gd/cert/run --rhost
Change-Id: I07885881aa19538e62bd9cdcdac79127349f60b6
parent d9904535
Loading
Loading
Loading
Loading
+32 −1
Original line number Diff line number Diff line
@@ -180,4 +180,35 @@ void ScalarField::GenRustGetter(std::ostream& s, Size start_offset, Size end_off
  }
}

void ScalarField::GenRustWriter(std::ostream&, Size, Size) const {}
void ScalarField::GenRustWriter(std::ostream& s, Size start_offset, Size end_offset) const {
  Size size = GetSize();
  int num_leading_bits = GetRustBitOffset(s, start_offset, end_offset, GetSize());

  s << "let " << GetName() << " = self." << GetName() << ";";
  if (util::RoundSizeUp(size.bits()) != size.bits()) {
    uint64_t mask = 0;
    for (int i = 0; i < size.bits(); i++) {
      mask <<= 1;
      mask |= 1;
    }
    s << "let " << GetName() << " = ";
    s << GetName() << " & 0x" << std::hex << mask << std::dec << ";";
  }

  int access_offset = 0;
  if (num_leading_bits != 0) {
    access_offset = -1;
    uint64_t mask = 0;
    for (int i = 0; i < num_leading_bits; i++) {
      mask <<= 1;
      mask |= 1;
    }
    s << "let " << GetName() << " = (" << GetName() << " << " << num_leading_bits << ") | ("
      << "(buffer[" << start_offset.bytes() << "] as " << GetRustDataType() << ") & 0x" << std::hex << mask << std::dec
      << ");";
  }

  s << "buffer[" << start_offset.bytes() + access_offset << ".."
    << start_offset.bytes() + GetSize().bytes() + access_offset << "].copy_from_slice(&" << GetName()
    << ".to_le_bytes());";
}
+4 −0
Original line number Diff line number Diff line
@@ -881,6 +881,10 @@ void PacketDef::GenRustStructImpls(std::ostream& s) const {

  fields = fields_.GetFieldsWithoutTypes({
      BodyField::kFieldType,
      CountField::kFieldType,
      PaddingField::kFieldType,
      ReservedField::kFieldType,
      SizeField::kFieldType,
  });

  for (auto const& field : fields) {