tree: 963dfb6ab57dea2b70e17cd54ea4d289fdc6e350 [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/protocol-method-add/README.md

Add a protocol method

Overview

-initstep 1step 2step 3step 4step 5
fidllinklinklink
dartlinklink
golinklinklink
hlcpplinklink
llcpplinklink
rustlinklinklinklink

Initial State

FIDL

protocol Example {
    ExistingMethod();
};

Dart

class Server extends fidllib.Example {
  @override
  Future<void> existingMethod() async {}
}

void client(fidllib.ExampleProxy client) async {
  await client.existingMethod();
}

Go

type client struct {
	addMethod *lib.ExampleWithCtxInterface
}

func (c client) test() {
	c.addMethod.ExistingMethod(context.Background())
}

type server struct{}

// Assert that server implements the Example interface
var _ lib.ExampleWithCtx = &server{}

func (*server) ExistingMethod(fidl.Context) error {
	return nil
}

HLCPP

class Server : public fidl_test::Example {
  void ExistingMethod() final {}
};

void client(fidl_test::ExamplePtr client) { client->ExistingMethod(); }

LLCPP

class Server final : public fidl_test::Example::Interface {
 public:
  void ExistingMethod(ExistingMethodCompleter::Sync& completer) final {}
};

void client(fidl::Client<fidl_test::Example> client) { client->ExistingMethod(); }

Rust

struct ExampleFakeProxy;

impl fidl_lib::ExampleProxyInterface for ExampleFakeProxy {
    fn existing_method(&self) -> Result<(), fidl::Error> {
        Ok(())
    }
}

async fn example_service(chan: fasync::Channel) -> Result<(), fidl::Error> {
    let mut stream = fidl_lib::ExampleRequestStream::from_channel(chan);
    while let Some(req) = stream.try_next().await? {
        match req {
            fidl_lib::ExampleRequest::ExistingMethod { .. } => {}
        }
    }
    Ok(())
}

Update Source Code

Go

  • Embed the protocol's WithCtxTransitionBase struct into the server type.
  	c.addMethod.ExistingMethod(context.Background())
  }
  
- type server struct{}
+ type server struct {
+ 	lib.ExampleWithCtxTransitionalBase
+ }
  
  // Assert that server implements the Example interface
  var _ lib.ExampleWithCtx = &server{}

Rust

  • Add #[allow(unreachable_patterns)] to the server's request stream match.
  • Add an underscore arm to the server's request stream match.
  async fn example_service(chan: fasync::Channel) -> Result<(), fidl::Error> {
      let mut stream = fidl_lib::ExampleRequestStream::from_channel(chan);
      while let Some(req) = stream.try_next().await? {
+         #[allow(unreachable_patterns)]
          match req {
              fidl_lib::ExampleRequest::ExistingMethod { .. } => {}
+             _ => {}
          }
      }
      Ok(())

Update FIDL Library

  • Add the new method and mark it with the [Transitional] attribute.
  protocol Example {
      ExistingMethod();
+     [Transitional]
+     NewMethod();
  };

Update Source Code

Dart

  • Add the new method to protocol implementations.
  • Start using the new method in client code.
  class Server extends fidllib.Example {
    @override
    Future<void> existingMethod() async {}
+ 
+   @override
+   Future<void> newMethod() async {}
  }
  
  void client(fidllib.ExampleProxy client) async {
    await client.existingMethod();
+   await client.newMethod();
  }

Go

  • Implement the new method for the server type.
  • Remove the protocol's WithCtxTransitionBase struct from the server type.
  • Start using the new method in client code.
  
  func (c client) test() {
  	c.addMethod.ExistingMethod(context.Background())
+ 	c.addMethod.NewMethod(context.Background())
  }
  
- type server struct {
- 	lib.ExampleWithCtxTransitionalBase
- }
+ type server struct{}
  
  // Assert that server implements the Example interface
  var _ lib.ExampleWithCtx = &server{}

  	return nil
  }
  
+ func (*server) NewMethod(fidl.Context) error {
+ 	return nil
+ }
+ 

HLCPP

  • Add the new method to protocol implementations.
  • Start using the new method in client code.
  class Server : public fidl_test::Example {
    void ExistingMethod() final {}
+   void NewMethod() final {}
  };
  
- void client(fidl_test::ExamplePtr client) { client->ExistingMethod(); }
+ void client(fidl_test::ExamplePtr client) {
+   client->ExistingMethod();
+   client->NewMethod();
+ }

LLCPP

  • Add the new method to protocol implementations.
  • Start using the new method in client code.
  class Server final : public fidl_test::Example::Interface {
   public:
    void ExistingMethod(ExistingMethodCompleter::Sync& completer) final {}
+   void NewMethod(NewMethodCompleter::Sync& completer) final {}
  };
  
- void client(fidl::Client<fidl_test::Example> client) { client->ExistingMethod(); }
+ void client(fidl::Client<fidl_test::Example> client) {
+   client->ExistingMethod();
+   client->NewMethod();
+ }

Rust

  • Add the new method to the protocol's ProxyInterface implementation.
  
  impl fidl_lib::ExampleProxyInterface for ExampleFakeProxy {
      fn existing_method(&self) -> Result<(), fidl::Error> {
+         Ok(())
+     }
+     fn new_method(&self) -> Result<(), fidl::Error> {
          Ok(())
      }
  }

Update FIDL Library

  • Remove the [Transitional] attribute from the new method.
  protocol Example {
      ExistingMethod();
-     [Transitional]
      NewMethod();
  };

Update Source Code

Rust

  • Remove #[allow(unreachable_patterns)] from the server's request stream match.
  • Replace the underscore arm in the server's request stream match with one that handles the new method.
  async fn example_service(chan: fasync::Channel) -> Result<(), fidl::Error> {
      let mut stream = fidl_lib::ExampleRequestStream::from_channel(chan);
      while let Some(req) = stream.try_next().await? {
-         #[allow(unreachable_patterns)]
          match req {
              fidl_lib::ExampleRequest::ExistingMethod { .. } => {}
-             _ => {}
+             fidl_lib::ExampleRequest::NewMethod { .. } => {}
          }
      }
      Ok(())