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

fix: relax schema_container_of target requirement with ?Sized to allow slices #245

Merged
merged 1 commit into from
Oct 13, 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
2 changes: 1 addition & 1 deletion borsh-derive/src/internals/attributes/field/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl Attributes {
}
pub(crate) fn collect_bounds(&self, ty: BoundType) -> Vec<WherePredicate> {
let predicates = self.get_bounds(ty);
predicates.unwrap_or(vec![])
predicates.unwrap_or_default()
}
}

Expand Down
2 changes: 1 addition & 1 deletion borsh/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl BorshSchemaContainer {
}

/// generate [BorshSchemaContainer] for type `T`
pub fn for_type<T: BorshSchema>() -> Self {
pub fn for_type<T: BorshSchema + ?Sized>() -> Self {
let mut definitions = Default::default();
T::add_definitions_recursively(&mut definitions);
Self::new(T::declaration(), definitions)
Expand Down
8 changes: 5 additions & 3 deletions borsh/src/schema_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ pub fn try_from_slice_with_schema<T: BorshDeserialize + BorshSchema>(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<T: BorshSerialize + BorshSchema>(value: &T) -> Result<Vec<u8>> {
pub fn try_to_vec_with_schema<T: BorshSerialize + BorshSchema + ?Sized>(
value: &T,
) -> Result<Vec<u8>> {
let schema = schema_container_of::<T>();
let mut res = crate::to_vec(&schema)?;
value.serialize(&mut res)?;
Expand All @@ -30,7 +32,7 @@ pub fn try_to_vec_with_schema<T: BorshSerialize + BorshSchema>(value: &T) -> Res
/// generate [BorshSchemaContainer] for type `T`
///
/// this is an alias of [BorshSchemaContainer::for_type]
pub fn schema_container_of<T: BorshSchema>() -> BorshSchemaContainer {
pub fn schema_container_of<T: BorshSchema + ?Sized>() -> BorshSchemaContainer {
BorshSchemaContainer::for_type::<T>()
}

Expand All @@ -44,7 +46,7 @@ pub fn schema_container_of<T: BorshSchema>() -> BorshSchemaContainer {
///
/// assert_eq!(Ok(8), borsh::max_serialized_size::<usize>());
/// ```
pub fn max_serialized_size<T: BorshSchema>(
pub fn max_serialized_size<T: BorshSchema + ?Sized>(
) -> core::result::Result<usize, SchemaMaxSerializedSizeError> {
let schema = BorshSchemaContainer::for_type::<T>();
schema.max_serialized_size()
Expand Down
68 changes: 68 additions & 0 deletions borsh/tests/test_schema_vec.rs
Original file line number Diff line number Diff line change
@@ -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<i64>".to_string(),
map! {
"Vec<i64>" => 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::<Vec<i64>>();

assert_eq!(
schema,
BorshSchemaContainer::new(
"Vec<i64>".to_string(),
map! {
"Vec<i64>" => Definition::Sequence {
length_width: Definition::DEFAULT_LENGTH_WIDTH,
length_range: Definition::DEFAULT_LENGTH_RANGE,
elements: "i64".to_string(),
},
"i64" => Definition::Primitive(8)

}
)
)
}
Loading