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: add BorshSchema implementation for core::num::NonZero... integers #214

Merged
merged 4 commits into from
Sep 12, 2023
Merged
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
29 changes: 24 additions & 5 deletions borsh/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ impl BorshSchema for () {
}

macro_rules! impl_for_renamed_primitives {
($($type: ident : $name: ident)+) => {
($($type: ty : $name: ident)+) => {
$(
impl BorshSchema for $type {
fn add_definitions_recursively(_definitions: &mut BTreeMap<Declaration, Definition>) {}
Expand All @@ -289,6 +289,19 @@ impl_for_renamed_primitives!(str: string);
impl_for_renamed_primitives!(isize: i64);
impl_for_renamed_primitives!(usize: u64);

impl_for_renamed_primitives!(core::num::NonZeroI8: nonzero_i8);
impl_for_renamed_primitives!(core::num::NonZeroI16: nonzero_i16);
impl_for_renamed_primitives!(core::num::NonZeroI32: nonzero_i32);
impl_for_renamed_primitives!(core::num::NonZeroI64: nonzero_i64);
impl_for_renamed_primitives!(core::num::NonZeroI128: nonzero_i128);
impl_for_renamed_primitives!(core::num::NonZeroU8: nonzero_u8);
impl_for_renamed_primitives!(core::num::NonZeroU16: nonzero_u16);
impl_for_renamed_primitives!(core::num::NonZeroU32: nonzero_u32);
impl_for_renamed_primitives!(core::num::NonZeroU64: nonzero_u64);
impl_for_renamed_primitives!(core::num::NonZeroU128: nonzero_u128);
// see 12 lines above
impl_for_renamed_primitives!(core::num::NonZeroUsize: nonzero_u64);

impl<T, const N: usize> BorshSchema for [T; N]
where
T: BorshSchema,
Expand Down Expand Up @@ -625,13 +638,19 @@ mod tests {

#[test]
fn simple_tuple() {
let actual_name = <(u64, String)>::declaration();
let actual_name = <(u64, core::num::NonZeroU16, String)>::declaration();
let mut actual_defs = map!();
<(u64, String)>::add_definitions_recursively(&mut actual_defs);
assert_eq!("Tuple<u64, string>", actual_name);
<(u64, core::num::NonZeroU16, String)>::add_definitions_recursively(&mut actual_defs);
assert_eq!("Tuple<u64, nonzero_u16, string>", actual_name);
assert_eq!(
map! {
"Tuple<u64, string>" => Definition::Tuple { elements: vec![ "u64".to_string(), "string".to_string()]}
"Tuple<u64, nonzero_u16, string>" => Definition::Tuple {
elements: vec![
"u64".to_string(),
"nonzero_u16".to_string(),
"string".to_string()
]
}
},
actual_defs
);
Expand Down
13 changes: 8 additions & 5 deletions borsh/src/schema_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,11 @@ fn max_serialized_size_impl<'a>(

// Primitive types.
Err("nil") => Ok(0),
Err("bool" | "i8" | "u8") => Ok(count),
Err("i16" | "u16") => mul(count, 2),
Err("i32" | "u32" | "f32") => mul(count, 4),
Err("i64" | "u64" | "f64") => mul(count, 8),
Err("i128" | "u128") => mul(count, 16),
Err("bool" | "i8" | "u8" | "nonzero_i8" | "nonzero_u8") => Ok(count),
Err("i16" | "u16" | "nonzero_i16" | "nonzero_u16") => mul(count, 2),
Err("i32" | "u32" | "f32" | "nonzero_i32" | "nonzero_u32") => mul(count, 4),
Err("i64" | "u64" | "f64" | "nonzero_i64" | "nonzero_u64") => mul(count, 8),
Err("i128" | "u128" | "nonzero_i128" | "nonzero_u128") => mul(count, 16),

// string is just Vec<u8>
Err("string") => mul(count, add(MAX_LEN, 4)?),
Expand Down Expand Up @@ -278,6 +278,9 @@ mod tests {
test_ok::<u16>(2);
test_ok::<usize>(8);

test_ok::<core::num::NonZeroI16>(2);
test_ok::<core::num::NonZeroU32>(4);

test_ok::<Option<()>>(1);
test_ok::<Option<u8>>(2);
test_ok::<core::result::Result<u8, usize>>(9);
Expand Down
Loading