RFC-0022:說明:結構成員的預設值

RFC-0022:說明:結構成員的預設值
狀態已接受
區域
  • FIDL
說明

FIDL 結構體成員可能具有預設值。目前我們僅已部分實作預設支援功能 (請見下方章節),旨在說明預設設定的運作方式。

作者
提交日期 (年/月)2018-07-17
審查日期 (年/月)2018-09-27

摘要

FIDL 結構體成員可能具有預設值。目前我們僅已部分實作預設支援功能 (請見下文說明),本提案旨在說明預設設定的行為方式。

提振精神

  1. 這種做法可滿足各種語言繫結的規律性,還能避免使用不一致或非預期用途造成的危害。
  2. 避免在語言要求明確初始化時,省去費力的手動成員逐一初始化作業。

非動機包括:

  • 以傳輸格式儲存位元組

這麼做不是想節省傳輸位元組數,或是在編碼或解碼時節省處理能力的動機。

今日實作

結構成員可以使用 FIDL 語言表示預設值:

  • (+) 支援數字常值、布林常值和字串常值。
  • (-) 不會由 fidlc 對結構成員的常值可指派性提供類型檢查。字串常值「hello」可能會指派給 bool、指派給 uint 的負數,或是指派給 int16 的離界數。
  • (-) 語言繫結支援不一致,目前僅適用於 C++ 和 Dart 繫結。系統不支援 Go、C 或 Rust。

例如 (來自 //zircon/system/host/fidl/examples/types.test.fidl):

struct default_values {
    bool b1 = true;
    bool b2 = false;
    int8 i8 = -23;
    int16 i16 = 34;
    int32 i32 = -34595;
    int64 i64 = 3948038;
    uint8 u8 = 0;
    uint16 u16 = 348;
    uint32 u32 = 9038;
    uint64 u64 = 19835;
    float32 f32 = 1.30;
    float64 f64 = 0.0000054;
    string s = "hello";
};

設計

結構成員可以定義預設值。預設值會以類似 C 的 = {value} 模式顯示在欄位定義結尾。

語法

// cat.fidl

enum CatAction : int8 {
    SIT = -10;
    WALK = 0;
    SNEAK = 2;
};

struct Location {
    uint8 pos_x = 10;  // Position X
    uint8 pos_y;       // Position Y. Default unspecified. Fall-back to 0
    float32 pos_z = 3.14;  // Position Z.
    float32 pos_t;         // Default unspecified. Fall-back to 0.0
};

struct Cat {
    string name;      // Automatic default to empty string
    CatAction action = CatAction::SNEAK;
    Location loc;

};

語義

請參閱 RFC-006,其中清楚說明預設語意及繫結的要求。

支援的類型

  • 原始類型:
    • boolint8int16int32int64uint8uint16uint32uint64float32float64
  • 不可為空值 stringstring:N
    • string:N 會將保留且未使用的記憶體歸零。

不支援的類型

  • array<T>:N
    • 設為零
  • 不可為空值的類型:vector<T>vector<T>:N
    • 設為零
  • 可為空值類型:string?string:N?vector<T>?vector<T>:N?
    • 設為空值
  • handle
  • struct
    • 雖然 struct 中每個成員都有預設值,但 struct 本身沒有預設值。
  • union
    • 為避免衝突,系統將忽略 union 成員的任何預設值或 union 子結構 (任何深度) 的預設值。

預設特點

焦點位於值本身,而非指派值的「原理」。這至少意味著兩件事:

  • 這並沒有區別,如果因為興趣參數已由其他機制明確指派,而使用預設值,則使用預設值。
  • 在進行封送或解除封送時,不需要額外的 (透明) 邏輯層可以指派值。

實作

以下提供幾個實作 C、Rust 和 Go 繫結的實作範例

// in FIDL "default.fidl"
struct Location {
    uint8 pos_x = 10;
    uint8 pos_y = 20;
    uint8 pos_x;       // Should be set to "zero" according to above.
};
// C binding "defaults/fidl.h"
typedef struct _Location_raw {
   uint8_t pos_x;
   uint8_t pos_y;
   uint8_t pos_z
} Location;

Location Location_default = { 10, 20, 0 }; // Or in the source file.
                                           // May be used for memcmp,
memcpy, etc.

#define Location(my_instance) Location my_instance = Location_default;
// C code "example.c"
#include <fidl.h>
void showme(Location loc) {
    printf("(%u, %u, %u)\n", loc.pos_x, loc.pos_y, loc.pos_z);
}

int main() {
    Location(alpha);
    Location beta;
    Location gamma = Location_default;
    showme(alpha); showme(beta); showme(gamma);
    return 0;
}
// Rust binding
struct Location {
    pos_x: u8,
    pos_y: u8,
    pos_z: u8,
}
impl std::default::Default for Location {
    fn default() -> Self { Self { pos_x: 10, pos_y: 20, pos_z: 0 } }
}
// Go binding, using export control
type location struct {
    pos_x  uint8
    pos_y  uint8
    pos_z  uint8
}

Func NewLocation() location {
    loc := location{}
    loc.pos_x = 10
    loc.pos_y = 20
    // loc.pos_z = 0  Maybe omitted.
    return loc
}

效能

不適用

安全性

不適用

測試

不適用

回溯相容性

這項變更會使 FIDL 檔案來源回溯不相容。不需要變更 ABI 或線上格式。

缺點、替代方案和未知

如果在所有語言繫結中實作起來都相當簡單,系統不會評估該功能。

先前的圖片和參考資料

通訊協定緩衝區、扁平緩衝區提供預設值。Golang 有「零值」的概念,其中宣告的變數沒有明確初始值的變數會明確初始化為零。

開放原始碼方法:

// From https://github.com/creasty/defaults
type Sample struct {
        Name   string `default:"John Smith"`
        Age    int    `default:"27"`
        Gender Gender `default:"m"`

        Slice       []string       `default:"[]"`
        SliceByJSON []int          `default:"[1, 2, 3]"` // Supports JSON format
        Map         map[string]int `default:"{}"`
        MapByJSON   map[string]int `default:"{\"foo\": 123}"`

        Struct    OtherStruct  `default:"{}"`
        StructPtr *OtherStruct `default:"{\"Foo\": 123}"`

        NoTag  OtherStruct               // Recurses into a nested struct even without a tag
        OptOut OtherStruct `default:"-"` // Opt-out
}