diff --git a/near-sdk/src/store/tree_map/mod.rs b/near-sdk/src/store/tree_map/mod.rs index dfa85209f..2cfc801ff 100644 --- a/near-sdk/src/store/tree_map/mod.rs +++ b/near-sdk/src/store/tree_map/mod.rs @@ -30,18 +30,26 @@ fn expect(val: Option) -> T { /// - `min`/`max`: O(log(N)) /// - `above`/`below`: O(log(N)) /// - `range` of K elements: O(Klog(N)) -#[derive(BorshDeserialize, BorshSerialize)] +#[near(inside_nearsdk)] pub struct TreeMap where K: BorshSerialize + Ord, V: BorshSerialize, H: ToKey, { - // ser/de is independent of `K`, `V`, `H` ser/de, `BorshSerialize`/`BorshDeserialize` bounds removed - #[borsh(bound(serialize = "", deserialize = ""))] + // ser/de is independent of `K`, `V`, `H` ser/de, `BorshSerialize`/`BorshDeserialize`/`BorshSchema` bounds removed + #[cfg_attr(not(feature = "abi"), borsh(bound(serialize = "", deserialize = "")))] + #[cfg_attr( + feature = "abi", + borsh(bound(serialize = "", deserialize = ""), schema(params = "")) + )] values: LookupMap, - // ser/de is independent of `K` ser/de, `BorshSerialize`/`BorshDeserialize` bounds removed - #[borsh(bound(serialize = "", deserialize = ""))] + // ser/de is independent of `K` ser/de, `BorshSerialize`/`BorshDeserialize`/`BorshSchema` bounds removed + #[cfg_attr(not(feature = "abi"), borsh(bound(serialize = "", deserialize = "")))] + #[cfg_attr( + feature = "abi", + borsh(bound(serialize = "", deserialize = ""), schema(params = "")) + )] tree: Tree, } @@ -2129,4 +2137,22 @@ mod tests { swap_set(&mut map); assert_eq!(map.tree.root, Some(FreeListIndex(0))); } + + #[cfg(feature = "abi")] + #[test] + fn test_borsh_schema() { + #[derive( + borsh::BorshSerialize, borsh::BorshDeserialize, PartialEq, Eq, PartialOrd, Ord, + )] + struct NoSchemaStruct; + + assert_eq!( + "TreeMap".to_string(), + as borsh::BorshSchema>::declaration() + ); + let mut defs = Default::default(); + as borsh::BorshSchema>::add_definitions_recursively(&mut defs); + + insta::assert_snapshot!(format!("{:#?}", defs)); + } } diff --git a/near-sdk/src/store/tree_map/snapshots/near_sdk__store__tree_map__tests__borsh_schema.snap b/near-sdk/src/store/tree_map/snapshots/near_sdk__store__tree_map__tests__borsh_schema.snap new file mode 100644 index 000000000..b56f11d49 --- /dev/null +++ b/near-sdk/src/store/tree_map/snapshots/near_sdk__store__tree_map__tests__borsh_schema.snap @@ -0,0 +1,122 @@ +--- +source: near-sdk/src/store/tree_map/mod.rs +expression: "format!(\"{:#?}\", defs)" +--- +{ + "()": Primitive( + 0, + ), + "FreeList": Struct { + fields: NamedFields( + [ + ( + "first_free", + "Option", + ), + ( + "occupied_count", + "u32", + ), + ( + "elements", + "Vector", + ), + ], + ), + }, + "FreeListIndex": Struct { + fields: UnnamedFields( + [ + "u32", + ], + ), + }, + "IndexMap": Struct { + fields: NamedFields( + [ + ( + "prefix", + "Vec", + ), + ], + ), + }, + "LookupMap": Struct { + fields: NamedFields( + [ + ( + "prefix", + "Vec", + ), + ], + ), + }, + "Option": Enum { + tag_width: 1, + variants: [ + ( + 0, + "None", + "()", + ), + ( + 1, + "Some", + "FreeListIndex", + ), + ], + }, + "Tree": Struct { + fields: NamedFields( + [ + ( + "root", + "Option", + ), + ( + "nodes", + "FreeList", + ), + ], + ), + }, + "TreeMap": Struct { + fields: NamedFields( + [ + ( + "values", + "LookupMap", + ), + ( + "tree", + "Tree", + ), + ], + ), + }, + "Vec": Sequence { + length_width: 4, + length_range: 0..=4294967295, + elements: "u8", + }, + "Vector": Struct { + fields: NamedFields( + [ + ( + "len", + "u32", + ), + ( + "values", + "IndexMap", + ), + ], + ), + }, + "u32": Primitive( + 4, + ), + "u8": Primitive( + 1, + ), +} diff --git a/near-sdk/src/store/unordered_set/mod.rs b/near-sdk/src/store/unordered_set/mod.rs index e5a32b72f..221f210dd 100644 --- a/near-sdk/src/store/unordered_set/mod.rs +++ b/near-sdk/src/store/unordered_set/mod.rs @@ -10,6 +10,7 @@ use crate::store::free_list::FreeListIndex; use crate::store::key::{Sha256, ToKey}; use crate::{env, IntoStorageKey}; use borsh::{BorshDeserialize, BorshSerialize}; +use near_sdk_macros::near; use std::borrow::Borrow; use std::fmt; @@ -87,7 +88,7 @@ use std::fmt; /// /// [`with_hasher`]: Self::with_hasher /// [`LookupSet`]: crate::store::LookupSet -#[derive(BorshDeserialize, BorshSerialize)] +#[near(inside_nearsdk)] #[deprecated( since = "5.0.0", note = "Suboptimal iteration performance. See performance considerations doc for details." @@ -97,11 +98,19 @@ where T: BorshSerialize + Ord, H: ToKey, { - // ser/de is independent of `T` ser/de, `BorshSerialize`/`BorshDeserialize` bounds removed - #[borsh(bound(serialize = "", deserialize = ""))] + // ser/de is independent of `T` ser/de, `BorshSerialize`/`BorshDeserialize`/`BorshSchema` bounds removed + #[cfg_attr(not(feature = "abi"), borsh(bound(serialize = "", deserialize = "")))] + #[cfg_attr( + feature = "abi", + borsh(bound(serialize = "", deserialize = ""), schema(params = "")) + )] elements: FreeList, - // ser/de is independent of `T`, `H` ser/de, `BorshSerialize`/`BorshDeserialize` bounds removed - #[borsh(bound(serialize = "", deserialize = ""))] + // ser/de is independent of `T`, `H` ser/de, `BorshSerialize`/`BorshDeserialize`/`BorshSchema` bounds removed + #[cfg_attr(not(feature = "abi"), borsh(bound(serialize = "", deserialize = "")))] + #[cfg_attr( + feature = "abi", + borsh(bound(serialize = "", deserialize = ""), schema(params = "")) + )] index: LookupMap, } @@ -949,4 +958,24 @@ mod tests { // Check the last removed value. assert_eq!(set.elements.get(FreeListIndex(6)), None); } + + #[cfg(feature = "abi")] + #[test] + fn test_borsh_schema() { + #[derive( + borsh::BorshSerialize, borsh::BorshDeserialize, PartialEq, Eq, PartialOrd, Ord, + )] + struct NoSchemaStruct; + + assert_eq!( + "UnorderedSet".to_string(), + as borsh::BorshSchema>::declaration() + ); + let mut defs = Default::default(); + as borsh::BorshSchema>::add_definitions_recursively( + &mut defs, + ); + + insta::assert_snapshot!(format!("{:#?}", defs)); + } } diff --git a/near-sdk/src/store/unordered_set/snapshots/near_sdk__store__unordered_set__tests__borsh_schema.snap b/near-sdk/src/store/unordered_set/snapshots/near_sdk__store__unordered_set__tests__borsh_schema.snap new file mode 100644 index 000000000..6aafc6c6c --- /dev/null +++ b/near-sdk/src/store/unordered_set/snapshots/near_sdk__store__unordered_set__tests__borsh_schema.snap @@ -0,0 +1,108 @@ +--- +source: near-sdk/src/store/unordered_set/mod.rs +expression: "format!(\"{:#?}\", defs)" +--- +{ + "()": Primitive( + 0, + ), + "FreeList": Struct { + fields: NamedFields( + [ + ( + "first_free", + "Option", + ), + ( + "occupied_count", + "u32", + ), + ( + "elements", + "Vector", + ), + ], + ), + }, + "FreeListIndex": Struct { + fields: UnnamedFields( + [ + "u32", + ], + ), + }, + "IndexMap": Struct { + fields: NamedFields( + [ + ( + "prefix", + "Vec", + ), + ], + ), + }, + "LookupMap": Struct { + fields: NamedFields( + [ + ( + "prefix", + "Vec", + ), + ], + ), + }, + "Option": Enum { + tag_width: 1, + variants: [ + ( + 0, + "None", + "()", + ), + ( + 1, + "Some", + "FreeListIndex", + ), + ], + }, + "UnorderedSet": Struct { + fields: NamedFields( + [ + ( + "elements", + "FreeList", + ), + ( + "index", + "LookupMap", + ), + ], + ), + }, + "Vec": Sequence { + length_width: 4, + length_range: 0..=4294967295, + elements: "u8", + }, + "Vector": Struct { + fields: NamedFields( + [ + ( + "len", + "u32", + ), + ( + "values", + "IndexMap", + ), + ], + ), + }, + "u32": Primitive( + 4, + ), + "u8": Primitive( + 1, + ), +}