Note: This document covers API impact only. For more details, see the ABI compatibility page

Change an enum from flexible to strict

Overview

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

Initial State {#init}

FIDL {#fidl-init}

  1. flexible enum Color : int32 {
  2. RED = 1;
  3. BLUE = 2;
  4. };

Dart {#dart-init}

  1. fidllib.Color complement(fidllib.Color color) {
  2. if (color.isUnknown()) {
  3. return color;
  4. }
  5. switch (color) {
  6. case fidllib.Color.blue:
  7. return fidllib.Color.red;
  8. case fidllib.Color.red:
  9. return fidllib.Color.blue;
  10. default:
  11. return null;
  12. }
  13. }

Go {#go-init}

  1. func complement(color lib.Color) lib.Color {
  2. if color.IsUnknown() {
  3. return color
  4. }
  5. switch color {
  6. case lib.ColorBlue:
  7. return lib.ColorRed
  8. case lib.ColorRed:
  9. return lib.ColorBlue
  10. default:
  11. return color
  12. }
  13. }

HLCPP {#hlcpp-init}

  1. fidl_test::Color complement(fidl_test::Color color) {
  2. if (color.IsUnknown()) {
  3. return color;
  4. }
  5. switch (color) {
  6. case fidl_test::Color::RED:
  7. return fidl_test::Color::BLUE;
  8. case fidl_test::Color::BLUE:
  9. return fidl_test::Color::RED;
  10. default:
  11. return color;
  12. }
  13. }

LLCPP {#llcpp-init}

  1. fidl_test::Color complement(fidl_test::Color color) {
  2. if (color.IsUnknown()) {
  3. return color;
  4. }
  5. switch (color) {
  6. case fidl_test::Color::RED:
  7. return fidl_test::Color::BLUE;
  8. case fidl_test::Color::BLUE:
  9. return fidl_test::Color::RED;
  10. default:
  11. return color;
  12. }
  13. }

Rust {#rust-init}

  1. fn complement(color: &fidl_lib::Color) -> Option<fidl_lib::Color> {
  2. match color.validate() {
  3. Ok(fidl_lib::Color::Red) => Some(fidl_lib::Color::Blue),
  4. Ok(fidl_lib::Color::Blue) => Some(fidl_lib::Color::Red),
  5. _ => None,
  6. }
  7. }

Update Source Code {#step-1}

Dart {#dart-1}

  1. fidllib.Color complement(fidllib.Color color) {
  2. - if (color.isUnknown()) {
  3. - return color;
  4. - }
  5. + assert(color.isUnknown() == false);
  6. switch (color) {
  7. case fidllib.Color.blue:
  8. return fidllib.Color.red;
  9. case fidllib.Color.red:
  10. return fidllib.Color.blue;
  11. default:
  12. return null;
  13. }
  14. }

Go {#go-1}

  • Remove usages of any flexible specific APIs
  1. func complement(color lib.Color) lib.Color {
  2. - if color.IsUnknown() {
  3. - return color
  4. - }
  5. switch color {
  6. case lib.ColorBlue:
  7. return lib.ColorRed
  8. case lib.ColorRed:
  9. return lib.ColorBlue
  10. default:
  11. return color
  12. }
  13. }

HLCPP {#hlcpp-1}

  • Remove usages of any flexible specific APIs
  1. fidl_test::Color complement(fidl_test::Color color) {
  2. - if (color.IsUnknown()) {
  3. - return color;
  4. - }
  5. switch (color) {
  6. case fidl_test::Color::RED:
  7. return fidl_test::Color::BLUE;
  8. case fidl_test::Color::BLUE:
  9. return fidl_test::Color::RED;
  10. default:
  11. return color;
  12. }
  13. }

LLCPP {#llcpp-1}

  • Remove usages of any flexible specific APIs
  1. fidl_test::Color complement(fidl_test::Color color) {
  2. - if (color.IsUnknown()) {
  3. - return color;
  4. - }
  5. switch (color) {
  6. case fidl_test::Color::RED:
  7. return fidl_test::Color::BLUE;
  8. case fidl_test::Color::BLUE:
  9. return fidl_test::Color::RED;
  10. default:
  11. return color;
  12. }
  13. }

Rust {#rust-1}

  • Remove usages of any flexible specific APIs
  • Allow unreachable patterns and add an underscore arm to any match statements on the enum.
  1. fn complement(color: &fidl_lib::Color) -> Option<fidl_lib::Color> {
  2. - match color.validate() {
  3. - Ok(fidl_lib::Color::Red) => Some(fidl_lib::Color::Blue),
  4. - Ok(fidl_lib::Color::Blue) => Some(fidl_lib::Color::Red),
  5. + #[allow(unreachable_patterns)]
  6. + match color {
  7. + fidl_lib::Color::Red => Some(fidl_lib::Color::Blue),
  8. + fidl_lib::Color::Blue => Some(fidl_lib::Color::Red),
  9. _ => None,
  10. }
  11. }

Update FIDL Library {#step-2}

  • Change from flexible to strict
  1. - flexible enum Color : int32 {
  2. + strict enum Color : int32 {
  3. RED = 1;
  4. BLUE = 2;
  5. };

Update Source Code {#step-3}

Rust {#rust-3}

  • Remove the unreachable patterns attribute and underscore arm.
  1. - fn complement(color: &fidl_lib::Color) -> Option<fidl_lib::Color> {
  2. - #[allow(unreachable_patterns)]
  3. + fn complement(color: &fidl_lib::Color) -> fidl_lib::Color {
  4. match color {
  5. - fidl_lib::Color::Red => Some(fidl_lib::Color::Blue),
  6. - fidl_lib::Color::Blue => Some(fidl_lib::Color::Red),
  7. - _ => None,
  8. + fidl_lib::Color::Red => fidl_lib::Color::Blue,
  9. + fidl_lib::Color::Blue => fidl_lib::Color::Red,
  10. }
  11. }