-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rust): support enum serialization (#1817)
## What does this PR do? Support enum serialization 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. --> - [ ] 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. -->
- Loading branch information
1 parent
3cef53c
commit 19d62b3
Showing
6 changed files
with
107 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"), | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters