RFC-0022:说明:结构体成员的默认值 | |
---|---|
状态 | 已接受 |
领域 |
|
说明 | FIDL 结构成员可以有默认值。目前,对默认值的支持已部分实现(请参见下一部分),此方案旨在阐明默认值的行为方式。 |
作者 | |
提交日期(年-月-日) | 2018-07-17 |
审核日期(年-月-日) | 2018-09-27 |
总结
FIDL 结构成员可以有默认值。目前,对默认值的支持已部分实现(请参阅下面的部分),此方案旨在阐明默认值的行为方式。
设计初衷
- 它在语言绑定中实现规则性,防止出现不一致或意外的用途,并且
- 当语言要求进行显式初始化时,消除逐个成员的手动初始化工作
无动机包括:
- 以传输格式保存字节
我们不是为了以有线格式节省字节或在编码或解码时节省处理能力的动机。
今日实现
默认值可以针对结构体成员以 FIDL 语言表示:
- (+) 支持数字字面量、布尔值字面量和字符串字面量。
- (-)
fidlc
不提供关于是否可将字面量分配给结构体成员的类型检查。 可以为布尔值分配字符串字面量“hello”,为 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,其中阐明了默认值的语义以及绑定要求。
支持的类型
- 基元类型:
bool
、int8
、int16
、int32
、int64
、uint8
、uint16
、uint32
、uint64
、float32
、float64
- 不可为 null 的
string
、string:N
string:N
应将预留且未使用的内存清零。
不支持的类型
array<T>:N
- 设置为零
- 不可为 null 的类型:
vector<T>
、vector<T>:N
- 设置为零
- 可为 null 的类型:
string?
、string:N?
、vector<T>?
、vector<T>:N?
- 设为 null
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
}