From 78d85926d7eeec46ec9d46c3517ed0c2a989d454 Mon Sep 17 00:00:00 2001 From: Newton Ni Date: Sat, 12 Dec 2020 18:29:06 -0500 Subject: [PATCH 1/6] Flatten `Option>` The `#[serde(default)]` attribute will correctly handle missing fields and deserialize to an empty `Vec`, which is easier to work with. --- bril-rs/src/lib.rs | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/bril-rs/src/lib.rs b/bril-rs/src/lib.rs index 6feb75d1d..0e568fd7e 100644 --- a/bril-rs/src/lib.rs +++ b/bril-rs/src/lib.rs @@ -9,11 +9,12 @@ pub struct Program { #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Function { pub name: String, - #[serde(skip_serializing_if = "Option::is_none")] - pub args: Option>, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub args: Vec, #[serde(rename = "type")] #[serde(skip_serializing_if = "Option::is_none")] pub return_type: Option, + #[serde(default, skip_serializing_if = "Vec::is_empty")] pub instrs: Vec, } @@ -46,21 +47,21 @@ pub enum Instruction { dest: String, #[serde(rename = "type")] op_type: Type, - #[serde(skip_serializing_if = "Option::is_none")] - args: Option>, - #[serde(skip_serializing_if = "Option::is_none")] - funcs: Option>, - #[serde(skip_serializing_if = "Option::is_none")] - labels: Option>, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + args: Vec, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + funcs: Vec, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + labels: Vec, }, Effect { op: EffectOps, - #[serde(skip_serializing_if = "Option::is_none")] - args: Option>, - #[serde(skip_serializing_if = "Option::is_none")] - funcs: Option>, - #[serde(skip_serializing_if = "Option::is_none")] - labels: Option>, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + args: Vec, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + funcs: Vec, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + labels: Vec, }, } #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] From 466490b0672fddefb9036596573685323071dd86 Mon Sep 17 00:00:00 2001 From: Newton Ni Date: Sat, 12 Dec 2020 18:32:30 -0500 Subject: [PATCH 2/6] Use raw identifiers for type fields This is more of a stylistic choice: [raw identifiers][ri] are a bit ugly, but allow us to stick closer to the syntax reference. [ri]: https://doc.rust-lang.org/edition-guide/rust-2018/module-system/raw-identifiers.html --- bril-rs/src/lib.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/bril-rs/src/lib.rs b/bril-rs/src/lib.rs index 0e568fd7e..d6233bc65 100644 --- a/bril-rs/src/lib.rs +++ b/bril-rs/src/lib.rs @@ -11,9 +11,8 @@ pub struct Function { pub name: String, #[serde(default, skip_serializing_if = "Vec::is_empty")] pub args: Vec, - #[serde(rename = "type")] #[serde(skip_serializing_if = "Option::is_none")] - pub return_type: Option, + pub r#type: Option, #[serde(default, skip_serializing_if = "Vec::is_empty")] pub instrs: Vec, } @@ -21,8 +20,7 @@ pub struct Function { #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Argument { pub name: String, - #[serde(rename = "type")] - pub arg_type: Type, + pub r#type: Type, } #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] @@ -38,15 +36,13 @@ pub enum Instruction { Constant { op: ConstOps, dest: String, - #[serde(rename = "type")] - const_type: Type, + r#type: Type, value: Literal, }, Value { op: ValueOps, dest: String, - #[serde(rename = "type")] - op_type: Type, + r#type: Type, #[serde(default, skip_serializing_if = "Vec::is_empty")] args: Vec, #[serde(default, skip_serializing_if = "Vec::is_empty")] From e115c98c6d91f99cbdda315772d0fb1625ed7090 Mon Sep 17 00:00:00 2001 From: Newton Ni Date: Sat, 12 Dec 2020 18:36:05 -0500 Subject: [PATCH 3/6] Gate values and types behind `float` feature --- bril-rs/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bril-rs/src/lib.rs b/bril-rs/src/lib.rs index d6233bc65..449f7d07d 100644 --- a/bril-rs/src/lib.rs +++ b/bril-rs/src/lib.rs @@ -140,6 +140,7 @@ pub enum ValueOps { pub enum Type { Int, Bool, + #[cfg(feature = "float")] Float, #[cfg(feature = "memory")] #[serde(rename = "ptr")] @@ -151,6 +152,7 @@ pub enum Type { pub enum Literal { Int(i64), Bool(bool), + #[cfg(feature = "float")] Float(f64), } From 61108298925fa5777d0c5ed664c3275ed083045c Mon Sep 17 00:00:00 2001 From: Newton Ni Date: Sat, 12 Dec 2020 18:46:52 -0500 Subject: [PATCH 4/6] Derive some additional traits Deriving [`Copy`][cp] in particular makes life a lot easier. [cp]: https://doc.rust-lang.org/beta/core/marker/trait.Copy.html --- bril-rs/src/lib.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/bril-rs/src/lib.rs b/bril-rs/src/lib.rs index 449f7d07d..ed2d953de 100644 --- a/bril-rs/src/lib.rs +++ b/bril-rs/src/lib.rs @@ -60,13 +60,14 @@ pub enum Instruction { labels: Vec, }, } -#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] + +#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq, Hash)] pub enum ConstOps { #[serde(rename = "const")] Const, } -#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] +#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq, Hash)] #[serde(rename_all = "lowercase")] pub enum EffectOps { #[serde(rename = "jmp")] @@ -90,7 +91,7 @@ pub enum EffectOps { Guard, } -#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] +#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq, Hash)] #[serde(rename_all = "lowercase")] pub enum ValueOps { Add, @@ -135,7 +136,7 @@ pub enum ValueOps { PtrAdd, } -#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)] #[serde(rename_all = "lowercase")] pub enum Type { Int, @@ -147,7 +148,7 @@ pub enum Type { Pointer(Box), } -#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] +#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq)] #[serde(untagged)] pub enum Literal { Int(i64), From 31ff89536af03fcc1dc29fe4c45aa0daf9bae0fa Mon Sep 17 00:00:00 2001 From: Newton Ni Date: Sun, 13 Dec 2020 11:25:56 -0500 Subject: [PATCH 5/6] Revert "Use raw identifiers for type fields" This reverts commit 466490b0672fddefb9036596573685323071dd86. --- bril-rs/src/lib.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/bril-rs/src/lib.rs b/bril-rs/src/lib.rs index ed2d953de..4fa4e5df9 100644 --- a/bril-rs/src/lib.rs +++ b/bril-rs/src/lib.rs @@ -11,8 +11,9 @@ pub struct Function { pub name: String, #[serde(default, skip_serializing_if = "Vec::is_empty")] pub args: Vec, + #[serde(rename = "type")] #[serde(skip_serializing_if = "Option::is_none")] - pub r#type: Option, + pub return_type: Option, #[serde(default, skip_serializing_if = "Vec::is_empty")] pub instrs: Vec, } @@ -20,7 +21,8 @@ pub struct Function { #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Argument { pub name: String, - pub r#type: Type, + #[serde(rename = "type")] + pub arg_type: Type, } #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] @@ -36,13 +38,15 @@ pub enum Instruction { Constant { op: ConstOps, dest: String, - r#type: Type, + #[serde(rename = "type")] + const_type: Type, value: Literal, }, Value { op: ValueOps, dest: String, - r#type: Type, + #[serde(rename = "type")] + op_type: Type, #[serde(default, skip_serializing_if = "Vec::is_empty")] args: Vec, #[serde(default, skip_serializing_if = "Vec::is_empty")] From 8f165e3ded51a7791c34fc18cfdbf46e0a740f9a Mon Sep 17 00:00:00 2001 From: Newton Ni Date: Sun, 13 Dec 2020 11:29:56 -0500 Subject: [PATCH 6/6] Remove `Copy` trait --- bril-rs/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bril-rs/src/lib.rs b/bril-rs/src/lib.rs index 4fa4e5df9..5b8e9520e 100644 --- a/bril-rs/src/lib.rs +++ b/bril-rs/src/lib.rs @@ -152,7 +152,7 @@ pub enum Type { Pointer(Box), } -#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq)] +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] #[serde(untagged)] pub enum Literal { Int(i64),