Skip to main content

fory_core/serializer/
set.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use crate::error::Error;
19use crate::resolver::context::ReadContext;
20use crate::resolver::context::WriteContext;
21use crate::resolver::type_resolver::TypeResolver;
22use crate::serializer::collection::{
23    read_collection_data, read_collection_type_info, write_collection_data,
24    write_collection_type_info,
25};
26
27use crate::serializer::{ForyDefault, Serializer};
28use crate::types::TypeId;
29use std::collections::{BTreeSet, HashSet};
30use std::mem;
31
32impl<T: Serializer + ForyDefault + Eq + std::hash::Hash> Serializer for HashSet<T> {
33    fn fory_write_data(&self, context: &mut WriteContext) -> Result<(), Error> {
34        write_collection_data(self, context, false)
35    }
36
37    fn fory_write_data_generic(
38        &self,
39        context: &mut WriteContext,
40        has_generics: bool,
41    ) -> Result<(), Error> {
42        write_collection_data(self, context, has_generics)
43    }
44
45    fn fory_write_type_info(context: &mut WriteContext) -> Result<(), Error> {
46        write_collection_type_info(context, TypeId::SET as u32)
47    }
48
49    fn fory_read_data(context: &mut ReadContext) -> Result<Self, Error> {
50        read_collection_data(context)
51    }
52
53    fn fory_read_type_info(context: &mut ReadContext) -> Result<(), Error> {
54        read_collection_type_info(context, TypeId::SET as u32)
55    }
56
57    fn fory_reserved_space() -> usize {
58        mem::size_of::<i32>()
59    }
60
61    fn fory_get_type_id(_: &TypeResolver) -> Result<TypeId, Error> {
62        Ok(TypeId::SET)
63    }
64
65    fn fory_type_id_dyn(&self, _: &TypeResolver) -> Result<TypeId, Error> {
66        Ok(TypeId::SET)
67    }
68
69    fn fory_static_type_id() -> TypeId
70    where
71        Self: Sized,
72    {
73        TypeId::SET
74    }
75
76    fn as_any(&self) -> &dyn std::any::Any {
77        self
78    }
79}
80
81impl<T> ForyDefault for HashSet<T> {
82    fn fory_default() -> Self {
83        HashSet::new()
84    }
85}
86
87impl<T: Serializer + ForyDefault + Ord> Serializer for BTreeSet<T> {
88    fn fory_write_data(&self, context: &mut WriteContext) -> Result<(), Error> {
89        write_collection_data(self, context, false)
90    }
91
92    fn fory_write_data_generic(
93        &self,
94        context: &mut WriteContext,
95        has_generics: bool,
96    ) -> Result<(), Error> {
97        write_collection_data(self, context, has_generics)
98    }
99
100    fn fory_write_type_info(context: &mut WriteContext) -> Result<(), Error> {
101        write_collection_type_info(context, TypeId::SET as u32)
102    }
103
104    fn fory_read_data(context: &mut ReadContext) -> Result<Self, Error> {
105        read_collection_data(context)
106    }
107
108    fn fory_read_type_info(context: &mut ReadContext) -> Result<(), Error> {
109        read_collection_type_info(context, TypeId::SET as u32)
110    }
111
112    fn fory_reserved_space() -> usize {
113        mem::size_of::<i32>()
114    }
115
116    fn fory_get_type_id(_: &TypeResolver) -> Result<TypeId, Error> {
117        Ok(TypeId::SET)
118    }
119
120    fn fory_type_id_dyn(&self, _: &TypeResolver) -> Result<TypeId, Error> {
121        Ok(TypeId::SET)
122    }
123
124    fn fory_static_type_id() -> TypeId
125    where
126        Self: Sized,
127    {
128        TypeId::SET
129    }
130
131    fn as_any(&self) -> &dyn std::any::Any {
132        self
133    }
134}
135
136impl<T> ForyDefault for BTreeSet<T> {
137    fn fory_default() -> Self {
138        BTreeSet::new()
139    }
140}