fory_core/serializer/
string.rs1use super::codec::SerializerCodec;
19use super::pointee::{PointeeCodec, PointeeSerializer};
20use crate::context::{ReadContext, WriteContext};
21use crate::error::Error;
22use crate::resolver::RefMode;
23use crate::serializer::util::read_basic_type_info;
24use crate::serializer::Serializer;
25use crate::type_id::TypeId;
26use std::convert::TryFrom;
27use std::sync::Arc;
28
29#[allow(dead_code)]
30enum StrEncoding {
31 Latin1 = 0,
32 Utf16 = 1,
33 Utf8 = 2,
34}
35
36#[inline(always)]
37pub(super) fn checked_string_len(len: u64) -> Result<usize, Error> {
38 match usize::try_from(len) {
39 Ok(len) => Ok(len),
40 Err(_) => Err(string_len_overflow(len)),
41 }
42}
43
44#[cold]
45#[inline(never)]
46fn string_len_overflow(len: u64) -> Error {
47 Error::invalid_data(format!(
48 "string byte length {len} exceeds the platform address space"
49 ))
50}
51
52impl Serializer for String {
53 type Target = Self;
54
55 const READ_DATA_ALWAYS_ADVANCES: bool = true;
56
57 #[inline(always)]
58 fn write_data(value: &Self, context: &mut WriteContext) -> Result<(), Error> {
59 <Self as PointeeSerializer<str>>::write_pointee_data(value, context)
60 }
61
62 #[inline(always)]
63 fn read_data(context: &mut ReadContext) -> Result<Self, Error> {
64 let header = context.reader.read_var_u36_small()?;
65 let len = checked_string_len(header >> 2)?;
66 match header & 0b11 {
67 0 => context.reader.read_latin1_string(len),
68 1 => context.reader.read_utf16_string(len),
69 2 if context.is_check_string_read() => context.reader.read_utf8_string(len),
70 2 => unsafe { context.reader.read_utf8_string_unchecked(len) },
73 encoding => Err(Error::encoding_error(format!(
74 "wrong encoding value: {}",
75 encoding
76 ))),
77 }
78 }
79
80 #[inline(always)]
81 fn default_value(_: &mut ReadContext) -> Result<Self, Error> {
82 Ok(String::new())
83 }
84
85 #[inline(always)]
86 fn read_arc_any(
87 context: &mut ReadContext,
88 ) -> Result<Arc<dyn std::any::Any + Send + Sync>, Error> {
89 Ok(Arc::new(Self::read_data(context)?))
90 }
91
92 #[inline(always)]
93 fn reserved_space() -> usize {
94 std::mem::size_of::<i32>()
95 }
96
97 #[inline(always)]
98 fn static_type_id() -> TypeId {
99 TypeId::STRING
100 }
101
102 #[inline(always)]
103 fn write_type_info(context: &mut WriteContext) -> Result<(), Error> {
104 context.writer.write_u8(TypeId::STRING as u8);
105 Ok(())
106 }
107
108 #[inline(always)]
109 fn read_type_info(context: &mut ReadContext) -> Result<(), Error> {
110 read_basic_type_info::<Self>(context)
111 }
112}
113
114impl PointeeSerializer<str> for String {
115 const POINTEE_SIZE: usize = 0;
116
117 #[inline(always)]
118 fn write_pointee_data(value: &str, context: &mut WriteContext) -> Result<(), Error> {
119 let header = (value.len() as i32 as u64) << 2 | StrEncoding::Utf8 as u64;
120 context.writer.write_var_u36_small(header);
121 context.writer.write_utf8_string(value);
122 Ok(())
123 }
124}
125
126impl<const NULLABLE: bool, const TRACK_REF: bool> PointeeSerializer<str>
127 for SerializerCodec<String, NULLABLE, TRACK_REF>
128{
129 const POINTEE_SIZE: usize = 0;
130
131 #[inline(always)]
132 fn write_pointee_data(value: &str, context: &mut WriteContext) -> Result<(), Error> {
133 String::write_pointee_data(value, context)
134 }
135}
136
137impl<const NULLABLE: bool, const TRACK_REF: bool> PointeeCodec<str, String>
138 for SerializerCodec<String, NULLABLE, TRACK_REF>
139{
140 #[inline(always)]
141 fn write_pointee_with_mode(
142 value: &str,
143 context: &mut WriteContext,
144 ref_mode: RefMode,
145 write_type_info: bool,
146 _has_generics: bool,
147 ) -> Result<(), Error> {
148 Self::write_pointee(value, context, ref_mode, write_type_info)
149 }
150}
151
152#[cfg(test)]
153mod tests {
154 use super::*;
155
156 #[test]
157 fn checked_length_range() {
158 assert_eq!(checked_string_len(17).unwrap(), 17);
159
160 let beyond_u32 = u64::from(u32::MAX) + 1;
161 if usize::BITS > u32::BITS {
162 assert_eq!(checked_string_len(beyond_u32).unwrap() as u64, beyond_u32);
163 } else {
164 assert!(checked_string_len(beyond_u32).is_err());
165 }
166 }
167}