Skip to content

Commit

Permalink
feat: implement BorshSchema for VecDeque and LinkedList (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
dj8yfo authored Apr 29, 2024
1 parent 96268f2 commit 238ea42
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 34 deletions.
42 changes: 25 additions & 17 deletions borsh/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate as borsh; // For `#[derive(BorshSerialize, BorshDeserialize)]`.
use crate::__private::maybestd::{
borrow,
boxed::Box,
collections::{btree_map::Entry, BTreeMap, BTreeSet},
collections::{btree_map::Entry, BTreeMap, BTreeSet, LinkedList, VecDeque},
format,
string::{String, ToString},
vec,
Expand Down Expand Up @@ -635,25 +635,33 @@ where
}
}

impl<T> BorshSchema for Vec<T>
where
T: BorshSchema,
{
fn add_definitions_recursively(definitions: &mut BTreeMap<Declaration, Definition>) {
let definition = Definition::Sequence {
length_width: Definition::DEFAULT_LENGTH_WIDTH,
length_range: Definition::DEFAULT_LENGTH_RANGE,
elements: T::declaration(),
};
add_definition(Self::declaration(), definition, definitions);
T::add_definitions_recursively(definitions);
}
macro_rules! impl_for_vec_like_collection {
($type: ident) => {
impl<T> BorshSchema for $type<T>
where
T: BorshSchema,
{
fn add_definitions_recursively(definitions: &mut BTreeMap<Declaration, Definition>) {
let definition = Definition::Sequence {
length_width: Definition::DEFAULT_LENGTH_WIDTH,
length_range: Definition::DEFAULT_LENGTH_RANGE,
elements: T::declaration(),
};
add_definition(Self::declaration(), definition, definitions);
T::add_definitions_recursively(definitions);
}

fn declaration() -> Declaration {
format!(r#"Vec<{}>"#, T::declaration())
}
fn declaration() -> Declaration {
format!(r#"{}<{}>"#, stringify!($type), T::declaration())
}
}
};
}

impl_for_vec_like_collection!(Vec);
impl_for_vec_like_collection!(VecDeque);
impl_for_vec_like_collection!(LinkedList);

impl<T> BorshSchema for [T]
where
T: BorshSchema,
Expand Down
45 changes: 28 additions & 17 deletions borsh/tests/schema/test_vecs.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
use crate::common_macro::schema_imports::*;
use alloc::collections::{VecDeque, LinkedList};

#[test]
fn simple_vec() {
let actual_name = Vec::<u64>::declaration();
let mut actual_defs = schema_map!();
Vec::<u64>::add_definitions_recursively(&mut actual_defs);
assert_eq!("Vec<u64>", actual_name);
assert_eq!(
schema_map! {
"Vec<u64>" => Definition::Sequence {
length_width: Definition::DEFAULT_LENGTH_WIDTH,
length_range: Definition::DEFAULT_LENGTH_RANGE,
elements: "u64".to_string(),
},
"u64" => Definition::Primitive(8)
},
actual_defs
);
macro_rules! test_vec_like_collection_schema {
[$test_name: ident, $type: ident] => [

#[test]
fn $test_name() {
let actual_name = $type::<u64>::declaration();
let mut actual_defs = schema_map!();
$type::<u64>::add_definitions_recursively(&mut actual_defs);

assert_eq!(format!("{}<u64>", stringify!($type)), actual_name);
assert_eq!(
schema_map! {
actual_name => Definition::Sequence {
length_width: Definition::DEFAULT_LENGTH_WIDTH,
length_range: Definition::DEFAULT_LENGTH_RANGE,
elements: "u64".to_string(),
},
"u64" => Definition::Primitive(8)
},
actual_defs
);
}
];
}

test_vec_like_collection_schema!(simple_vec, Vec);
test_vec_like_collection_schema!(vec_deque, VecDeque);
test_vec_like_collection_schema!(linked_list, LinkedList);

#[test]
fn nested_vec() {
let actual_name = Vec::<Vec<u64>>::declaration();
Expand Down

0 comments on commit 238ea42

Please sign in to comment.