Skip to content

Commit cd84c59

Browse files
author
dj8yf0μl
committed
feat: implement BorshSchema for VecDeque and LinkedList
1 parent 96268f2 commit cd84c59

File tree

2 files changed

+53
-34
lines changed

2 files changed

+53
-34
lines changed

borsh/src/schema.rs

+25-17
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate as borsh; // For `#[derive(BorshSerialize, BorshDeserialize)]`.
1616
use crate::__private::maybestd::{
1717
borrow,
1818
boxed::Box,
19-
collections::{btree_map::Entry, BTreeMap, BTreeSet},
19+
collections::{btree_map::Entry, BTreeMap, BTreeSet, LinkedList, VecDeque},
2020
format,
2121
string::{String, ToString},
2222
vec,
@@ -635,25 +635,33 @@ where
635635
}
636636
}
637637

638-
impl<T> BorshSchema for Vec<T>
639-
where
640-
T: BorshSchema,
641-
{
642-
fn add_definitions_recursively(definitions: &mut BTreeMap<Declaration, Definition>) {
643-
let definition = Definition::Sequence {
644-
length_width: Definition::DEFAULT_LENGTH_WIDTH,
645-
length_range: Definition::DEFAULT_LENGTH_RANGE,
646-
elements: T::declaration(),
647-
};
648-
add_definition(Self::declaration(), definition, definitions);
649-
T::add_definitions_recursively(definitions);
650-
}
638+
macro_rules! impl_for_vec_like_collection {
639+
($type: ident) => {
640+
impl<T> BorshSchema for $type<T>
641+
where
642+
T: BorshSchema,
643+
{
644+
fn add_definitions_recursively(definitions: &mut BTreeMap<Declaration, Definition>) {
645+
let definition = Definition::Sequence {
646+
length_width: Definition::DEFAULT_LENGTH_WIDTH,
647+
length_range: Definition::DEFAULT_LENGTH_RANGE,
648+
elements: T::declaration(),
649+
};
650+
add_definition(Self::declaration(), definition, definitions);
651+
T::add_definitions_recursively(definitions);
652+
}
651653

652-
fn declaration() -> Declaration {
653-
format!(r#"Vec<{}>"#, T::declaration())
654-
}
654+
fn declaration() -> Declaration {
655+
format!(r#"{}<{}>"#, stringify!($type), T::declaration())
656+
}
657+
}
658+
};
655659
}
656660

661+
impl_for_vec_like_collection!(Vec);
662+
impl_for_vec_like_collection!(VecDeque);
663+
impl_for_vec_like_collection!(LinkedList);
664+
657665
impl<T> BorshSchema for [T]
658666
where
659667
T: BorshSchema,

borsh/tests/schema/test_vecs.rs

+28-17
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,35 @@
11
use crate::common_macro::schema_imports::*;
2+
use alloc::collections::{VecDeque, LinkedList};
23

3-
#[test]
4-
fn simple_vec() {
5-
let actual_name = Vec::<u64>::declaration();
6-
let mut actual_defs = schema_map!();
7-
Vec::<u64>::add_definitions_recursively(&mut actual_defs);
8-
assert_eq!("Vec<u64>", actual_name);
9-
assert_eq!(
10-
schema_map! {
11-
"Vec<u64>" => Definition::Sequence {
12-
length_width: Definition::DEFAULT_LENGTH_WIDTH,
13-
length_range: Definition::DEFAULT_LENGTH_RANGE,
14-
elements: "u64".to_string(),
15-
},
16-
"u64" => Definition::Primitive(8)
17-
},
18-
actual_defs
19-
);
4+
macro_rules! test_vec_like_collection_schema {
5+
[$test_name: ident, $type: ident] => [
6+
7+
#[test]
8+
fn $test_name() {
9+
let actual_name = $type::<u64>::declaration();
10+
let mut actual_defs = schema_map!();
11+
$type::<u64>::add_definitions_recursively(&mut actual_defs);
12+
13+
assert_eq!(format!("{}<u64>", stringify!($type)), actual_name);
14+
assert_eq!(
15+
schema_map! {
16+
actual_name => Definition::Sequence {
17+
length_width: Definition::DEFAULT_LENGTH_WIDTH,
18+
length_range: Definition::DEFAULT_LENGTH_RANGE,
19+
elements: "u64".to_string(),
20+
},
21+
"u64" => Definition::Primitive(8)
22+
},
23+
actual_defs
24+
);
25+
}
26+
];
2027
}
2128

29+
test_vec_like_collection_schema!(simple_vec, Vec);
30+
test_vec_like_collection_schema!(vec_deque, VecDeque);
31+
test_vec_like_collection_schema!(linked_list, LinkedList);
32+
2233
#[test]
2334
fn nested_vec() {
2435
let actual_name = Vec::<Vec<u64>>::declaration();

0 commit comments

Comments
 (0)