Skip to content

Commit

Permalink
feat(nr): serde for signed ints (#9211)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maddiaa0 authored Oct 24, 2024
1 parent 008fdd1 commit 66f31c7
Showing 1 changed file with 95 additions and 0 deletions.
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()));
}

0 comments on commit 66f31c7

Please sign in to comment.