You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@fury.apache.org by "jiacai2050 (via GitHub)" <gi...@apache.org> on 2024/08/26 14:13:09 UTC
[PR] feat: support enum serializaiton [fury]
jiacai2050 opened a new pull request, #1817:
URL: https://github.com/apache/fury/pull/1817
<!--
**Thanks for contributing to Fury.**
**If this is your first time opening a PR on fury, you can refer to [CONTRIBUTING.md](https://github.com/apache/fury/blob/main/CONTRIBUTING.md).**
Contribution Checklist
- The **Apache Fury (incubating)** community has restrictions on the naming of pr titles. You can also find instructions in [CONTRIBUTING.md](https://github.com/apache/fury/blob/main/CONTRIBUTING.md).
- Fury has a strong focus on performance. If the PR you submit will have an impact on performance, please benchmark it first and provide the benchmark result here.
-->
## What does this PR do?
Support enum serializaiton for Rust.
## Related issues
Close #1393
## Does this PR introduce any user-facing change?
<!--
If any user-facing interface changes, please [open an issue](https://github.com/apache/fury/issues/new/choose) describing the need to do so and update the document if necessary.
-->
- [x] Does this PR introduce any public API change?
- [ ] Does this PR introduce any binary protocol compatibility change?
## Benchmark
<!--
When the PR has an impact on performance (if you don't know whether the PR will have an impact on performance, you can submit the PR first, and if it will have impact on performance, the code reviewer will explain it), be sure to attach a benchmark data here.
-->
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For additional commands, e-mail: commits-help@fury.apache.org
Re: [PR] feat(rust): support enum serialization [fury]
Posted by "chaokunyang (via GitHub)" <gi...@apache.org>.
chaokunyang commented on code in PR #1817:
URL: https://github.com/apache/fury/pull/1817#discussion_r1732270053
##########
rust/fury-derive/src/object/derive_enum.rs:
##########
@@ -0,0 +1,68 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use proc_macro2::TokenStream;
+use quote::quote;
+use syn::DataEnum;
+
+pub fn gen_type_def(_data_enum: &DataEnum) -> TokenStream {
+ quote! {
+ fn type_def(fury: &fury_core::fury::Fury) -> Vec<u8> {
+ Vec::new()
+ }
+ }
+}
+
+pub fn gen_write(data_enum: &DataEnum) -> TokenStream {
+ let variant_idents: Vec<_> = data_enum.variants.iter().map(|v| &v.ident).collect();
+ let variant_values: Vec<_> = (0..variant_idents.len()).map(|v| v as i32).collect();
+
+ quote! {
+ fn write(&self, context: &mut fury_core::resolver::context::WriteContext) {
+ match self {
+ #(
+ Self::#variant_idents => {
+ context.writer.var_int32(#variant_values);
+ }
+ )*
+ }
+ }
+
+ fn reserved_space() -> usize {
+ 4
+ }
+ }
+}
+
+pub fn gen_read(data_enum: &DataEnum) -> TokenStream {
+ let variant_idents: Vec<_> = data_enum.variants.iter().map(|v| &v.ident).collect();
+ let variant_values: Vec<_> = (0..variant_idents.len()).map(|v| v as i32).collect();
+
+ quote! {
+ fn read(
+ context: &mut fury_core::resolver::context::ReadContext,
+ ) -> Result<Self, fury_core::error::Error> {
+ let v = context.reader.var_int32();
+ match v {
+ #(
+ #variant_values => Ok(Self::#variant_idents),
+ )*
+ _ => panic!("unknown value"),
Review Comment:
Not related to this PR, but maybe it's time to think about how we better handle exceptional data. If we don't handle such dirty data, it's easy to construct data to attack the service
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For additional commands, e-mail: commits-help@fury.apache.org
Re: [PR] feat(rust): support enum serialization [fury]
Posted by "theweipeng (via GitHub)" <gi...@apache.org>.
theweipeng commented on code in PR #1817:
URL: https://github.com/apache/fury/pull/1817#discussion_r1734346570
##########
rust/fury-derive/src/object/derive_enum.rs:
##########
@@ -0,0 +1,68 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use proc_macro2::TokenStream;
+use quote::quote;
+use syn::DataEnum;
+
+pub fn gen_type_def(_data_enum: &DataEnum) -> TokenStream {
+ quote! {
+ fn type_def(fury: &fury_core::fury::Fury) -> Vec<u8> {
+ Vec::new()
Review Comment:
We can levae it empty since enum types do not require compatibility support
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For additional commands, e-mail: commits-help@fury.apache.org
Re: [PR] feat(rust): support enum serialization [fury]
Posted by "jiacai2050 (via GitHub)" <gi...@apache.org>.
jiacai2050 commented on code in PR #1817:
URL: https://github.com/apache/fury/pull/1817#discussion_r1732020576
##########
rust/fury-derive/src/object/derive_enum.rs:
##########
@@ -0,0 +1,68 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use proc_macro2::TokenStream;
+use quote::quote;
+use syn::DataEnum;
+
+pub fn gen_type_def(_data_enum: &DataEnum) -> TokenStream {
+ quote! {
+ fn type_def(fury: &fury_core::fury::Fury) -> Vec<u8> {
+ Vec::new()
Review Comment:
Since there is no fields in enum, so we can leave it empty?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For additional commands, e-mail: commits-help@fury.apache.org
Re: [PR] feat(rust): support enum serialization [fury]
Posted by "theweipeng (via GitHub)" <gi...@apache.org>.
theweipeng commented on code in PR #1817:
URL: https://github.com/apache/fury/pull/1817#discussion_r1734344684
##########
rust/fury-derive/src/object/derive_enum.rs:
##########
@@ -0,0 +1,68 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use proc_macro2::TokenStream;
+use quote::quote;
+use syn::DataEnum;
+
+pub fn gen_type_def(_data_enum: &DataEnum) -> TokenStream {
+ quote! {
+ fn type_def(fury: &fury_core::fury::Fury) -> Vec<u8> {
+ Vec::new()
+ }
+ }
+}
+
+pub fn gen_write(data_enum: &DataEnum) -> TokenStream {
+ let variant_idents: Vec<_> = data_enum.variants.iter().map(|v| &v.ident).collect();
+ let variant_values: Vec<_> = (0..variant_idents.len()).map(|v| v as i32).collect();
+
+ quote! {
+ fn write(&self, context: &mut fury_core::resolver::context::WriteContext) {
+ match self {
+ #(
+ Self::#variant_idents => {
+ context.writer.var_int32(#variant_values);
+ }
+ )*
+ }
+ }
+
+ fn reserved_space() -> usize {
+ 4
Review Comment:
Yes, it is right
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For additional commands, e-mail: commits-help@fury.apache.org
Re: [PR] feat(rust): support enum serialization [fury]
Posted by "theweipeng (via GitHub)" <gi...@apache.org>.
theweipeng merged PR #1817:
URL: https://github.com/apache/fury/pull/1817
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For additional commands, e-mail: commits-help@fury.apache.org
Re: [PR] feat: support enum serializaiton [fury]
Posted by "jiacai2050 (via GitHub)" <gi...@apache.org>.
jiacai2050 commented on code in PR #1817:
URL: https://github.com/apache/fury/pull/1817#discussion_r1731323541
##########
rust/fury-derive/src/object/derive_enum.rs:
##########
@@ -0,0 +1,68 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use proc_macro2::TokenStream;
+use quote::quote;
+use syn::DataEnum;
+
+pub fn gen_type_def(_data_enum: &DataEnum) -> TokenStream {
+ quote! {
+ fn type_def(fury: &fury_core::fury::Fury) -> Vec<u8> {
+ Vec::new()
+ }
+ }
+}
+
+pub fn gen_write(data_enum: &DataEnum) -> TokenStream {
+ let variant_idents: Vec<_> = data_enum.variants.iter().map(|v| &v.ident).collect();
+ let variant_values: Vec<_> = (0..variant_idents.len()).map(|v| v as i32).collect();
+
+ quote! {
+ fn write(&self, context: &mut fury_core::resolver::context::WriteContext) {
+ match self {
+ #(
+ Self::#variant_idents => {
+ context.writer.var_int32(#variant_values);
+ }
+ )*
+ }
+ }
+
+ fn reserved_space() -> usize {
+ 4
Review Comment:
I'm not sure if this is right.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For additional commands, e-mail: commits-help@fury.apache.org
Re: [PR] feat(rust): support enum serialization [fury]
Posted by "jiacai2050 (via GitHub)" <gi...@apache.org>.
jiacai2050 commented on code in PR #1817:
URL: https://github.com/apache/fury/pull/1817#discussion_r1732455207
##########
rust/fury-derive/src/object/derive_enum.rs:
##########
@@ -0,0 +1,68 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use proc_macro2::TokenStream;
+use quote::quote;
+use syn::DataEnum;
+
+pub fn gen_type_def(_data_enum: &DataEnum) -> TokenStream {
+ quote! {
+ fn type_def(fury: &fury_core::fury::Fury) -> Vec<u8> {
+ Vec::new()
+ }
+ }
+}
+
+pub fn gen_write(data_enum: &DataEnum) -> TokenStream {
+ let variant_idents: Vec<_> = data_enum.variants.iter().map(|v| &v.ident).collect();
+ let variant_values: Vec<_> = (0..variant_idents.len()).map(|v| v as i32).collect();
+
+ quote! {
+ fn write(&self, context: &mut fury_core::resolver::context::WriteContext) {
+ match self {
+ #(
+ Self::#variant_idents => {
+ context.writer.var_int32(#variant_values);
+ }
+ )*
+ }
+ }
+
+ fn reserved_space() -> usize {
+ 4
+ }
+ }
+}
+
+pub fn gen_read(data_enum: &DataEnum) -> TokenStream {
+ let variant_idents: Vec<_> = data_enum.variants.iter().map(|v| &v.ident).collect();
+ let variant_values: Vec<_> = (0..variant_idents.len()).map(|v| v as i32).collect();
+
+ quote! {
+ fn read(
+ context: &mut fury_core::resolver::context::ReadContext,
+ ) -> Result<Self, fury_core::error::Error> {
+ let v = context.reader.var_int32();
+ match v {
+ #(
+ #variant_values => Ok(Self::#variant_idents),
+ )*
+ _ => panic!("unknown value"),
Review Comment:
Agree, the Rust codebase contains too many `unwrap`, we need to refactor it to return `Result` before implement more features.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For additional commands, e-mail: commits-help@fury.apache.org
Re: [PR] feat(rust): support enum serialization [fury]
Posted by "jiacai2050 (via GitHub)" <gi...@apache.org>.
jiacai2050 commented on code in PR #1817:
URL: https://github.com/apache/fury/pull/1817#discussion_r1732455207
##########
rust/fury-derive/src/object/derive_enum.rs:
##########
@@ -0,0 +1,68 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use proc_macro2::TokenStream;
+use quote::quote;
+use syn::DataEnum;
+
+pub fn gen_type_def(_data_enum: &DataEnum) -> TokenStream {
+ quote! {
+ fn type_def(fury: &fury_core::fury::Fury) -> Vec<u8> {
+ Vec::new()
+ }
+ }
+}
+
+pub fn gen_write(data_enum: &DataEnum) -> TokenStream {
+ let variant_idents: Vec<_> = data_enum.variants.iter().map(|v| &v.ident).collect();
+ let variant_values: Vec<_> = (0..variant_idents.len()).map(|v| v as i32).collect();
+
+ quote! {
+ fn write(&self, context: &mut fury_core::resolver::context::WriteContext) {
+ match self {
+ #(
+ Self::#variant_idents => {
+ context.writer.var_int32(#variant_values);
+ }
+ )*
+ }
+ }
+
+ fn reserved_space() -> usize {
+ 4
+ }
+ }
+}
+
+pub fn gen_read(data_enum: &DataEnum) -> TokenStream {
+ let variant_idents: Vec<_> = data_enum.variants.iter().map(|v| &v.ident).collect();
+ let variant_values: Vec<_> = (0..variant_idents.len()).map(|v| v as i32).collect();
+
+ quote! {
+ fn read(
+ context: &mut fury_core::resolver::context::ReadContext,
+ ) -> Result<Self, fury_core::error::Error> {
+ let v = context.reader.var_int32();
+ match v {
+ #(
+ #variant_values => Ok(Self::#variant_idents),
+ )*
+ _ => panic!("unknown value"),
Review Comment:
Agree, the Rust codebase contains too many `unwrap`, we need to refactor it to return `Result` before more feature request.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For additional commands, e-mail: commits-help@fury.apache.org