Skip to content

Commit

Permalink
feat!: add Definition::Primitive
Browse files Browse the repository at this point in the history
  • Loading branch information
dj8yf0μl committed Sep 15, 2023
1 parent d22259a commit ff32f39
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 93 deletions.
88 changes: 61 additions & 27 deletions borsh/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ pub type FieldName = String;
/// [varint]: https://en.wikipedia.org/wiki/Variable-length_quantity#Variants
#[derive(Clone, PartialEq, Eq, Debug, BorshSerialize, BorshDeserialize, BorshSchemaMacro)]
pub enum Definition {
/// A fixed-size type, which is considered undivisible
Primitive(u8),
/// A fixed-size array with the length known at the compile time and the same-type elements.
Array { length: u32, elements: Declaration },
/// A sequence of elements of length known at the run time and the same-type elements.
Expand Down Expand Up @@ -261,11 +263,14 @@ where
}

macro_rules! impl_for_renamed_primitives {
($($type: ty : $name: ident)+) => {
($($ty: ty : $name: ident => $size: expr);+) => {
$(
impl BorshSchema for $type {
impl BorshSchema for $ty {
#[inline]
fn add_definitions_recursively(_definitions: &mut BTreeMap<Declaration, Definition>) {}
fn add_definitions_recursively(definitions: &mut BTreeMap<Declaration, Definition>) {
let definition = Definition::Primitive($size);
add_definition(Self::declaration(), definition, definitions);
}
#[inline]
fn declaration() -> Declaration { stringify!($name).into() }
}
Expand All @@ -274,31 +279,51 @@ macro_rules! impl_for_renamed_primitives {
}

macro_rules! impl_for_primitives {
($($type: ident)+) => {
impl_for_renamed_primitives!{$($type : $type)+}
($($ty: ident => $size: expr);+) => {
impl_for_renamed_primitives!{$($ty : $ty => $size);+}
};
}

impl_for_primitives!(bool f32 f64 i8 i16 i32 i64 i128 u8 u16 u32 u64 u128);
impl_for_renamed_primitives!(String: string);
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);
impl_for_primitives!(bool => 1; f32 => 4; f64 => 8; i8 => 1; i16 => 2; i32 => 4; i64 => 8; i128 => 16);
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);

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

impl_for_renamed_primitives!((): nil);
impl_for_renamed_primitives!((): nil => 0);

impl BorshSchema for String {
#[inline]
fn add_definitions_recursively(
_definitions: &mut BTreeMap<Declaration, Definition>,
) {}
#[inline]
fn declaration() -> Declaration {
"string".into()
}
}
impl BorshSchema for str {
#[inline]
fn add_definitions_recursively(
_definitions: &mut BTreeMap<Declaration, Definition>,
) {}
#[inline]
fn declaration() -> Declaration {
"string".into()
}
}

impl BorshSchema for core::ops::RangeFull {
#[inline]
Expand Down Expand Up @@ -369,6 +394,7 @@ where
};
add_definition(Self::declaration(), definition, definitions);
T::add_definitions_recursively(definitions);
<()>::add_definitions_recursively(definitions);
}

fn declaration() -> Declaration {
Expand All @@ -391,6 +417,7 @@ where
};
add_definition(Self::declaration(), definition, definitions);
T::add_definitions_recursively(definitions);
E::add_definitions_recursively(definitions);
}

fn declaration() -> Declaration {
Expand Down Expand Up @@ -610,7 +637,9 @@ mod tests {
("None".to_string(), "nil".to_string()),
("Some".to_string(), "u64".to_string()),
]
}
},
"u64" => Definition::Primitive(8),
"nil" => Definition::Primitive(0)
},
actual_defs
);
Expand Down Expand Up @@ -686,7 +715,9 @@ mod tests {
"nonzero_u16".to_string(),
"string".to_string()
]
}
},
"u64" => Definition::Primitive(8),
"nonzero_u16" => Definition::Primitive(2)
},
actual_defs
);
Expand Down Expand Up @@ -751,7 +782,8 @@ mod tests {
assert_eq!(
map! {
"BTreeMap<u64, string>" => Definition::Sequence { elements: "Tuple<u64, string>".to_string()} ,
"Tuple<u64, string>" => Definition::Tuple { elements: vec![ "u64".to_string(), "string".to_string()]}
"Tuple<u64, string>" => Definition::Tuple { elements: vec![ "u64".to_string(), "string".to_string()]},
"u64" => Definition::Primitive(8)
},
actual_defs
);
Expand All @@ -778,7 +810,9 @@ mod tests {
<[u64; 32]>::add_definitions_recursively(&mut actual_defs);
assert_eq!("Array<u64, 32>", actual_name);
assert_eq!(
map! {"Array<u64, 32>" => Definition::Array { length: 32, elements: "u64".to_string()}},
map! {"Array<u64, 32>" => Definition::Array { length: 32, elements: "u64".to_string()},
"u64" => Definition::Primitive(8)
},
actual_defs
);
}
Expand Down
6 changes: 3 additions & 3 deletions borsh/src/schema/container_ext.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::{BorshSchemaContainer, Declaration, Definition, Fields};

use max_size::is_zero_size;
pub use max_size::SchemaMaxSerializedSizeError;
pub use validate::SchemaContainerValidateError;
use max_size::{is_zero_size, ZeroSizeError};
pub use max_size::Error as SchemaMaxSerializedSizeError;
pub use validate::Error as SchemaContainerValidateError;

mod max_size;
mod validate;
Loading

0 comments on commit ff32f39

Please sign in to comment.