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

Add an enum member

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 {
  2. RED = 1;
  3. BLUE = 2;
  4. };

Dart {#dart-init}

  1. fidllib.Color writer(String s) {
  2. if (s == 'red') {
  3. return fidllib.Color.red;
  4. } else if (s == 'blue') {
  5. return fidllib.Color.blue;
  6. } else {
  7. return fidllib.Color.$unknown;
  8. }
  9. }
  10. String reader(fidllib.Color color) {
  11. switch (color) {
  12. case fidllib.Color.blue:
  13. return 'blue';
  14. case fidllib.Color.red:
  15. return 'red';
  16. default:
  17. return '<unknown>';
  18. }
  19. }

Go {#go-init}

  1. func writer(s string) lib.Color {
  2. switch s {
  3. case "blue":
  4. return lib.ColorBlue
  5. case "red":
  6. return lib.ColorRed
  7. default:
  8. return lib.Color_Unknown
  9. }
  10. }
  11. func reader(color lib.Color) string {
  12. switch color {
  13. case lib.ColorBlue:
  14. return "blue"
  15. case lib.ColorRed:
  16. return "red"
  17. default:
  18. return "<unknown>"
  19. }
  20. }

HLCPP {#hlcpp-init}

  1. fidl_test::Color writer(std::string s) {
  2. if (s == "red") {
  3. return fidl_test::Color::RED;
  4. } else if (s == "blue") {
  5. return fidl_test::Color::BLUE;
  6. } else {
  7. return fidl_test::Color::Unknown();
  8. }
  9. }
  10. std::string reader(fidl_test::Color color) {
  11. switch (color) {
  12. case fidl_test::Color::RED:
  13. return "red";
  14. case fidl_test::Color::BLUE:
  15. return "blue";
  16. default:
  17. return "<unknown>";
  18. }
  19. }

LLCPP {#llcpp-init}

  1. fidl_test::Color writer(std::string s) {
  2. if (s == "red") {
  3. return fidl_test::Color::RED;
  4. } else if (s == "blue") {
  5. return fidl_test::Color::BLUE;
  6. } else {
  7. return fidl_test::Color::Unknown();
  8. }
  9. }
  10. std::string reader(fidl_test::Color color) {
  11. switch (color) {
  12. case fidl_test::Color::RED:
  13. return "red";
  14. case fidl_test::Color::BLUE:
  15. return "blue";
  16. default:
  17. return "<unknown>";
  18. }
  19. }

Rust {#rust-init}

  1. fn writer(s: &str) -> fidl_lib::Color {
  2. match s {
  3. "red" => fidl_lib::Color::Red,
  4. "blue" => fidl_lib::Color::Blue,
  5. _ => fidl_lib::Color::unknown(),
  6. }
  7. }
  8. fn reader(color: fidl_lib::Color) -> &'static str {
  9. match color {
  10. fidl_lib::Color::Red => "red",
  11. fidl_lib::Color::Blue => "blue",
  12. fidl_lib::ColorUnknown!() => "unknown",
  13. }
  14. }

Update Source Code {#step-1}

Rust {#rust-1}

  • Replace any unknown match arm macros with a catch-all case to handle the soon-to-be-added variant.
  1. fn writer(s: &str) -> fidl_lib::Color {
  2. match s {
  3. "red" => fidl_lib::Color::Red,
  4. "blue" => fidl_lib::Color::Blue,
  5. _ => fidl_lib::Color::unknown(),
  6. }
  7. }
  8. fn reader(color: fidl_lib::Color) -> &'static str {
  9. match color {
  10. fidl_lib::Color::Red => "red",
  11. fidl_lib::Color::Blue => "blue",
  12. - fidl_lib::ColorUnknown!() => "unknown",
  13. + _ => "unknown",
  14. }
  15. }

Update FIDL Library {#step-2}

  • Add the new member
  1. flexible enum Color {
  2. RED = 1;
  3. BLUE = 2;
  4. + YELLOW = 3;
  5. };

Update Source Code {#step-3}

Dart {#dart-3}

  • Writers can now produce instances of the new enum member.
  1. fidllib.Color writer(String s) {
  2. if (s == 'red') {
  3. return fidllib.Color.red;
  4. } else if (s == 'blue') {
  5. return fidllib.Color.blue;
  6. + } else if (s == 'yellow') {
  7. + return fidllib.Color.yellow;
  8. } else {
  9. return fidllib.Color.$unknown;
  10. }
  11. }
  12. String reader(fidllib.Color color) {
  13. switch (color) {
  14. case fidllib.Color.blue:
  15. return 'blue';
  16. case fidllib.Color.red:
  17. return 'red';
  18. + case fidllib.Color.yellow:
  19. + return 'yellow';
  20. default:
  21. return '<unknown>';
  22. }
  23. }

Go {#go-3}

  • Writers can now produce instances of the new enum member.
  1. func writer(s string) lib.Color {
  2. switch s {
  3. case "blue":
  4. return lib.ColorBlue
  5. case "red":
  6. return lib.ColorRed
  7. + case "yellow":
  8. + return lib.ColorYellow
  9. default:
  10. return lib.Color_Unknown
  11. }
  12. }
  13. func reader(color lib.Color) string {
  14. switch color {
  15. case lib.ColorBlue:
  16. return "blue"
  17. case lib.ColorRed:
  18. return "red"
  19. + case lib.ColorYellow:
  20. + return "yellow"
  21. default:
  22. return "<unknown>"
  23. }
  24. }

HLCPP {#hlcpp-3}

  • Writers can now produce instances of the new enum member.
  1. fidl_test::Color writer(std::string s) {
  2. if (s == "red") {
  3. return fidl_test::Color::RED;
  4. } else if (s == "blue") {
  5. return fidl_test::Color::BLUE;
  6. + } else if (s == "yellow") {
  7. + return fidl_test::Color::YELLOW;
  8. } else {
  9. return fidl_test::Color::Unknown();
  10. }
  11. }
  12. std::string reader(fidl_test::Color color) {
  13. switch (color) {
  14. case fidl_test::Color::RED:
  15. return "red";
  16. case fidl_test::Color::BLUE:
  17. return "blue";
  18. + case fidl_test::Color::YELLOW:
  19. + return "yellow";
  20. default:
  21. return "<unknown>";
  22. }
  23. }

LLCPP {#llcpp-3}

  • Writers can now produce instances of the new enum member.
  1. fidl_test::Color writer(std::string s) {
  2. if (s == "red") {
  3. return fidl_test::Color::RED;
  4. } else if (s == "blue") {
  5. return fidl_test::Color::BLUE;
  6. + } else if (s == "yellow") {
  7. + return fidl_test::Color::YELLOW;
  8. } else {
  9. return fidl_test::Color::Unknown();
  10. }
  11. }
  12. std::string reader(fidl_test::Color color) {
  13. switch (color) {
  14. case fidl_test::Color::RED:
  15. return "red";
  16. case fidl_test::Color::BLUE:
  17. return "blue";
  18. + case fidl_test::Color::YELLOW:
  19. + return "yellow";
  20. default:
  21. return "<unknown>";
  22. }
  23. }

Rust {#rust-3}

  • Writers can now produce instances of the new enum member.
  • Replace the catch-all case with the unknown macro.
  1. fn writer(s: &str) -> fidl_lib::Color {
  2. match s {
  3. "red" => fidl_lib::Color::Red,
  4. "blue" => fidl_lib::Color::Blue,
  5. + "yellow" => fidl_lib::Color::Yellow,
  6. _ => fidl_lib::Color::unknown(),
  7. }
  8. }
  9. fn reader(color: fidl_lib::Color) -> &'static str {
  10. match color {
  11. fidl_lib::Color::Red => "red",
  12. fidl_lib::Color::Blue => "blue",
  13. - _ => "unknown",
  14. + fidl_lib::Color::Yellow => "yellow",
  15. + fidl_lib::ColorUnknown!() => "unknown",
  16. }
  17. }