Skip to content
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

feat(nr): serde for signed ints #9211

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ use crate::traits::{Serialize, Deserialize};

global BOOL_SERIALIZED_LEN: u32 = 1;
global U8_SERIALIZED_LEN: u32 = 1;
global U16_SERIALIZED_LEN: u32 = 1;
global U32_SERIALIZED_LEN: u32 = 1;
global U64_SERIALIZED_LEN: u32 = 1;
global U128_SERIALIZED_LEN: u32 = 1;
global FIELD_SERIALIZED_LEN: u32 = 1;
global I8_SERIALIZED_LEN: u32 = 1;
global I16_SERIALIZED_LEN: u32 = 1;
global I32_SERIALIZED_LEN: u32 = 1;
global I64_SERIALIZED_LEN: u32 = 1;

impl Serialize<BOOL_SERIALIZED_LEN> for bool {
fn serialize(self) -> [Field; BOOL_SERIALIZED_LEN] {
Expand All @@ -31,6 +36,18 @@ impl Deserialize<U8_SERIALIZED_LEN> for u8 {
}
}

impl Serialize<U16_SERIALIZED_LEN> for u16 {
fn serialize(self) -> [Field; U16_SERIALIZED_LEN] {
[self as Field]
}
}

impl Deserialize<U16_SERIALIZED_LEN> for u16 {
fn deserialize(fields: [Field; U16_SERIALIZED_LEN]) -> Self {
fields[0] as u16
}
}

impl Serialize<U32_SERIALIZED_LEN> for u32 {
fn serialize(self) -> [Field; U32_SERIALIZED_LEN] {
[self as Field]
Expand Down Expand Up @@ -79,6 +96,54 @@ impl Deserialize<FIELD_SERIALIZED_LEN> for Field {
}
}

impl Serialize<I8_SERIALIZED_LEN> for i8 {
fn serialize(self) -> [Field; I8_SERIALIZED_LEN] {
[self as Field]
}
}

impl Deserialize<I8_SERIALIZED_LEN> for i8 {
fn deserialize(fields: [Field; I8_SERIALIZED_LEN]) -> Self {
fields[0] as i8
}
}

impl Serialize<I16_SERIALIZED_LEN> for i16 {
fn serialize(self) -> [Field; I16_SERIALIZED_LEN] {
[self as Field]
}
}

impl Deserialize<I16_SERIALIZED_LEN> for i16 {
fn deserialize(fields: [Field; I16_SERIALIZED_LEN]) -> Self {
fields[0] as i16
}
}

impl Serialize<I32_SERIALIZED_LEN> for i32 {
fn serialize(self) -> [Field; I32_SERIALIZED_LEN] {
[self as Field]
}
}

impl Deserialize<I32_SERIALIZED_LEN> for i32 {
fn deserialize(fields: [Field; I32_SERIALIZED_LEN]) -> Self {
fields[0] as i32
}
}

impl Serialize<I64_SERIALIZED_LEN> for i64 {
fn serialize(self) -> [Field; I64_SERIALIZED_LEN] {
[self as Field]
}
}

impl Deserialize<I64_SERIALIZED_LEN> for i64 {
fn deserialize(fields: [Field; I64_SERIALIZED_LEN]) -> Self {
fields[0] as i64
}
}

impl<T, let N: u32, let M: u32> Serialize<N * M> for [T; N]
where
T: Serialize<M>,
Expand Down Expand Up @@ -106,3 +171,33 @@ where
reader.read_struct_array::<T, M, N>(Deserialize::deserialize, result)
}
}

#[test]
fn test_u16_serialization() {
let a: u16 = 10;
assert_eq(a, u16::deserialize(a.serialize()));
}

#[test]
fn test_i8_serialization() {
let a: i8 = -10;
assert_eq(a, i8::deserialize(a.serialize()));
}

#[test]
fn test_i16_serialization() {
let a: i16 = -10;
assert_eq(a, i16::deserialize(a.serialize()));
}

#[test]
fn test_i32_serialization() {
let a: i32 = -10;
assert_eq(a, i32::deserialize(a.serialize()));
}

#[test]
fn test_i64_serialization() {
let a: i64 = -10;
assert_eq(a, i64::deserialize(a.serialize()));
}
Loading