From 4389290e01f1154ad44ca22fddd011825c008956 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dj8yf0=CE=BCl?= Date: Thu, 12 Oct 2023 21:12:50 +0300 Subject: [PATCH] chore: relax `schema_container_of` target requirement with `?Sized` to allow slices --- .../src/internals/attributes/field/mod.rs | 2 +- borsh/src/schema.rs | 2 +- borsh/src/schema_helpers.rs | 8 ++- borsh/tests/test_schema_vec.rs | 68 +++++++++++++++++++ 4 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 borsh/tests/test_schema_vec.rs diff --git a/borsh-derive/src/internals/attributes/field/mod.rs b/borsh-derive/src/internals/attributes/field/mod.rs index 45489b25b..0ff2555c1 100644 --- a/borsh-derive/src/internals/attributes/field/mod.rs +++ b/borsh-derive/src/internals/attributes/field/mod.rs @@ -178,7 +178,7 @@ impl Attributes { } pub(crate) fn collect_bounds(&self, ty: BoundType) -> Vec { let predicates = self.get_bounds(ty); - predicates.unwrap_or(vec![]) + predicates.unwrap_or_default() } } diff --git a/borsh/src/schema.rs b/borsh/src/schema.rs index 9182f4ac7..0fce4652a 100644 --- a/borsh/src/schema.rs +++ b/borsh/src/schema.rs @@ -169,7 +169,7 @@ impl BorshSchemaContainer { } /// generate [BorshSchemaContainer] for type `T` - pub fn for_type() -> Self { + pub fn for_type() -> Self { let mut definitions = Default::default(); T::add_definitions_recursively(&mut definitions); Self::new(T::declaration(), definitions) diff --git a/borsh/src/schema_helpers.rs b/borsh/src/schema_helpers.rs index a59812424..422c36138 100644 --- a/borsh/src/schema_helpers.rs +++ b/borsh/src/schema_helpers.rs @@ -20,7 +20,9 @@ pub fn try_from_slice_with_schema(v: &[u8]) - /// Serialize object into a vector of bytes and prefix with the schema serialized as vector of /// bytes in Borsh format. -pub fn try_to_vec_with_schema(value: &T) -> Result> { +pub fn try_to_vec_with_schema( + value: &T, +) -> Result> { let schema = schema_container_of::(); let mut res = crate::to_vec(&schema)?; value.serialize(&mut res)?; @@ -30,7 +32,7 @@ pub fn try_to_vec_with_schema(value: &T) -> Res /// generate [BorshSchemaContainer] for type `T` /// /// this is an alias of [BorshSchemaContainer::for_type] -pub fn schema_container_of() -> BorshSchemaContainer { +pub fn schema_container_of() -> BorshSchemaContainer { BorshSchemaContainer::for_type::() } @@ -44,7 +46,7 @@ pub fn schema_container_of() -> BorshSchemaContainer { /// /// assert_eq!(Ok(8), borsh::max_serialized_size::()); /// ``` -pub fn max_serialized_size( +pub fn max_serialized_size( ) -> core::result::Result { let schema = BorshSchemaContainer::for_type::(); schema.max_serialized_size() diff --git a/borsh/tests/test_schema_vec.rs b/borsh/tests/test_schema_vec.rs new file mode 100644 index 000000000..7ff3932ca --- /dev/null +++ b/borsh/tests/test_schema_vec.rs @@ -0,0 +1,68 @@ +#![cfg_attr(not(feature = "std"), no_std)] +#![cfg(hash_collections)] +#![cfg(feature = "unstable__schema")] + +#[cfg(feature = "std")] +use std::collections::BTreeMap; + +#[cfg(not(feature = "std"))] +extern crate alloc; +#[cfg(not(feature = "std"))] +use alloc::{collections::BTreeMap, string::ToString}; + +use borsh::{schema::*, schema_container_of}; + +macro_rules! map( + () => { BTreeMap::new() }; + { $($key:expr => $value:expr),+ } => { + { + let mut m = BTreeMap::new(); + $( + m.insert($key.to_string(), $value); + )+ + m + } + }; +); + +#[test] +fn slice_schema_container() { + let schema = schema_container_of::<[i64]>(); + + assert_eq!( + schema, + BorshSchemaContainer::new( + "Vec".to_string(), + map! { + "Vec" => Definition::Sequence { + length_width: Definition::DEFAULT_LENGTH_WIDTH, + length_range: Definition::DEFAULT_LENGTH_RANGE, + elements: "i64".to_string(), + }, + "i64" => Definition::Primitive(8) + + } + ) + ) +} + +#[test] +fn vec_schema_container() { + let schema = schema_container_of::>(); + + assert_eq!( + schema, + BorshSchemaContainer::new( + "Vec".to_string(), + map! { + "Vec" => Definition::Sequence { + length_width: Definition::DEFAULT_LENGTH_WIDTH, + length_range: Definition::DEFAULT_LENGTH_RANGE, + elements: "i64".to_string(), + }, + "i64" => Definition::Primitive(8) + + } + ) + ) +}