This is an automated email from the ASF dual-hosted git repository.
chaokunyang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/fory.git
The following commit(s) were added to refs/heads/main by this push:
new 18bfea920 feat(Rust): Support usize (#2870)
18bfea920 is described below
commit 18bfea920a7af5cc6e8ea21d63d9575771efabfd
Author: urlyy <[email protected]>
AuthorDate: Sun Nov 2 15:15:33 2025 +0800
feat(Rust): Support usize (#2870)
## What does this PR do?
Add support for `usize`.
## Related issues
#2857
#2859
---
rust/fory-core/src/buffer.rs | 13 ++
rust/fory-core/src/fory.rs | 2 +-
rust/fory-core/src/meta/type_meta.rs | 1 +
rust/fory-core/src/resolver/type_resolver.rs | 2 +
rust/fory-core/src/serializer/list.rs | 2 +
rust/fory-core/src/serializer/primitive_list.rs | 2 +-
rust/fory-core/src/serializer/skip.rs | 6 +
rust/fory-core/src/serializer/unsigned_number.rs | 6 +
rust/fory-core/src/serializer/util.rs | 3 +-
rust/fory-core/src/types.rs | 49 ++++----
rust/fory-derive/src/object/util.rs | 7 +-
rust/tests/tests/test_unsigned.rs | 148 +++++++++++++++--------
12 files changed, 156 insertions(+), 85 deletions(-)
diff --git a/rust/fory-core/src/buffer.rs b/rust/fory-core/src/buffer.rs
index b4a658268..b91d9617b 100644
--- a/rust/fory-core/src/buffer.rs
+++ b/rust/fory-core/src/buffer.rs
@@ -129,6 +129,11 @@ impl<'a> Writer<'a> {
self.bf.write_u64::<LittleEndian>(value).unwrap();
}
+ #[inline(always)]
+ pub fn write_usize(&mut self, value: usize) {
+ self.bf.write_u64::<LittleEndian>(value as u64).unwrap();
+ }
+
#[inline(always)]
pub fn write_varint32(&mut self, value: i32) {
let zigzag = ((value as i64) << 1) ^ ((value as i64) >> 31);
@@ -468,6 +473,14 @@ impl<'a> Reader<'a> {
Ok(result)
}
+ #[inline(always)]
+ pub fn read_usize(&mut self) -> Result<usize, Error> {
+ let slice = self.slice_after_cursor();
+ let result = LittleEndian::read_u64(slice);
+ self.move_next(8);
+ Ok(result as usize)
+ }
+
#[inline(always)]
pub fn read_i64(&mut self) -> Result<i64, Error> {
Ok(self.read_u64()? as i64)
diff --git a/rust/fory-core/src/fory.rs b/rust/fory-core/src/fory.rs
index 5d02f699f..d9a046bb3 100644
--- a/rust/fory-core/src/fory.rs
+++ b/rust/fory-core/src/fory.rs
@@ -298,7 +298,7 @@ impl Fory {
///
/// # Returns
///
- /// `ture` if the serialization mode is compatible, `false` otherwise`.
+ /// `true` if the serialization mode is compatible, `false` otherwise`.
pub fn is_compatible(&self) -> bool {
self.compatible
}
diff --git a/rust/fory-core/src/meta/type_meta.rs
b/rust/fory-core/src/meta/type_meta.rs
index 387e33b37..d105b0344 100644
--- a/rust/fory-core/src/meta/type_meta.rs
+++ b/rust/fory-core/src/meta/type_meta.rs
@@ -417,6 +417,7 @@ impl TypeMetaLayer {
TypeId::U16 => 2,
TypeId::U32 => 4,
TypeId::U64 => 8,
+ TypeId::USIZE => 8,
_ => unreachable!(),
}
}
diff --git a/rust/fory-core/src/resolver/type_resolver.rs
b/rust/fory-core/src/resolver/type_resolver.rs
index 9b0a5ae7b..e1faf81ed 100644
--- a/rust/fory-core/src/resolver/type_resolver.rs
+++ b/rust/fory-core/src/resolver/type_resolver.rs
@@ -461,6 +461,7 @@ impl TypeResolver {
self.register_internal_serializer::<u16>(TypeId::U16)?;
self.register_internal_serializer::<u32>(TypeId::U32)?;
self.register_internal_serializer::<u64>(TypeId::U64)?;
+ self.register_internal_serializer::<usize>(TypeId::USIZE)?;
self.register_internal_serializer::<String>(TypeId::STRING)?;
self.register_internal_serializer::<NaiveDateTime>(TypeId::TIMESTAMP)?;
self.register_internal_serializer::<NaiveDate>(TypeId::LOCAL_DATE)?;
@@ -476,6 +477,7 @@ impl TypeResolver {
self.register_internal_serializer::<Vec<u16>>(TypeId::U16_ARRAY)?;
self.register_internal_serializer::<Vec<u32>>(TypeId::U32_ARRAY)?;
self.register_internal_serializer::<Vec<u64>>(TypeId::U64_ARRAY)?;
+ self.register_internal_serializer::<Vec<usize>>(TypeId::USIZE_ARRAY)?;
self.register_generic_trait::<Vec<String>>()?;
self.register_generic_trait::<LinkedList<i32>>()?;
self.register_generic_trait::<LinkedList<String>>()?;
diff --git a/rust/fory-core/src/serializer/list.rs
b/rust/fory-core/src/serializer/list.rs
index 504a5b992..4db945d76 100644
--- a/rust/fory-core/src/serializer/list.rs
+++ b/rust/fory-core/src/serializer/list.rs
@@ -46,6 +46,7 @@ fn get_primitive_type_id<T: Serializer>() -> TypeId {
TypeId::U16 => TypeId::U16_ARRAY,
TypeId::U32 => TypeId::U32_ARRAY,
TypeId::U64 => TypeId::U64_ARRAY,
+ TypeId::USIZE => TypeId::USIZE_ARRAY,
_ => TypeId::UNKNOWN,
}
}
@@ -68,6 +69,7 @@ pub fn is_primitive_type<T: Serializer>() -> bool {
| TypeId::U16
| TypeId::U32
| TypeId::U64
+ | TypeId::USIZE,
)
}
diff --git a/rust/fory-core/src/serializer/primitive_list.rs
b/rust/fory-core/src/serializer/primitive_list.rs
index fc31ed748..168e56178 100644
--- a/rust/fory-core/src/serializer/primitive_list.rs
+++ b/rust/fory-core/src/serializer/primitive_list.rs
@@ -26,7 +26,7 @@ pub fn fory_write_data<T: Serializer>(this: &[T], context:
&mut WriteContext) ->
if context.is_xlang()
&& matches!(
T::fory_static_type_id(),
- TypeId::U16 | TypeId::U32 | TypeId::U64
+ TypeId::U8 | TypeId::U16 | TypeId::U32 | TypeId::U64 |
TypeId::USIZE
)
{
return Err(Error::not_allowed(
diff --git a/rust/fory-core/src/serializer/skip.rs
b/rust/fory-core/src/serializer/skip.rs
index 1af4e5c6b..17e2cd0ae 100644
--- a/rust/fory-core/src/serializer/skip.rs
+++ b/rust/fory-core/src/serializer/skip.rs
@@ -417,6 +417,9 @@ fn skip_value(
types::U64 => {
<u64 as Serializer>::fory_read_data(context)?;
}
+ types::USIZE => {
+ <usize as Serializer>::fory_read_data(context)?;
+ }
types::U16_ARRAY => {
<Vec<u16> as Serializer>::fory_read_data(context)?;
}
@@ -426,6 +429,9 @@ fn skip_value(
types::U64_ARRAY => {
<Vec<u64> as Serializer>::fory_read_data(context)?;
}
+ types::USIZE_ARRAY => {
+ <Vec<usize> as Serializer>::fory_read_data(context)?;
+ }
// Container types
types::LIST | types::SET => {
diff --git a/rust/fory-core/src/serializer/unsigned_number.rs
b/rust/fory-core/src/serializer/unsigned_number.rs
index 4ac8089da..9277b7e4d 100644
--- a/rust/fory-core/src/serializer/unsigned_number.rs
+++ b/rust/fory-core/src/serializer/unsigned_number.rs
@@ -92,3 +92,9 @@ impl_unsigned_num_serializer!(u8, Writer::write_u8,
Reader::read_u8, TypeId::U8)
impl_unsigned_num_serializer!(u16, Writer::write_u16, Reader::read_u16,
TypeId::U16);
impl_unsigned_num_serializer!(u32, Writer::write_u32, Reader::read_u32,
TypeId::U32);
impl_unsigned_num_serializer!(u64, Writer::write_u64, Reader::read_u64,
TypeId::U64);
+impl_unsigned_num_serializer!(
+ usize,
+ Writer::write_usize,
+ Reader::read_usize,
+ TypeId::USIZE
+);
diff --git a/rust/fory-core/src/serializer/util.rs
b/rust/fory-core/src/serializer/util.rs
index 586f3552b..5655719bb 100644
--- a/rust/fory-core/src/serializer/util.rs
+++ b/rust/fory-core/src/serializer/util.rs
@@ -22,7 +22,7 @@ use crate::serializer::Serializer;
use crate::types::TypeId;
use crate::types::{is_user_type, ENUM, NAMED_ENUM};
-const NO_REF_FLAG_TYPE_IDS: [u32; 11] = [
+const NO_REF_FLAG_TYPE_IDS: [u32; 12] = [
TypeId::BOOL as u32,
TypeId::INT8 as u32,
TypeId::INT16 as u32,
@@ -34,6 +34,7 @@ const NO_REF_FLAG_TYPE_IDS: [u32; 11] = [
TypeId::U16 as u32,
TypeId::U32 as u32,
TypeId::U64 as u32,
+ TypeId::USIZE as u32,
];
#[inline(always)]
diff --git a/rust/fory-core/src/types.rs b/rust/fory-core/src/types.rs
index 430fe8ff8..6577d1ab5 100644
--- a/rust/fory-core/src/types.rs
+++ b/rust/fory-core/src/types.rs
@@ -85,13 +85,15 @@ pub enum TypeId {
U16 = 65,
U32 = 66,
U64 = 67,
- VAR_U32 = 68,
- VAR_U64 = 69,
- SLI_U64 = 70,
- U16_ARRAY = 71,
- U32_ARRAY = 72,
- U64_ARRAY = 73,
- UNKNOWN = 74,
+ USIZE = 68,
+ VAR_U32 = 69,
+ VAR_U64 = 70,
+ SLI_U64 = 71,
+ U16_ARRAY = 72,
+ U32_ARRAY = 73,
+ U64_ARRAY = 74,
+ USIZE_ARRAY = 75,
+ UNKNOWN = 76,
}
pub const BOOL: u32 = TypeId::BOOL as u32;
@@ -137,12 +139,14 @@ pub const U8: u32 = TypeId::U8 as u32;
pub const U16: u32 = TypeId::U16 as u32;
pub const U32: u32 = TypeId::U32 as u32;
pub const U64: u32 = TypeId::U64 as u32;
+pub const USIZE: u32 = TypeId::USIZE as u32;
pub const VAR_U32: u32 = TypeId::VAR_U32 as u32;
pub const VAR_U64: u32 = TypeId::VAR_U64 as u32;
pub const SLI_U64: u32 = TypeId::SLI_U64 as u32;
pub const U16_ARRAY: u32 = TypeId::U16_ARRAY as u32;
pub const U32_ARRAY: u32 = TypeId::U32_ARRAY as u32;
pub const U64_ARRAY: u32 = TypeId::U64_ARRAY as u32;
+pub const USIZE_ARRAY: u32 = TypeId::USIZE_ARRAY as u32;
pub const UNKNOWN: u32 = TypeId::UNKNOWN as u32;
const MAX_UNT32: u64 = (1 << 31) - 1;
@@ -160,7 +164,7 @@ pub fn compute_string_hash(s: &str) -> u32 {
hash as u32
}
-pub static BASIC_TYPES: [TypeId; 25] = [
+pub static BASIC_TYPES: [TypeId; 27] = [
TypeId::BOOL,
TypeId::INT8,
TypeId::INT16,
@@ -183,12 +187,14 @@ pub static BASIC_TYPES: [TypeId; 25] = [
TypeId::U16,
TypeId::U32,
TypeId::U64,
+ TypeId::USIZE,
TypeId::U16_ARRAY,
TypeId::U32_ARRAY,
TypeId::U64_ARRAY,
+ TypeId::USIZE_ARRAY,
];
-pub static PRIMITIVE_TYPES: [u32; 11] = [
+pub static PRIMITIVE_TYPES: [u32; 12] = [
TypeId::BOOL as u32,
TypeId::INT8 as u32,
TypeId::INT16 as u32,
@@ -200,9 +206,10 @@ pub static PRIMITIVE_TYPES: [u32; 11] = [
TypeId::U16 as u32,
TypeId::U32 as u32,
TypeId::U64 as u32,
+ TypeId::USIZE as u32,
];
-pub static PRIMITIVE_ARRAY_TYPES: [u32; 11] = [
+pub static PRIMITIVE_ARRAY_TYPES: [u32; 12] = [
TypeId::BOOL_ARRAY as u32,
TypeId::BINARY as u32,
TypeId::INT8_ARRAY as u32,
@@ -214,9 +221,10 @@ pub static PRIMITIVE_ARRAY_TYPES: [u32; 11] = [
TypeId::U16_ARRAY as u32,
TypeId::U32_ARRAY as u32,
TypeId::U64_ARRAY as u32,
+ TypeId::U64_ARRAY as u32,
];
-pub static BASIC_TYPE_NAMES: [&str; 14] = [
+pub static BASIC_TYPE_NAMES: [&str; 15] = [
"bool",
"i8",
"i16",
@@ -231,6 +239,7 @@ pub static BASIC_TYPE_NAMES: [&str; 14] = [
"u16",
"u32",
"u64",
+ "usize",
];
pub static CONTAINER_TYPES: [TypeId; 3] = [TypeId::LIST, TypeId::SET,
TypeId::MAP];
@@ -249,6 +258,7 @@ pub static PRIMITIVE_ARRAY_TYPE_MAP: &[(&str, u32, &str)] =
&[
("u16", TypeId::U16_ARRAY as u32, "Vec<u16>"),
("u32", TypeId::U32_ARRAY as u32, "Vec<u32>"),
("u64", TypeId::U64_ARRAY as u32, "Vec<u64>"),
+ ("usize", TypeId::USIZE_ARRAY as u32, "Vec<usize>"),
];
#[inline(always)]
@@ -266,6 +276,7 @@ pub fn is_primitive_type_id(type_id: TypeId) -> bool {
| TypeId::U16
| TypeId::U32
| TypeId::U64
+ | TypeId::USIZE
)
}
@@ -323,22 +334,6 @@ pub fn compute_field_hash(hash: u32, id: i16) -> u32 {
new_hash as u32
}
-// pub fn compute_struct_hash(props: Vec<(&str, FieldType)>) -> u32 {
-// let mut hash = 17;
-// props.iter().for_each(|prop| {
-// let (_name, ty) = prop;
-// hash = match ty {
-// FieldType::ARRAY | FieldType::MAP => compute_field_hash(hash,
*ty as i16),
-// _ => hash,
-// };
-// let is_basic_type = BASIC_TYPES.contains(ty);
-// if is_basic_type {
-// hash = compute_field_hash(hash, *ty as i16);
-// }
-// });
-// hash
-// }
-
pub mod config_flags {
pub const IS_NULL_FLAG: u8 = 1 << 0;
pub const IS_LITTLE_ENDIAN_FLAG: u8 = 2;
diff --git a/rust/fory-derive/src/object/util.rs
b/rust/fory-derive/src/object/util.rs
index 92e184ced..cee4f3370 100644
--- a/rust/fory-derive/src/object/util.rs
+++ b/rust/fory-derive/src/object/util.rs
@@ -484,8 +484,8 @@ fn extract_option_inner(s: &str) -> Option<&str> {
s.strip_prefix("Option<")?.strip_suffix(">")
}
-const PRIMITIVE_TYPE_NAMES: [&str; 11] = [
- "bool", "i8", "i16", "i32", "i64", "f32", "f64", "u8", "u16", "u32", "u64",
+const PRIMITIVE_TYPE_NAMES: [&str; 12] = [
+ "bool", "i8", "i16", "i32", "i64", "f32", "f64", "u8", "u16", "u32",
"u64", "usize",
];
fn get_primitive_type_id(ty: &str) -> u32 {
@@ -501,6 +501,7 @@ fn get_primitive_type_id(ty: &str) -> u32 {
"u16" => TypeId::U16 as u32,
"u32" => TypeId::U32 as u32,
"u64" => TypeId::U64 as u32,
+ "usize" => TypeId::USIZE as u32,
_ => unreachable!("Unknown primitive type: {}", ty),
}
}
@@ -555,6 +556,7 @@ pub(crate) fn get_type_id_by_name(ty: &str) -> u32 {
"Vec<u16>" => return TypeId::U16_ARRAY as u32,
"Vec<u32>" => return TypeId::U32_ARRAY as u32,
"Vec<u64>" => return TypeId::U64_ARRAY as u32,
+ "Vec<usize>" => return TypeId::USIZE_ARRAY as u32,
_ => {}
}
@@ -601,6 +603,7 @@ fn get_primitive_type_size(type_id_num: u32) -> i32 {
TypeId::U16 => 2,
TypeId::U32 => 4,
TypeId::U64 => 8,
+ TypeId::USIZE => 8,
_ => unreachable!(),
}
}
diff --git a/rust/tests/tests/test_unsigned.rs
b/rust/tests/tests/test_unsigned.rs
index d667c12f5..68d2b1f2d 100644
--- a/rust/tests/tests/test_unsigned.rs
+++ b/rust/tests/tests/test_unsigned.rs
@@ -24,22 +24,31 @@ use test_helpers::{test_arc_any, test_box_any, test_rc_any,
test_roundtrip};
#[test]
fn test_unsigned_numbers() {
let fory = Fory::default();
- test_roundtrip(&fory, 255u8);
- test_roundtrip(&fory, 65535u16);
- test_roundtrip(&fory, 4294967295u32);
- test_roundtrip(&fory, 18446744073709551615u64);
+ test_roundtrip(&fory, u8::MAX);
+ test_roundtrip(&fory, u16::MAX);
+ test_roundtrip(&fory, u32::MAX);
+ test_roundtrip(&fory, u64::MAX);
+ test_roundtrip(&fory, usize::MAX);
}
#[test]
fn test_unsigned_arrays() {
let fory = Fory::default();
- test_roundtrip(&fory, vec![0u8, 1, 2, 255]);
- test_roundtrip(&fory, vec![0u16, 100, 1000, 65535]);
- test_roundtrip(&fory, vec![0u32, 1000, 1000000, 4294967295]);
- test_roundtrip(
- &fory,
- vec![0u64, 1000000, 1000000000000, 18446744073709551615],
- );
+ test_roundtrip(&fory, vec![0u8, 1, 2, u8::MAX]);
+ test_roundtrip(&fory, vec![0u16, 100, 1000, u16::MAX]);
+ test_roundtrip(&fory, vec![0u32, 1000, 1000000, u32::MAX]);
+ test_roundtrip(&fory, vec![0u64, 1000000, 1000000000000, u64::MAX]);
+ test_roundtrip(&fory, vec![0usize, 1000000, 1000000000000, usize::MAX]);
+}
+
+#[test]
+fn test_unsigned_arrays_when_xlang() {
+ let fory = Fory::default().xlang(true);
+ assert!(fory.serialize(&vec![u8::MAX]).is_err());
+ assert!(fory.serialize(&vec![u16::MAX]).is_err());
+ assert!(fory.serialize(&vec![u32::MAX]).is_err());
+ assert!(fory.serialize(&vec![u64::MAX]).is_err());
+ assert!(fory.serialize(&vec![usize::MAX]).is_err());
}
#[test]
@@ -50,22 +59,28 @@ fn test_unsigned_struct_non_compatible() {
b: u16,
c: u32,
d: u64,
+ e: usize,
+ vec_u8: Vec<u8>,
vec_u16: Vec<u16>,
vec_u32: Vec<u32>,
vec_u64: Vec<u64>,
+ vec_usize: Vec<usize>,
}
let mut fory = Fory::default();
fory.register::<UnsignedData>(100).unwrap();
let data = UnsignedData {
- a: 255,
- b: 65535,
- c: 4294967295,
- d: 18446744073709551615,
- vec_u16: vec![0, 100, 1000, 65535],
- vec_u32: vec![0, 1000, 1000000, 4294967295],
- vec_u64: vec![0, 1000000, 1000000000000, 18446744073709551615],
+ a: u8::MAX,
+ b: u16::MAX,
+ c: u32::MAX,
+ d: u64::MAX,
+ e: usize::MAX,
+ vec_u8: vec![0, 1, 10, u8::MAX],
+ vec_u16: vec![0, 100, 1000, u16::MAX],
+ vec_u32: vec![0, 1000, 1000000, u32::MAX],
+ vec_u64: vec![0, 1000000, 1000000000000, u64::MAX],
+ vec_usize: vec![0, 1000000, 1000000000000, usize::MAX],
};
let bytes = fory.serialize(&data).unwrap();
@@ -81,22 +96,28 @@ fn test_unsigned_struct_compatible() {
b: u16,
c: u32,
d: u64,
+ e: usize,
+ vec_u8: Vec<u8>,
vec_u16: Vec<u16>,
vec_u32: Vec<u32>,
vec_u64: Vec<u64>,
+ vec_usize: Vec<usize>,
}
let mut fory = Fory::default().compatible(true);
fory.register::<UnsignedData>(100).unwrap();
let data = UnsignedData {
- a: 255,
- b: 65535,
- c: 4294967295,
- d: 18446744073709551615,
- vec_u16: vec![0, 100, 1000, 65535],
- vec_u32: vec![0, 1000, 1000000, 4294967295],
- vec_u64: vec![0, 1000000, 1000000000000, 18446744073709551615],
+ a: u8::MAX,
+ b: u16::MAX,
+ c: u32::MAX,
+ d: u64::MAX,
+ e: usize::MAX,
+ vec_u8: vec![0, 1, 10, u8::MAX],
+ vec_u16: vec![0, 100, 1000, u16::MAX],
+ vec_u32: vec![0, 1000, 1000000, u32::MAX],
+ vec_u64: vec![0, 1000000, 1000000000000, u64::MAX],
+ vec_usize: vec![0, 1000000, 1000000000000, usize::MAX],
};
let bytes = fory.serialize(&data).unwrap();
@@ -173,18 +194,21 @@ fn test_unsigned_edge_cases() {
test_roundtrip(&fory, 0u16);
test_roundtrip(&fory, 0u32);
test_roundtrip(&fory, 0u64);
+ test_roundtrip(&fory, 0usize);
// Test maximum values
test_roundtrip(&fory, u8::MAX);
test_roundtrip(&fory, u16::MAX);
test_roundtrip(&fory, u32::MAX);
test_roundtrip(&fory, u64::MAX);
+ test_roundtrip(&fory, usize::MAX);
// Test empty arrays
test_roundtrip(&fory, Vec::<u8>::new());
test_roundtrip(&fory, Vec::<u16>::new());
test_roundtrip(&fory, Vec::<u32>::new());
test_roundtrip(&fory, Vec::<u64>::new());
+ test_roundtrip(&fory, Vec::<usize>::new());
}
#[test]
@@ -195,6 +219,7 @@ fn test_unsigned_with_option_non_compatible() {
opt_u16: Option<u16>,
opt_u32: Option<u32>,
opt_u64: Option<u64>,
+ opt_usize: Option<usize>,
}
let mut fory = Fory::default();
@@ -202,10 +227,11 @@ fn test_unsigned_with_option_non_compatible() {
// Test with Some values
let data_some = OptionalUnsigned {
- opt_u8: Some(255),
- opt_u16: Some(65535),
- opt_u32: Some(4294967295),
- opt_u64: Some(18446744073709551615),
+ opt_u8: Some(u8::MAX),
+ opt_u16: Some(u16::MAX),
+ opt_u32: Some(u32::MAX),
+ opt_u64: Some(u64::MAX),
+ opt_usize: Some(usize::MAX),
};
let bytes = fory.serialize(&data_some).unwrap();
@@ -218,6 +244,7 @@ fn test_unsigned_with_option_non_compatible() {
opt_u16: None,
opt_u32: None,
opt_u64: None,
+ opt_usize: None,
};
let bytes = fory.serialize(&data_none).unwrap();
@@ -233,6 +260,7 @@ fn test_unsigned_with_option_compatible() {
opt_u16: Option<u16>,
opt_u32: Option<u32>,
opt_u64: Option<u64>,
+ opt_usize: Option<usize>,
}
let mut fory = Fory::default().compatible(true);
@@ -240,10 +268,11 @@ fn test_unsigned_with_option_compatible() {
// Test with Some values
let data_some = OptionalUnsigned {
- opt_u8: Some(255),
- opt_u16: Some(65535),
- opt_u32: Some(4294967295),
- opt_u64: Some(18446744073709551615),
+ opt_u8: Some(u8::MAX),
+ opt_u16: Some(u16::MAX),
+ opt_u32: Some(u32::MAX),
+ opt_u64: Some(u64::MAX),
+ opt_usize: Some(usize::MAX),
};
let bytes = fory.serialize(&data_some).unwrap();
@@ -256,6 +285,7 @@ fn test_unsigned_with_option_compatible() {
opt_u16: None,
opt_u32: None,
opt_u64: None,
+ opt_usize: None,
};
let bytes = fory.serialize(&data_none).unwrap();
@@ -279,6 +309,7 @@ fn test_unsigned_mixed_fields_compatible() {
vec_u32: Vec<u32>,
new_u64: u64,
new_opt_u32: Option<u32>,
+ new_usize: usize,
}
let mut fory1 = Fory::default().compatible(true);
@@ -299,6 +330,7 @@ fn test_unsigned_mixed_fields_compatible() {
assert_eq!(result.vec_u32, vec![1000, 2000, 3000]);
assert_eq!(result.new_u64, 0); // Default value
assert_eq!(result.new_opt_u32, None); // Default value
+ assert_eq!(result.new_usize, 0)
}
#[test]
@@ -306,34 +338,44 @@ fn test_unsigned_with_smart_pointers() {
let fory = Fory::default();
// Test Box<dyn Any> with unsigned types
- test_box_any(&fory, 255u8);
- test_box_any(&fory, 65535u16);
- test_box_any(&fory, 4294967295u32);
- test_box_any(&fory, 18446744073709551615u64);
+ test_box_any(&fory, u8::MAX);
+ test_box_any(&fory, u16::MAX);
+ test_box_any(&fory, u32::MAX);
+ test_box_any(&fory, u64::MAX);
+ test_box_any(&fory, usize::MAX);
// Test Rc<dyn Any> with unsigned types
- test_rc_any(&fory, 255u8);
- test_rc_any(&fory, 65535u16);
- test_rc_any(&fory, 4294967295u32);
- test_rc_any(&fory, 18446744073709551615u64);
+ test_rc_any(&fory, u8::MAX);
+ test_rc_any(&fory, u16::MAX);
+ test_rc_any(&fory, u32::MAX);
+ test_rc_any(&fory, u64::MAX);
+ test_rc_any(&fory, usize::MAX);
// Test Arc<dyn Any> with unsigned types
- test_arc_any(&fory, 255u8);
- test_arc_any(&fory, 65535u16);
- test_arc_any(&fory, 4294967295u32);
- test_arc_any(&fory, 18446744073709551615u64);
+ test_arc_any(&fory, u8::MAX);
+ test_arc_any(&fory, u16::MAX);
+ test_arc_any(&fory, u32::MAX);
+ test_arc_any(&fory, u64::MAX);
+ test_arc_any(&fory, usize::MAX);
// Test Box<dyn Any> with unsigned arrays
- test_box_any(&fory, vec![0u8, 127, 255]);
- test_box_any(&fory, vec![0u16, 1000, 65535]);
- test_box_any(&fory, vec![0u32, 1000000, 4294967295]);
- test_box_any(&fory, vec![0u64, 1000000000000, 18446744073709551615]);
+ test_box_any(&fory, vec![0u8, 127, u8::MAX]);
+ test_box_any(&fory, vec![0u16, 1000, u16::MAX]);
+ test_box_any(&fory, vec![0u32, 1000000, u32::MAX]);
+ test_box_any(&fory, vec![0u64, 1000000000000, u64::MAX]);
+ test_box_any(&fory, vec![0usize, 1000000000000, usize::MAX]);
// Test Rc<dyn Any> with unsigned arrays
- test_rc_any(&fory, vec![100u16, 200, 300, 65535]);
- test_rc_any(&fory, vec![1000u32, 2000, 3000, 4294967295]);
+ test_rc_any(&fory, vec![0u8, 127, u8::MAX]);
+ test_rc_any(&fory, vec![100u16, 200, 300, u16::MAX]);
+ test_rc_any(&fory, vec![1000u32, 2000, 3000, u32::MAX]);
+ test_rc_any(&fory, vec![0u64, 1000000000000, u64::MAX]);
+ test_rc_any(&fory, vec![0usize, 1000000000000, usize::MAX]);
// Test Arc<dyn Any> with unsigned arrays
- test_arc_any(&fory, vec![999u32, 888, 777, 4294967295]);
- test_arc_any(&fory, vec![123u64, 456789, 987654321, 18446744073709551615]);
+ test_arc_any(&fory, vec![0u8, 127, u8::MAX]);
+ test_arc_any(&fory, vec![100u16, 200, 300, u16::MAX]);
+ test_arc_any(&fory, vec![999u32, 888, 777, u32::MAX]);
+ test_arc_any(&fory, vec![123u64, 456789, 987654321, u64::MAX]);
+ test_arc_any(&fory, vec![123usize, 456789, 987654321, usize::MAX]);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]