-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0e18130
commit c14ec8f
Showing
26 changed files
with
427 additions
and
160 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// 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 super::context::{ReadContext, WriteContext}; | ||
use crate::error::Error; | ||
use crate::fury::Fury; | ||
use crate::serializer::StructSerializer; | ||
use std::any::TypeId; | ||
use std::{any::Any, collections::HashMap}; | ||
|
||
pub struct Harness { | ||
serializer: fn(&dyn Any, &mut WriteContext), | ||
deserializer: fn(&mut ReadContext) -> Result<Box<dyn Any>, Error>, | ||
} | ||
|
||
impl Harness { | ||
pub fn new( | ||
serializer: fn(&dyn Any, &mut WriteContext), | ||
deserializer: fn(&mut ReadContext) -> Result<Box<dyn Any>, Error>, | ||
) -> Harness { | ||
Harness { | ||
serializer, | ||
deserializer, | ||
} | ||
} | ||
|
||
pub fn get_serializer(&self) -> fn(&dyn Any, &mut WriteContext) { | ||
self.serializer | ||
} | ||
|
||
pub fn get_deserializer(&self) -> fn(&mut ReadContext) -> Result<Box<dyn Any>, Error> { | ||
self.deserializer | ||
} | ||
} | ||
|
||
pub struct ClassInfo { | ||
type_def: Vec<u8>, | ||
type_id: u32, | ||
} | ||
|
||
impl ClassInfo { | ||
pub fn new<T: StructSerializer>(fury: &Fury, type_id: u32) -> ClassInfo { | ||
ClassInfo { | ||
type_def: T::type_def(fury), | ||
type_id, | ||
} | ||
} | ||
|
||
pub fn get_type_id(&self) -> u32 { | ||
self.type_id | ||
} | ||
|
||
pub fn get_type_def(&self) -> &Vec<u8> { | ||
&self.type_def | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct ClassResolver { | ||
serialize_map: HashMap<u32, Harness>, | ||
type_id_map: HashMap<TypeId, u32>, | ||
class_info_map: HashMap<TypeId, ClassInfo>, | ||
} | ||
|
||
impl ClassResolver { | ||
pub fn get_class_info(&self, type_id: TypeId) -> &ClassInfo { | ||
self.class_info_map.get(&type_id).unwrap() | ||
} | ||
|
||
pub fn register<T: StructSerializer>(&mut self, class_info: ClassInfo, id: u32) { | ||
fn serializer<T2: 'static + StructSerializer>(this: &dyn Any, context: &mut WriteContext) { | ||
let this = this.downcast_ref::<T2>(); | ||
match this { | ||
Some(v) => { | ||
T2::serialize(v, context); | ||
} | ||
None => todo!(), | ||
} | ||
} | ||
|
||
fn deserializer<T2: 'static + StructSerializer>( | ||
context: &mut ReadContext, | ||
) -> Result<Box<dyn Any>, Error> { | ||
match T2::deserialize(context) { | ||
Ok(v) => Ok(Box::new(v)), | ||
Err(e) => Err(e), | ||
} | ||
} | ||
self.type_id_map.insert(TypeId::of::<T>(), id); | ||
self.serialize_map | ||
.insert(id, Harness::new(serializer::<T>, deserializer::<T>)); | ||
self.class_info_map.insert(TypeId::of::<T>(), class_info); | ||
} | ||
|
||
pub fn get_harness_by_type(&self, type_id: TypeId) -> Option<&Harness> { | ||
self.get_harness(*self.type_id_map.get(&type_id).unwrap()) | ||
} | ||
|
||
pub fn get_harness(&self, id: u32) -> Option<&Harness> { | ||
self.serialize_map.get(&id) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// 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 crate::error::Error; | ||
use crate::fury::Fury; | ||
use crate::resolver::context::{ReadContext, WriteContext}; | ||
use crate::serializer::Serializer; | ||
use crate::types::{FieldType, Mode, RefFlag}; | ||
use std::any::Any; | ||
|
||
impl Serializer for Box<dyn Any> { | ||
fn reserved_space() -> usize { | ||
0 | ||
} | ||
|
||
fn write(&self, _context: &mut WriteContext) { | ||
panic!("unreachable") | ||
} | ||
|
||
fn read(_context: &mut ReadContext) -> Result<Self, Error> { | ||
panic!("unreachable") | ||
} | ||
|
||
fn ty(_fury: &Fury) -> i16 { | ||
FieldType::FuryTypeTag.into() | ||
} | ||
|
||
fn serialize(&self, context: &mut WriteContext) { | ||
context | ||
.get_fury() | ||
.get_class_resolver() | ||
.get_harness_by_type(self.as_ref().type_id()) | ||
.unwrap() | ||
.get_serializer()(self.as_ref(), context); | ||
} | ||
|
||
fn deserialize(context: &mut ReadContext) -> Result<Self, Error> { | ||
let reset_cursor = context.reader.reset_cursor_to_here(); | ||
// ref flag | ||
let ref_flag = context.reader.i8(); | ||
|
||
if ref_flag == (RefFlag::NotNullValue as i8) || ref_flag == (RefFlag::RefValue as i8) { | ||
if context.get_fury().get_mode().eq(&Mode::Compatible) { | ||
let meta_index = context.reader.i16(); | ||
let type_id = context.meta_resolver.get(meta_index as usize).get_type_id(); | ||
reset_cursor(&mut context.reader); | ||
context | ||
.get_fury() | ||
.get_class_resolver() | ||
.get_harness(type_id) | ||
.unwrap() | ||
.get_deserializer()(context) | ||
} else { | ||
let type_id = context.reader.i16(); | ||
reset_cursor(&mut context.reader); | ||
context | ||
.get_fury() | ||
.get_class_resolver() | ||
.get_harness(type_id as u32) | ||
.unwrap() | ||
.get_deserializer()(context) | ||
} | ||
} else if ref_flag == (RefFlag::Null as i8) { | ||
Err(Error::Null) | ||
} else if ref_flag == (RefFlag::Ref as i8) { | ||
reset_cursor(&mut context.reader); | ||
Err(Error::Ref) | ||
} else { | ||
reset_cursor(&mut context.reader); | ||
Err(Error::BadRefFlag) | ||
} | ||
} | ||
} |
Oops, something went wrong.