[TEST] PSA: FIDL now supports named method payloads

25 views
Skip to first unread message

Alex Zaslavsky

unread,
Mar 9, 2022, 7:36:00 PM3/9/22
to fidl...@fuchsia.dev, anno...@fuchsia.dev

If you don't read/write FIDL, or interact with generated FIDL bindings, you may stop reading now.

TL;DR:

  • Named method payloads are now supported in FIDL:

// Old, repetitive way

protocol OldProtocol {

    InvertNumber(struct {num int16;}) -> (struct {num int16;});

    HalveNumber(struct {num int16;}) -> (struct {num int16;});

};


// New, DRY way

type NumberPayload = struct {

    num int16;

};

protocol NewProtocol {

    InvertNumber(NumberPayload) -> (NumberPayload);

    HalveNumber(NumberPayload) -> (NumberPayload);

};


  • This style is encouraged for new method definitions where appropriate, but already existing definitions will not be proactively converted to support this new syntax.

Example

Named payload conversions are 100% API preserving for struct payloads, which as of this writing are the only payloads in fuchsia.git, though support for table and union payloads is just around the corner.   The OldProtocol/NewProtocol example above will result in the following, functionally identical HLCPP output:

class OldProtocol {

  public:

    /* … */

    using InvertNumberCallback = fit::function<void(int16_t)>;

    virtual void InvertNumber(int16_t num, InvertNumberCallback callback) = 0;

    using HalveNumberCallback = fit::function<void(int16_t)>;

    virtual void HalveNumber(int16_t num, HalveNumberCallback callback) = 0;

};


class NewProtocol {

  public:

    /* … */

    using InvertNumberCallback = fit::function<void(int16_t)>;

    virtual void InvertNumber(int16_t num, InvertNumberCallback callback) = 0;

    using HalveNumberCallback = fit::function<void(int16_t)>;

    virtual void HalveNumber(int16_t num, HalveNumberCallback callback) = 0;

};

And this (also identical) Rust output:

pub trait OldProtocolProxyInterface: Send + Sync {

   type InvertNumberResponseFut: std::future::Future<Output = Result<(i16), fidl::Error>> + Send;

   fn r#invert_number(&self, num: i16) -> Self::InvertNumberResponseFut;

   type HalveNumberResponseFut: std::future::Future<Output = Result<(i16), fidl::Error>> + Send;

   fn r#halve_number(&self, num: i16) -> Self::HalveNumberResponseFut;

}


pub trait NewProtocolProxyInterface: Send + Sync {

   type InvertNumberResponseFut: std::future::Future<Output = Result<(i16), fidl::Error>> + Send;

   fn r#invert_number(&self, num: i16) -> Self::InvertNumberResponseFut;

   type HalveNumberResponseFut: std::future::Future<Output = Result<(i16), fidl::Error>> + Send;

   fn r#halve_number(&self, num: i16) -> Self::HalveNumberResponseFut;

}


Reply all
Reply to author
Forward
0 new messages