blob: cc29cdbf7896ce43ba3c37ca2f639d75d9062aa9 [file] [log] [blame] [view]
<!-- WARNING: This file is machine generated by //src/tests/fidl/source_compatibility/gen, do not edit. -->
Note: This document covers API impact only. For more details, see the
[ABI compatibility page](/docs/development/languages/fidl/guides/compatibility/README.md)
# Change an enum from flexible to strict
## Overview
-|[init](#init)|[step 1](#step-1)|[step 2](#step-2)|[step 3](#step-3)
---|---|---|---|---
fidl|[link](#fidl-init)||[link](#fidl-2)|
dart|[link](#dart-init)|[link](#dart-1)||
go|[link](#go-init)|[link](#go-1)||
hlcpp|[link](#hlcpp-init)|[link](#hlcpp-1)||
llcpp|[link](#llcpp-init)|[link](#llcpp-1)||
rust|[link](#rust-init)|[link](#rust-1)||[link](#rust-3)
## Initial State {#init}
### FIDL {#fidl-init}
```fidl
type Color = flexible enum : int32 {
RED = 1;
BLUE = 2;
};
```
### Dart {#dart-init}
```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 {#go-init}
```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 {#hlcpp-init}
```cpp
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 {#llcpp-init}
```cpp
fidl_test::wire::Color complement(fidl_test::wire::Color color) {
if (color.IsUnknown()) {
return color;
}
switch (color) {
case fidl_test::wire::Color::kRed:
return fidl_test::wire::Color::kBlue;
case fidl_test::wire::Color::kBlue:
return fidl_test::wire::Color::kRed;
default:
return color;
}
}
```
### Rust {#rust-init}
```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 {#step-1}
### Dart {#dart-1}
```diff
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 {#go-1}
- Remove usages of any flexible specific APIs
```diff
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 {#hlcpp-1}
- Remove usages of any flexible specific APIs
```diff
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 {#llcpp-1}
- Remove usages of any flexible specific APIs
```diff
fidl_test::wire::Color complement(fidl_test::wire::Color color) {
- if (color.IsUnknown()) {
- return color;
- }
switch (color) {
case fidl_test::wire::Color::kRed:
return fidl_test::wire::Color::kBlue;
case fidl_test::wire::Color::kBlue:
return fidl_test::wire::Color::kRed;
default:
return color;
}
}
```
### 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.
```diff
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 {#step-2}
- Change from `flexible` to `strict`
```diff
- type Color = flexible enum : int32 {
+ type Color = strict enum : int32 {
RED = 1;
BLUE = 2;
};
```
## Update Source Code {#step-3}
### Rust {#rust-3}
- Remove the unreachable patterns attribute and underscore arm.
```diff
- 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,
}
}
```