-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: relax
schema_container_of
target requirement with ?Sized
to…
… allow slices (#245)
- Loading branch information
Showing
3 changed files
with
74 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} | ||
) | ||
) | ||
} |