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: impl BorshSerialize/BorshDeserialize/BorshSchema for char #248

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
14 changes: 14 additions & 0 deletions borsh/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,20 @@ impl BorshDeserialize for bool {
}
}

impl BorshDeserialize for char {
#[inline]
fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
let int: u32 = BorshDeserialize::deserialize_reader(reader)?;

char::from_u32(int).ok_or_else(|| {
let msg = format!(
"to `char` conversion: u32 is not a valid Unicode Scalar Value: {:#x}",
int
);
Error::new(ErrorKind::InvalidData, msg)
})
}
}
impl<T> BorshDeserialize for Option<T>
where
T: BorshDeserialize,
Expand Down
15 changes: 15 additions & 0 deletions borsh/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ macro_rules! impl_for_primitives {
}

impl_for_primitives!(bool => 1; f32 => 4; f64 => 8; i8 => 1; i16 => 2; i32 => 4; i64 => 8; i128 => 16);
impl_for_primitives!(char => 4);
impl_for_primitives!(u8 => 1; u16 => 2; u32 => 4; u64 => 8; u128 => 16);
impl_for_renamed_primitives!(isize: i64 => 8);
impl_for_renamed_primitives!(usize: u64 => 8);
Expand Down Expand Up @@ -737,6 +738,20 @@ mod tests {
);
}

#[test]
fn char() {
let actual_name = char::declaration();
let mut actual_defs = map!();
char::add_definitions_recursively(&mut actual_defs);
assert_eq!("char", actual_name);
assert_eq!(
map! {
"char" => Definition::Primitive(4)
},
actual_defs
);
}

#[test]
fn nested_option() {
let actual_name = Option::<Option<u64>>::declaration();
Expand Down
1 change: 1 addition & 0 deletions borsh/src/schema/container_ext/max_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ mod tests {
fn max_serialized_size_primitives() {
test_ok::<()>(0);
test_ok::<bool>(1);
test_ok::<char>(4);

test_ok::<f32>(4);
test_ok::<f64>(8);
Expand Down
7 changes: 7 additions & 0 deletions borsh/src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ impl BorshSerialize for bool {
}
}

impl BorshSerialize for char {
#[inline]
fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> {
(*self as u32).serialize(writer)
}
}

impl<T> BorshSerialize for Option<T>
where
T: BorshSerialize,
Expand Down
10 changes: 10 additions & 0 deletions borsh/tests/snapshots/test_primitives__char.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: borsh/tests/test_primitives.rs
expression: buf
---
[
97,
0,
0,
0,
]
70 changes: 70 additions & 0 deletions borsh/tests/snapshots/test_primitives__char_vec.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
source: borsh/tests/test_primitives.rs
expression: buf
---
[
15,
0,
0,
0,
104,
0,
0,
0,
100,
39,
0,
0,
15,
254,
0,
0,
108,
0,
0,
0,
108,
0,
0,
0,
111,
0,
0,
0,
32,
0,
0,
0,
208,
143,
0,
0,
40,
117,
0,
0,
113,
95,
0,
0,
205,
84,
0,
0,
229,
78,
0,
0,
190,
143,
0,
0,
238,
118,
0,
0,
132,
118,
0,
0,
]
18 changes: 18 additions & 0 deletions borsh/tests/test_de_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,24 @@ fn test_zero_on_nonzero_integer_u32() {
);
}

#[test]
fn test_invalid_char_1() {
let bytes = &[0u8, 0u8, 17, 0u8];
assert_eq!(
from_slice::<char>(bytes).unwrap_err().to_string(),
"to `char` conversion: u32 is not a valid Unicode Scalar Value: 0x110000"
);
}

#[test]
fn test_invalid_char_2() {
let bytes = &[1u8, 222u8, 0u8, 0u8];
assert_eq!(
from_slice::<char>(bytes).unwrap_err().to_string(),
"to `char` conversion: u32 is not a valid Unicode Scalar Value: 0xde01"
);
}

#[test]
fn test_zero_on_nonzero_integer_i64() {
let bytes = &[0; 8];
Expand Down
21 changes: 21 additions & 0 deletions borsh/tests/test_primitives.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#![cfg_attr(not(feature = "std"), no_std)]

use borsh::{from_slice, to_vec};

#[cfg(not(feature = "std"))]
extern crate alloc;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

macro_rules! test_primitive {
($test_name: ident, $v: expr, $t: ty) => {
#[test]
Expand All @@ -24,3 +30,18 @@ test_primitive!(test_isize_max, isize::max_value(), isize);
test_primitive!(test_usize, 100usize, usize);
test_primitive!(test_usize_min, usize::min_value(), usize);
test_primitive!(test_usize_max, usize::max_value(), usize);

test_primitive!(test_char, 'a', char);

#[test]
fn test_char_vec() {
let initial = "h❤️llo 运用影响以达目的";
let expected = initial.chars().collect::<Vec<_>>();

let buf = to_vec(&expected).unwrap();
#[cfg(feature = "std")]
insta::assert_debug_snapshot!(buf);
let actual = from_slice::<Vec<char>>(&buf).expect("failed to deserialize");

assert_eq!(actual, expected);
}
Loading