tree: 7a0b60f22c7a8aa879e77451822c1ab3f8134409 [path history] [tgz]
  1. dart/
  2. fidl/
  3. go/
  4. hlcpp/
  5. llcpp/
  6. rust/
  7. BUILD.gn
  8. README.md
  9. test.json
  10. test_gn_sidecar.json
src/tests/fidl/source_compatibility/enum-flexible-strict/README.md

Change an enum from flexible to strict

Overview

-initstep 1step 2step 3
fidllinklink
dartlinklink
golinklink
hlcpplinklink
llcpplinklink
rustlinklinklink

Initial State

FIDL

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

Dart

fidllib.Color complement(fidllib.Color color) {
  if (color.isUnknown()) {
    return color;
  }
  switch (color) {
    case fidllib.Color.blue:
      return fidllib.Color.red;
    case fidllib.Color.red:
      return fidllib.Color.blue;
    default:
      return null;
  }
}

Go

func complement(color lib.Color) lib.Color {
	if color.IsUnknown() {
		return color
	}
	switch color {
	case lib.ColorBlue:
		return lib.ColorRed
	case lib.ColorRed:
		return lib.ColorBlue
	default:
		return color
	}
}

HLCPP

fidl_test::Color complement(fidl_test::Color color) {
  if (color.IsUnknown()) {
    return color;
  }
  switch (color) {
    case fidl_test::Color::RED:
      return fidl_test::Color::BLUE;
    case fidl_test::Color::BLUE:
      return fidl_test::Color::RED;
    default:
      return color;
  }
}

LLCPP

fidl_test::Color complement(fidl_test::Color color) {
  if (color.IsUnknown()) {
    return color;
  }
  switch (color) {
    case fidl_test::Color::RED:
      return fidl_test::Color::BLUE;
    case fidl_test::Color::BLUE:
      return fidl_test::Color::RED;
    default:
      return color;
  }
}

Rust

fn complement(color: &fidl_lib::Color) -> Option<fidl_lib::Color> {
    match color.validate() {
        Ok(fidl_lib::Color::Red) => Some(fidl_lib::Color::Blue),
        Ok(fidl_lib::Color::Blue) => Some(fidl_lib::Color::Red),
        _ => None,
    }
}

Update Source Code

Dart

  fidllib.Color complement(fidllib.Color color) {
-   if (color.isUnknown()) {
-     return color;
-   }
+   assert(color.isUnknown() == false);
    switch (color) {
      case fidllib.Color.blue:
        return fidllib.Color.red;
      case fidllib.Color.red:
        return fidllib.Color.blue;
      default:
        return null;
    }
  }
  

Go

  • Remove usages of any flexible specific APIs
  func complement(color lib.Color) lib.Color {
- 	if color.IsUnknown() {
- 		return color
- 	}
  	switch color {
  	case lib.ColorBlue:
  		return lib.ColorRed
  	case lib.ColorRed:
  		return lib.ColorBlue
  	default:
  		return color
  	}
  }
  

HLCPP

  • Remove usages of any flexible specific APIs
  fidl_test::Color complement(fidl_test::Color color) {
-   if (color.IsUnknown()) {
-     return color;
-   }
    switch (color) {
      case fidl_test::Color::RED:
        return fidl_test::Color::BLUE;
      case fidl_test::Color::BLUE:
        return fidl_test::Color::RED;
      default:
        return color;
    }
  }

LLCPP

  • Remove usages of any flexible specific APIs
  fidl_test::Color complement(fidl_test::Color color) {
-   if (color.IsUnknown()) {
-     return color;
-   }
    switch (color) {
      case fidl_test::Color::RED:
        return fidl_test::Color::BLUE;
      case fidl_test::Color::BLUE:
        return fidl_test::Color::RED;
      default:
        return color;
    }
  }

Rust

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

Update FIDL Library

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

Update Source Code

Rust

  • Remove the unreachable patterns attribute and underscore arm.
- fn complement(color: &fidl_lib::Color) -> Option<fidl_lib::Color> {
-     #[allow(unreachable_patterns)]
+ fn complement(color: &fidl_lib::Color) -> fidl_lib::Color {
      match color {
-         fidl_lib::Color::Red => Some(fidl_lib::Color::Blue),
-         fidl_lib::Color::Blue => Some(fidl_lib::Color::Red),
-         _ => None,
+         fidl_lib::Color::Red => fidl_lib::Color::Blue,
+         fidl_lib::Color::Blue => fidl_lib::Color::Red,
      }
  }