Change a bits from flexible to strict

Overview

- init step 1 step 2 step 3
fidl link link
dart link
go link
hlcpp link link link
llcpp link link
rust link link

Initial State

FIDL

type Flags = flexible bits {
    OPTION_A = 1;
    OPTION_B = 2;
};

Dart

int useBits(fidllib.Flags bits) {
  if (bits.hasUnknownBits()) {
    return bits.getUnknownBits();
  }

  var result = fidllib.Flags.$none;
  if ((bits & fidllib.Flags.optionA).$value != 0) {
    result |= fidllib.Flags.$mask;
  }
  return result.$value;
}

Go

func useBits(bits lib.Flags) uint32 {
    if bits.HasUnknownBits() {
        return uint32(bits.GetUnknownBits())
    }
    var result lib.Flags = 0
    if bits.HasBits(lib.FlagsOptionA) {
        result |= lib.Flags_Mask
    }
    return uint32(result)
}

HLCPP

fidl_test::Flags use_bits(fidl_test::Flags bits) {
  fidl_test::Flags result = bits | fidl_test::Flags::OPTION_A;
  auto truncated = fidl_test::Flags::TruncatingUnknown(uint32_t(result));
  ZX_ASSERT(!truncated.has_unknown_bits());

  result &= fidl_test::Flags::kMask;
  ZX_ASSERT(truncated == result);
  return result;
}

LLCPP

uint32_t use_bits(fidl_test::wire::Flags bits) {
  auto result = fidl_test::wire::Flags::TruncatingUnknown(7u);
  if (bits & fidl_test::wire::Flags::kOptionA) {
    result |= fidl_test::wire::Flags::kMask;
    printf("%d\n", uint32_t(result.unknown_bits()));
  }
  return uint32_t(result);
}

Rust

fn use_bits(bits: &fidl_lib::Flags) -> fidl_lib::Flags {
    let mut result = fidl_lib::Flags::empty();
    if bits.contains(fidl_lib::Flags::OptionA) {
        result.set(fidl_lib::Flags::all(), true);
        assert!(!result.has_unknown_bits());
    }
    return result;
}

Update Source Code

HLCPP

  • Remove any usages of the bits mask member. This generated value is renamed for strict bits, so it must be temporarily hardcoded.
  • Remove usages of flexible-only APIs
  fidl_test::Flags use_bits(fidl_test::Flags bits) {
    fidl_test::Flags result = bits | fidl_test::Flags::OPTION_A;
-   auto truncated = fidl_test::Flags::TruncatingUnknown(uint32_t(result));
-   ZX_ASSERT(!truncated.has_unknown_bits());
- 
-   result &= fidl_test::Flags::kMask;
-   ZX_ASSERT(truncated == result);
+   fidl_test::Flags mask = fidl_test::Flags::OPTION_A | fidl_test::Flags::OPTION_B;
+   result &= mask;
    return result;
  }

LLCPP

  • Remove usages of flexible-specific APIs
  uint32_t use_bits(fidl_test::wire::Flags bits) {
    auto result = fidl_test::wire::Flags::TruncatingUnknown(7u);
    if (bits & fidl_test::wire::Flags::kOptionA) {
      result |= fidl_test::wire::Flags::kMask;
-     printf("%d\n", uint32_t(result.unknown_bits()));
    }
    return uint32_t(result);
  }

Rust

  • Remove usages of flexible-specific APIs
  fn use_bits(bits: &fidl_lib::Flags) -> fidl_lib::Flags {
      let mut result = fidl_lib::Flags::empty();
      if bits.contains(fidl_lib::Flags::OptionA) {
          result.set(fidl_lib::Flags::all(), true);
-         assert!(!result.has_unknown_bits());
      }
      return result;
  }

Update FIDL Library

  • Switch from flexible to strict
- type Flags = flexible bits {
+ type Flags = strict bits {
      OPTION_A = 1;
      OPTION_B = 2;
  };

Update Source Code

HLCPP

  • Use the renamed FlagsMask constant
  fidl_test::Flags use_bits(fidl_test::Flags bits) {
    fidl_test::Flags result = bits | fidl_test::Flags::OPTION_A;
-   fidl_test::Flags mask = fidl_test::Flags::OPTION_A | fidl_test::Flags::OPTION_B;
-   result &= mask;
+   result &= fidl_test::FlagsMask;
    return result;
  }