-
Notifications
You must be signed in to change notification settings - Fork 358
feat: add unsigned number for dart #3144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
Hey @chaokunyang |
|
@ayush00git It's great to add wrapper with overlaoed numeric compute operator. But protobuf and flatfbuffer use int type instead, could we also support annotation based approach like we did in java: https://fory.apache.org/docs/next/guide/java/field_configuration#unsigned-integers Support both approaches will provide maximal flexibility to our users |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds support for unsigned integer types in Dart to enable cross-language serialization compatibility. Since Dart only has 64-bit signed integers, these wrapper classes provide proper unsigned integer semantics with automatic overflow/underflow wrapping behavior.
Changes:
- Added UInt8, UInt16, and UInt32 wrapper classes with automatic wrapping and full operator support
- Implemented 7 new serializers for unsigned types (UINT8, UINT16, UINT32, VAR_UINT32, UINT64, VAR_UINT64, TAGGED_UINT64)
- Added comprehensive test coverage for all unsigned type behaviors
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 9 comments.
Show a summary per file
| File | Description |
|---|---|
dart/packages/fory/lib/src/datatype/uint8.dart |
New UInt8 class with 8-bit unsigned integer wrapping logic and operator overloads |
dart/packages/fory/lib/src/datatype/uint16.dart |
New UInt16 class with 16-bit unsigned integer wrapping logic and operator overloads |
dart/packages/fory/lib/src/datatype/uint32.dart |
New UInt32 class with 32-bit unsigned integer wrapping logic and operator overloads |
dart/packages/fory/lib/src/const/obj_type.dart |
Added 7 new unsigned type enum values (UINT8-TAGGED_UINT64) and updated ID range check |
dart/packages/fory/lib/src/serializer/primitive_type_serializer.dart |
Implemented 7 serializers with caching for all unsigned integer types |
dart/packages/fory-test/test/datatype_test/uint_test.dart |
Added comprehensive tests for UInt8, UInt16, UInt32 covering wrapping, operators, and edge cases |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
@chaokunyang |
|
Hey @chaokunyang |
Why?
Dart doesn't have native unsigned integer types (only a single 64-bit signed int but many other languages (Rust, Go, C++, JavaScript) do. When serializing data across languages, we need to properly handle unsigned integers to ensure correct values and efficient encoding.
For example:
u32with value3_000_000_000cannot be directly represented in Dart without proper unsigned handlingWhat does this PR do?
1. Adds Unsigned Integer Type Support (Dart)
New type IDs:
UINT8(40),UINT16(41),UINT32(42),VAR_UINT32(43),UINT64(44),VAR_UINT64(45),TAGGED_UINT64(46)Unsigned types use the same bit width as signed types but interpret values in the unsigned range.
2. Dart: Adds Wrapper Classes for Fixed-Size Unsigned Integers
Created
UInt8UInt16andUInt32wrapper classes that extendFixedNum:Features:
Core wrapping logic:
3. Dart: Adds Serializers for All Unsigned Types
Implemented 7 serializer classes following the existing cache pattern:
ByteReader/Writer methods used:
readUint8/16/32(),writeUint8/16/32()readVarUint32(),writeVarUint32()readUint64(),writeUint64()readVarInt64(),writeVarInt64()4. Dart: Adds Comprehensive Test Suite
Created test coverage for all unsigned types:
Run tests:
Related Issue
Does this PR introduce any user-facing change?
Does this PR introduce any public API change?
UInt8,UInt16,UInt32ObjType:UINT8,UINT16,UINT32,VAR_UINT32,UINT64,VAR_UINT64,TAGGED_UINT64Does this PR introduce any binary protocol compatibility change?
Benchmark
N/A - This PR focuses on correctness and cross-language compatibility. Performance characteristics of unsigned types are similar to their signed counterparts.