use crate::ensure;
use crate::error::Error;
use crate::resolver::context::{ReadContext, WriteContext};
use crate::serializer::Serializer;
use crate::types::TypeId;
use crate::types::{is_user_type, ENUM, NAMED_ENUM, UNION};
#[inline(always)]
pub(crate) fn read_basic_type_info<T: Serializer>(context: &mut ReadContext) -> Result<(), Error> {
let local_type_id = T::fory_get_type_id(context.get_type_resolver())?;
let local_type_id_u32 = local_type_id as u32;
let remote_type_id = context.reader.read_u8()? as u32;
ensure!(
local_type_id_u32 == remote_type_id,
Error::type_mismatch(local_type_id_u32, remote_type_id)
);
Ok(())
}
#[inline]
pub const fn field_need_read_type_info(type_id: u32) -> bool {
if type_id == ENUM || type_id == NAMED_ENUM || type_id == UNION {
return false;
}
is_user_type(type_id)
}
pub const fn field_need_write_type_info(static_type_id: TypeId) -> bool {
let static_type_id = static_type_id as u32;
if static_type_id == ENUM || static_type_id == NAMED_ENUM || static_type_id == UNION {
return false;
}
is_user_type(static_type_id)
}
#[inline]
pub const fn field_need_write_ref_into(_type_id: u32, nullable: bool) -> bool {
nullable
}
#[inline(always)]
pub fn write_dyn_data_generic<T: Serializer>(
value: &T,
context: &mut WriteContext,
has_generics: bool,
) -> Result<(), Error> {
let any_value = value.as_any();
let concrete_type_id = any_value.type_id();
let serializer_fn = context
.write_any_type_info(T::fory_static_type_id() as u32, concrete_type_id)?
.get_harness()
.get_write_data_fn();
serializer_fn(any_value, context, has_generics)
}