diff --git a/borsh-derive/src/internals/schema/enums/mod.rs b/borsh-derive/src/internals/schema/enums/mod.rs index b3fc1930a..9dfd9c764 100644 --- a/borsh-derive/src/internals/schema/enums/mod.rs +++ b/borsh-derive/src/internals/schema/enums/mod.rs @@ -55,7 +55,7 @@ pub fn process(input: &ItemEnum, cratename: Ident) -> syn::Result #add_recursive_defs let variants = #cratename::__private::maybestd::vec![#(#variants_defs),*]; let definition = #cratename::schema::Definition::Enum{variants}; - Self::add_definition(Self::declaration(), definition, definitions); + #cratename::schema::add_definition(Self::declaration(), definition, definitions); } }; diff --git a/borsh-derive/src/internals/schema/enums/snapshots/complex_enum.snap b/borsh-derive/src/internals/schema/enums/snapshots/complex_enum.snap index c63fb5ade..de7f00624 100644 --- a/borsh-derive/src/internals/schema/enums/snapshots/complex_enum.snap +++ b/borsh-derive/src/internals/schema/enums/snapshots/complex_enum.snap @@ -39,7 +39,7 @@ impl borsh::BorshSchema for A { let definition = borsh::schema::Definition::Enum { variants, }; - Self::add_definition(Self::declaration(), definition, definitions); + borsh::schema::add_definition(Self::declaration(), definition, definitions); } } diff --git a/borsh-derive/src/internals/schema/enums/snapshots/complex_enum_generics.snap b/borsh-derive/src/internals/schema/enums/snapshots/complex_enum_generics.snap index 5533779f1..32febbc0f 100644 --- a/borsh-derive/src/internals/schema/enums/snapshots/complex_enum_generics.snap +++ b/borsh-derive/src/internals/schema/enums/snapshots/complex_enum_generics.snap @@ -46,7 +46,7 @@ where let definition = borsh::schema::Definition::Enum { variants, }; - Self::add_definition(Self::declaration(), definition, definitions); + borsh::schema::add_definition(Self::declaration(), definition, definitions); } } diff --git a/borsh-derive/src/internals/schema/enums/snapshots/complex_enum_generics_borsh_skip_named_field.snap b/borsh-derive/src/internals/schema/enums/snapshots/complex_enum_generics_borsh_skip_named_field.snap index b45eac756..cdae83895 100644 --- a/borsh-derive/src/internals/schema/enums/snapshots/complex_enum_generics_borsh_skip_named_field.snap +++ b/borsh-derive/src/internals/schema/enums/snapshots/complex_enum_generics_borsh_skip_named_field.snap @@ -49,7 +49,7 @@ where let definition = borsh::schema::Definition::Enum { variants, }; - Self::add_definition(Self::declaration(), definition, definitions); + borsh::schema::add_definition(Self::declaration(), definition, definitions); } } diff --git a/borsh-derive/src/internals/schema/enums/snapshots/complex_enum_generics_borsh_skip_tuple_field.snap b/borsh-derive/src/internals/schema/enums/snapshots/complex_enum_generics_borsh_skip_tuple_field.snap index 7abec9070..f31a29129 100644 --- a/borsh-derive/src/internals/schema/enums/snapshots/complex_enum_generics_borsh_skip_tuple_field.snap +++ b/borsh-derive/src/internals/schema/enums/snapshots/complex_enum_generics_borsh_skip_tuple_field.snap @@ -47,7 +47,7 @@ where let definition = borsh::schema::Definition::Enum { variants, }; - Self::add_definition(Self::declaration(), definition, definitions); + borsh::schema::add_definition(Self::declaration(), definition, definitions); } } diff --git a/borsh-derive/src/internals/schema/enums/snapshots/filter_foreign_attrs.snap b/borsh-derive/src/internals/schema/enums/snapshots/filter_foreign_attrs.snap index 711046774..d6ee01965 100644 --- a/borsh-derive/src/internals/schema/enums/snapshots/filter_foreign_attrs.snap +++ b/borsh-derive/src/internals/schema/enums/snapshots/filter_foreign_attrs.snap @@ -34,7 +34,7 @@ impl borsh::BorshSchema for A { let definition = borsh::schema::Definition::Enum { variants, }; - Self::add_definition(Self::declaration(), definition, definitions); + borsh::schema::add_definition(Self::declaration(), definition, definitions); } } diff --git a/borsh-derive/src/internals/schema/enums/snapshots/generic_associated_type.snap b/borsh-derive/src/internals/schema/enums/snapshots/generic_associated_type.snap index 1bbda8a11..390a37f6d 100644 --- a/borsh-derive/src/internals/schema/enums/snapshots/generic_associated_type.snap +++ b/borsh-derive/src/internals/schema/enums/snapshots/generic_associated_type.snap @@ -60,7 +60,7 @@ where let definition = borsh::schema::Definition::Enum { variants, }; - Self::add_definition(Self::declaration(), definition, definitions); + borsh::schema::add_definition(Self::declaration(), definition, definitions); } } diff --git a/borsh-derive/src/internals/schema/enums/snapshots/generic_associated_type_param_override.snap b/borsh-derive/src/internals/schema/enums/snapshots/generic_associated_type_param_override.snap index c3bc0d3cb..36be2a688 100644 --- a/borsh-derive/src/internals/schema/enums/snapshots/generic_associated_type_param_override.snap +++ b/borsh-derive/src/internals/schema/enums/snapshots/generic_associated_type_param_override.snap @@ -61,7 +61,7 @@ where let definition = borsh::schema::Definition::Enum { variants, }; - Self::add_definition(Self::declaration(), definition, definitions); + borsh::schema::add_definition(Self::declaration(), definition, definitions); } } diff --git a/borsh-derive/src/internals/schema/enums/snapshots/recursive_enum.snap b/borsh-derive/src/internals/schema/enums/snapshots/recursive_enum.snap index 9aa4eb153..69d095c8c 100644 --- a/borsh-derive/src/internals/schema/enums/snapshots/recursive_enum.snap +++ b/borsh-derive/src/internals/schema/enums/snapshots/recursive_enum.snap @@ -41,7 +41,7 @@ where let definition = borsh::schema::Definition::Enum { variants, }; - Self::add_definition(Self::declaration(), definition, definitions); + borsh::schema::add_definition(Self::declaration(), definition, definitions); } } diff --git a/borsh-derive/src/internals/schema/enums/snapshots/simple_enum.snap b/borsh-derive/src/internals/schema/enums/snapshots/simple_enum.snap index afd1a9e24..b3d3928bb 100644 --- a/borsh-derive/src/internals/schema/enums/snapshots/simple_enum.snap +++ b/borsh-derive/src/internals/schema/enums/snapshots/simple_enum.snap @@ -27,7 +27,7 @@ impl borsh::BorshSchema for A { let definition = borsh::schema::Definition::Enum { variants, }; - Self::add_definition(Self::declaration(), definition, definitions); + borsh::schema::add_definition(Self::declaration(), definition, definitions); } } diff --git a/borsh-derive/src/internals/schema/enums/snapshots/single_field_enum.snap b/borsh-derive/src/internals/schema/enums/snapshots/single_field_enum.snap index 06ecf2013..9f9a50776 100644 --- a/borsh-derive/src/internals/schema/enums/snapshots/single_field_enum.snap +++ b/borsh-derive/src/internals/schema/enums/snapshots/single_field_enum.snap @@ -22,7 +22,7 @@ impl borsh::BorshSchema for A { let definition = borsh::schema::Definition::Enum { variants, }; - Self::add_definition(Self::declaration(), definition, definitions); + borsh::schema::add_definition(Self::declaration(), definition, definitions); } } diff --git a/borsh-derive/src/internals/schema/enums/snapshots/trailing_comma_generics.snap b/borsh-derive/src/internals/schema/enums/snapshots/trailing_comma_generics.snap index 2636746a2..5f529ff07 100644 --- a/borsh-derive/src/internals/schema/enums/snapshots/trailing_comma_generics.snap +++ b/borsh-derive/src/internals/schema/enums/snapshots/trailing_comma_generics.snap @@ -44,7 +44,7 @@ where let definition = borsh::schema::Definition::Enum { variants, }; - Self::add_definition(Self::declaration(), definition, definitions); + borsh::schema::add_definition(Self::declaration(), definition, definitions); } } diff --git a/borsh-derive/src/internals/schema/enums/snapshots/with_funcs_attr.snap b/borsh-derive/src/internals/schema/enums/snapshots/with_funcs_attr.snap index 8b0c41e23..49c1d6ad1 100644 --- a/borsh-derive/src/internals/schema/enums/snapshots/with_funcs_attr.snap +++ b/borsh-derive/src/internals/schema/enums/snapshots/with_funcs_attr.snap @@ -45,7 +45,7 @@ where let definition = borsh::schema::Definition::Enum { variants, }; - Self::add_definition(Self::declaration(), definition, definitions); + borsh::schema::add_definition(Self::declaration(), definition, definitions); } } diff --git a/borsh-derive/src/internals/schema/structs/mod.rs b/borsh-derive/src/internals/schema/structs/mod.rs index b467f03ea..793d3d8f0 100644 --- a/borsh-derive/src/internals/schema/structs/mod.rs +++ b/borsh-derive/src/internals/schema/structs/mod.rs @@ -64,7 +64,7 @@ pub fn process(input: &ItemStruct, cratename: Ident) -> syn::Result::add_definitions_recursively( definitions, diff --git a/borsh-derive/src/internals/schema/structs/snapshots/generic_associated_type_param_override.snap b/borsh-derive/src/internals/schema/structs/snapshots/generic_associated_type_param_override.snap index c42b7677f..d305dac6a 100644 --- a/borsh-derive/src/internals/schema/structs/snapshots/generic_associated_type_param_override.snap +++ b/borsh-derive/src/internals/schema/structs/snapshots/generic_associated_type_param_override.snap @@ -31,7 +31,7 @@ where fields, }; let no_recursion_flag = definitions.get(&Self::declaration()).is_none(); - Self::add_definition(Self::declaration(), definition, definitions); + borsh::schema::add_definition(Self::declaration(), definition, definitions); if no_recursion_flag { <::Associated as borsh::BorshSchema>::add_definitions_recursively( definitions, diff --git a/borsh-derive/src/internals/schema/structs/snapshots/generic_associated_type_param_override2.snap b/borsh-derive/src/internals/schema/structs/snapshots/generic_associated_type_param_override2.snap index b09937702..8bef3a34f 100644 --- a/borsh-derive/src/internals/schema/structs/snapshots/generic_associated_type_param_override2.snap +++ b/borsh-derive/src/internals/schema/structs/snapshots/generic_associated_type_param_override2.snap @@ -33,7 +33,7 @@ where fields, }; let no_recursion_flag = definitions.get(&Self::declaration()).is_none(); - Self::add_definition(Self::declaration(), definition, definitions); + borsh::schema::add_definition(Self::declaration(), definition, definitions); if no_recursion_flag { <( ::Associated, diff --git a/borsh-derive/src/internals/schema/structs/snapshots/generic_named_fields_struct_borsh_skip.snap b/borsh-derive/src/internals/schema/structs/snapshots/generic_named_fields_struct_borsh_skip.snap index cd826d92d..5355fac5e 100644 --- a/borsh-derive/src/internals/schema/structs/snapshots/generic_named_fields_struct_borsh_skip.snap +++ b/borsh-derive/src/internals/schema/structs/snapshots/generic_named_fields_struct_borsh_skip.snap @@ -25,7 +25,7 @@ where fields, }; let no_recursion_flag = definitions.get(&Self::declaration()).is_none(); - Self::add_definition(Self::declaration(), definition, definitions); + borsh::schema::add_definition(Self::declaration(), definition, definitions); if no_recursion_flag { ::add_definitions_recursively(definitions); } diff --git a/borsh-derive/src/internals/schema/structs/snapshots/generic_tuple_struct_borsh_skip1.snap b/borsh-derive/src/internals/schema/structs/snapshots/generic_tuple_struct_borsh_skip1.snap index 654f77ed3..bc84e146f 100644 --- a/borsh-derive/src/internals/schema/structs/snapshots/generic_tuple_struct_borsh_skip1.snap +++ b/borsh-derive/src/internals/schema/structs/snapshots/generic_tuple_struct_borsh_skip1.snap @@ -23,7 +23,7 @@ where fields, }; let no_recursion_flag = definitions.get(&Self::declaration()).is_none(); - Self::add_definition(Self::declaration(), definition, definitions); + borsh::schema::add_definition(Self::declaration(), definition, definitions); if no_recursion_flag { ::add_definitions_recursively(definitions); } diff --git a/borsh-derive/src/internals/schema/structs/snapshots/generic_tuple_struct_borsh_skip2.snap b/borsh-derive/src/internals/schema/structs/snapshots/generic_tuple_struct_borsh_skip2.snap index 03f2b45c0..d8418b242 100644 --- a/borsh-derive/src/internals/schema/structs/snapshots/generic_tuple_struct_borsh_skip2.snap +++ b/borsh-derive/src/internals/schema/structs/snapshots/generic_tuple_struct_borsh_skip2.snap @@ -28,7 +28,7 @@ where fields, }; let no_recursion_flag = definitions.get(&Self::declaration()).is_none(); - Self::add_definition(Self::declaration(), definition, definitions); + borsh::schema::add_definition(Self::declaration(), definition, definitions); if no_recursion_flag { ::add_definitions_recursively(definitions); ::add_definitions_recursively(definitions); diff --git a/borsh-derive/src/internals/schema/structs/snapshots/generic_tuple_struct_borsh_skip4.snap b/borsh-derive/src/internals/schema/structs/snapshots/generic_tuple_struct_borsh_skip4.snap index 8fae50a06..db065e7b3 100644 --- a/borsh-derive/src/internals/schema/structs/snapshots/generic_tuple_struct_borsh_skip4.snap +++ b/borsh-derive/src/internals/schema/structs/snapshots/generic_tuple_struct_borsh_skip4.snap @@ -22,7 +22,7 @@ impl borsh::BorshSchema for ASalad { fields, }; let no_recursion_flag = definitions.get(&Self::declaration()).is_none(); - Self::add_definition(Self::declaration(), definition, definitions); + borsh::schema::add_definition(Self::declaration(), definition, definitions); if no_recursion_flag { ::add_definitions_recursively(definitions); ::add_definitions_recursively(definitions); diff --git a/borsh-derive/src/internals/schema/structs/snapshots/recursive_struct.snap b/borsh-derive/src/internals/schema/structs/snapshots/recursive_struct.snap index d59ade89d..4c00ae233 100644 --- a/borsh-derive/src/internals/schema/structs/snapshots/recursive_struct.snap +++ b/borsh-derive/src/internals/schema/structs/snapshots/recursive_struct.snap @@ -23,7 +23,7 @@ impl borsh::BorshSchema for CRecC { fields, }; let no_recursion_flag = definitions.get(&Self::declaration()).is_none(); - Self::add_definition(Self::declaration(), definition, definitions); + borsh::schema::add_definition(Self::declaration(), definition, definitions); if no_recursion_flag { ::add_definitions_recursively(definitions); ::add_definitions_recursively(definitions); ::add_definitions_recursively(definitions); diff --git a/borsh-derive/src/internals/schema/structs/snapshots/trailing_comma_generics.snap b/borsh-derive/src/internals/schema/structs/snapshots/trailing_comma_generics.snap index 496d19831..f00f945fd 100644 --- a/borsh-derive/src/internals/schema/structs/snapshots/trailing_comma_generics.snap +++ b/borsh-derive/src/internals/schema/structs/snapshots/trailing_comma_generics.snap @@ -31,7 +31,7 @@ where fields, }; let no_recursion_flag = definitions.get(&Self::declaration()).is_none(); - Self::add_definition(Self::declaration(), definition, definitions); + borsh::schema::add_definition(Self::declaration(), definition, definitions); if no_recursion_flag { ::add_definitions_recursively(definitions); ::add_definitions_recursively(definitions); diff --git a/borsh-derive/src/internals/schema/structs/snapshots/tuple_struct_params.snap b/borsh-derive/src/internals/schema/structs/snapshots/tuple_struct_params.snap index 9a7b0466d..2d275bc21 100644 --- a/borsh-derive/src/internals/schema/structs/snapshots/tuple_struct_params.snap +++ b/borsh-derive/src/internals/schema/structs/snapshots/tuple_struct_params.snap @@ -29,7 +29,7 @@ where fields, }; let no_recursion_flag = definitions.get(&Self::declaration()).is_none(); - Self::add_definition(Self::declaration(), definition, definitions); + borsh::schema::add_definition(Self::declaration(), definition, definitions); if no_recursion_flag { ::add_definitions_recursively(definitions); ::add_definitions_recursively(definitions); diff --git a/borsh-derive/src/internals/schema/structs/snapshots/tuple_struct_partial_skip.snap b/borsh-derive/src/internals/schema/structs/snapshots/tuple_struct_partial_skip.snap index d1a9b867a..c141cb87e 100644 --- a/borsh-derive/src/internals/schema/structs/snapshots/tuple_struct_partial_skip.snap +++ b/borsh-derive/src/internals/schema/structs/snapshots/tuple_struct_partial_skip.snap @@ -21,7 +21,7 @@ impl borsh::BorshSchema for A { fields, }; let no_recursion_flag = definitions.get(&Self::declaration()).is_none(); - Self::add_definition(Self::declaration(), definition, definitions); + borsh::schema::add_definition(Self::declaration(), definition, definitions); if no_recursion_flag { ::add_definitions_recursively(definitions); } diff --git a/borsh-derive/src/internals/schema/structs/snapshots/tuple_struct_whole_skip.snap b/borsh-derive/src/internals/schema/structs/snapshots/tuple_struct_whole_skip.snap index 302705d3c..62b9ce5a1 100644 --- a/borsh-derive/src/internals/schema/structs/snapshots/tuple_struct_whole_skip.snap +++ b/borsh-derive/src/internals/schema/structs/snapshots/tuple_struct_whole_skip.snap @@ -17,7 +17,7 @@ impl borsh::BorshSchema for A { fields, }; let no_recursion_flag = definitions.get(&Self::declaration()).is_none(); - Self::add_definition(Self::declaration(), definition, definitions); + borsh::schema::add_definition(Self::declaration(), definition, definitions); if no_recursion_flag {} } } diff --git a/borsh-derive/src/internals/schema/structs/snapshots/unit_struct.snap b/borsh-derive/src/internals/schema/structs/snapshots/unit_struct.snap index 302705d3c..62b9ce5a1 100644 --- a/borsh-derive/src/internals/schema/structs/snapshots/unit_struct.snap +++ b/borsh-derive/src/internals/schema/structs/snapshots/unit_struct.snap @@ -17,7 +17,7 @@ impl borsh::BorshSchema for A { fields, }; let no_recursion_flag = definitions.get(&Self::declaration()).is_none(); - Self::add_definition(Self::declaration(), definition, definitions); + borsh::schema::add_definition(Self::declaration(), definition, definitions); if no_recursion_flag {} } } diff --git a/borsh-derive/src/internals/schema/structs/snapshots/with_funcs_attr.snap b/borsh-derive/src/internals/schema/structs/snapshots/with_funcs_attr.snap index d737c5510..ea1b965d8 100644 --- a/borsh-derive/src/internals/schema/structs/snapshots/with_funcs_attr.snap +++ b/borsh-derive/src/internals/schema/structs/snapshots/with_funcs_attr.snap @@ -29,7 +29,7 @@ where fields, }; let no_recursion_flag = definitions.get(&Self::declaration()).is_none(); - Self::add_definition(Self::declaration(), definition, definitions); + borsh::schema::add_definition(Self::declaration(), definition, definitions); if no_recursion_flag { third_party_impl::add_definitions_recursively::(definitions); ::add_definitions_recursively(definitions); diff --git a/borsh-derive/src/internals/schema/structs/snapshots/wrapper_struct.snap b/borsh-derive/src/internals/schema/structs/snapshots/wrapper_struct.snap index 9065efabd..521306cbb 100644 --- a/borsh-derive/src/internals/schema/structs/snapshots/wrapper_struct.snap +++ b/borsh-derive/src/internals/schema/structs/snapshots/wrapper_struct.snap @@ -23,7 +23,7 @@ where fields, }; let no_recursion_flag = definitions.get(&Self::declaration()).is_none(); - Self::add_definition(Self::declaration(), definition, definitions); + borsh::schema::add_definition(Self::declaration(), definition, definitions); if no_recursion_flag { ::add_definitions_recursively(definitions); } diff --git a/borsh/src/generate_schema_schema.rs b/borsh/src/generate_schema_schema.rs index 6046f54dc..5b7373d90 100644 --- a/borsh/src/generate_schema_schema.rs +++ b/borsh/src/generate_schema_schema.rs @@ -1,13 +1,13 @@ //! Generate `BorshSchemaCointainer` for `BorshSchemaContainer` and save it into a file. #![cfg_attr(not(feature = "std"), no_std)] -use borsh::schema::BorshSchema; -use borsh::BorshSerialize; +use borsh::{schema_container_of, BorshSerialize}; use std::fs::File; use std::io::Write; fn main() { - let container = borsh::schema::BorshSchemaContainer::schema_container(); + let container = schema_container_of::(); + println!("{:#?}", container); let data = container .try_to_vec() diff --git a/borsh/src/lib.rs b/borsh/src/lib.rs index 2663b7b10..3a38e61ea 100644 --- a/borsh/src/lib.rs +++ b/borsh/src/lib.rs @@ -21,7 +21,7 @@ [BorshDeserialize](crate::de::BorshDeserialize) traits. * **schema** - Gates [BorshSchema](crate::schema::BorshSchema) trait and its derive macro. - Gates [schema](crate::schema) and [schema_helpers](crate::schema_helpers) modules. + Gates [schema](crate::schema) module. This feature requires **derive** to be enabled too. * **rc** - Gates implementation of [BorshSerialize](crate::ser::BorshSerialize) and [BorshDeserialize](crate::de::BorshDeserialize) @@ -77,7 +77,7 @@ pub mod de; pub mod schema; /// Module is available if borsh is built with `features = ["derive", "schema"]`. #[cfg(derive_schema)] -pub mod schema_helpers; +pub(crate) mod schema_helpers; pub mod ser; pub use de::BorshDeserialize; @@ -85,7 +85,7 @@ pub use de::{from_reader, from_slice}; #[cfg(derive_schema)] pub use schema::BorshSchema; #[cfg(derive_schema)] -pub use schema_helpers::{try_from_slice_with_schema, try_to_vec_with_schema}; +pub use schema_helpers::{schema_container_of, try_from_slice_with_schema, try_to_vec_with_schema}; pub use ser::helpers::{to_vec, to_writer}; pub use ser::BorshSerialize; pub mod error; diff --git a/borsh/src/schema.rs b/borsh/src/schema.rs index 3d58b6102..41af449b3 100644 --- a/borsh/src/schema.rs +++ b/borsh/src/schema.rs @@ -132,6 +132,23 @@ where } } +/// Helper method to add a single type definition to the map. +pub fn add_definition( + declaration: Declaration, + definition: Definition, + definitions: &mut BTreeMap, +) { + match definitions.entry(declaration) { + Entry::Occupied(occ) => { + let existing_def = occ.get(); + assert_eq!(existing_def, &definition, "Redefining type schema for the same type name. Types with the same names are not supported."); + } + Entry::Vacant(vac) => { + vac.insert(definition); + } + } +} + /// The declaration and the definition of the type that can be used to (de)serialize Borsh without /// the Rust type that produced it. pub trait BorshSchema { @@ -139,33 +156,8 @@ pub trait BorshSchema { /// this is an empty map. Type definition explains how to serialize/deserialize a type. fn add_definitions_recursively(definitions: &mut BTreeMap); - /// Helper method to add a single type definition to the map. - fn add_definition( - declaration: Declaration, - definition: Definition, - definitions: &mut BTreeMap, - ) { - match definitions.entry(declaration) { - Entry::Occupied(occ) => { - let existing_def = occ.get(); - assert_eq!(existing_def, &definition, "Redefining type schema for the same type name. Types with the same names are not supported."); - } - Entry::Vacant(vac) => { - vac.insert(definition); - } - } - } /// Get the name of the type without brackets. fn declaration() -> Declaration; - - fn schema_container() -> BorshSchemaContainer { - let mut definitions = BTreeMap::new(); - Self::add_definitions_recursively(&mut definitions); - BorshSchemaContainer { - declaration: Self::declaration(), - definitions, - } - } } impl BorshSchema for BorshSchemaContainer @@ -188,7 +180,7 @@ where ), ]))); let definition = Definition::Struct { fields }; - Self::add_definition( + add_definition( ::declaration(), definition, definitions, @@ -254,7 +246,7 @@ where length: N as u32, elements: T::declaration(), }; - Self::add_definition(Self::declaration(), definition, definitions); + add_definition(Self::declaration(), definition, definitions); T::add_definitions_recursively(definitions); } fn declaration() -> Declaration { @@ -273,7 +265,7 @@ where ("Some".to_string(), T::declaration()), ], }; - Self::add_definition(Self::declaration(), definition, definitions); + add_definition(Self::declaration(), definition, definitions); T::add_definitions_recursively(definitions); } @@ -294,7 +286,7 @@ where ("Err".to_string(), E::declaration()), ], }; - Self::add_definition(Self::declaration(), definition, definitions); + add_definition(Self::declaration(), definition, definitions); T::add_definitions_recursively(definitions); } @@ -311,7 +303,7 @@ where let definition = Definition::Sequence { elements: T::declaration(), }; - Self::add_definition(Self::declaration(), definition, definitions); + add_definition(Self::declaration(), definition, definitions); T::add_definitions_recursively(definitions); } @@ -328,7 +320,7 @@ where let definition = Definition::Sequence { elements: T::declaration(), }; - Self::add_definition(Self::declaration(), definition, definitions); + add_definition(Self::declaration(), definition, definitions); T::add_definitions_recursively(definitions); } @@ -345,7 +337,7 @@ pub mod hashes { //! [HashMap](std::collections::HashMap)/[HashSet](std::collections::HashSet). use crate::BorshSchema; - use super::{Declaration, Definition}; + use super::{add_definition, Declaration, Definition}; use crate::__private::maybestd::collections::BTreeMap; use crate::__private::maybestd::collections::{HashMap, HashSet}; @@ -361,7 +353,7 @@ pub mod hashes { let definition = Definition::Sequence { elements: <(K, V)>::declaration(), }; - Self::add_definition(Self::declaration(), definition, definitions); + add_definition(Self::declaration(), definition, definitions); <(K, V)>::add_definitions_recursively(definitions); } @@ -377,7 +369,7 @@ pub mod hashes { let definition = Definition::Sequence { elements: ::declaration(), }; - Self::add_definition(Self::declaration(), definition, definitions); + add_definition(Self::declaration(), definition, definitions); ::add_definitions_recursively(definitions); } @@ -396,7 +388,7 @@ where let definition = Definition::Sequence { elements: <(K, V)>::declaration(), }; - Self::add_definition(Self::declaration(), definition, definitions); + add_definition(Self::declaration(), definition, definitions); <(K, V)>::add_definitions_recursively(definitions); } @@ -413,7 +405,7 @@ where let definition = Definition::Sequence { elements: ::declaration(), }; - Self::add_definition(Self::declaration(), definition, definitions); + add_definition(Self::declaration(), definition, definitions); ::add_definitions_recursively(definitions); } @@ -442,7 +434,7 @@ macro_rules! impl_tuple { let elements = vec![$($name::declaration()),+]; let definition = Definition::Tuple { elements }; - Self::add_definition(Self::declaration(), definition, definitions); + add_definition(Self::declaration(), definition, definitions); $( $name::add_definitions_recursively(definitions); )+ diff --git a/borsh/src/schema_helpers.rs b/borsh/src/schema_helpers.rs index c2be1b4a6..931009185 100644 --- a/borsh/src/schema_helpers.rs +++ b/borsh/src/schema_helpers.rs @@ -1,4 +1,5 @@ use crate::__private::maybestd::{ + collections::BTreeMap, io::{Error, ErrorKind, Result}, vec::Vec, }; @@ -11,7 +12,7 @@ use crate::{BorshDeserialize, BorshSchema, BorshSerialize}; /// correct. pub fn try_from_slice_with_schema(v: &[u8]) -> Result { let (schema, object) = from_slice::<(BorshSchemaContainer, T)>(v)?; - if T::schema_container() != schema { + if schema_container_of::() != schema { return Err(Error::new( ErrorKind::InvalidData, "Borsh schema does not match", @@ -23,8 +24,15 @@ pub fn try_from_slice_with_schema(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(value: &T) -> Result> { - let schema = T::schema_container(); + let schema = schema_container_of::(); let mut res = schema.try_to_vec()?; - res.extend(value.try_to_vec()?); + value.serialize(&mut res)?; Ok(res) } + +pub fn schema_container_of() -> BorshSchemaContainer { + let mut definitions = BTreeMap::new(); + T::add_definitions_recursively(&mut definitions); + + BorshSchemaContainer::new(T::declaration(), definitions) +} diff --git a/borsh/tests/smoke.rs b/borsh/tests/smoke.rs index 85b18a02d..fe4f49a65 100644 --- a/borsh/tests/smoke.rs +++ b/borsh/tests/smoke.rs @@ -9,13 +9,14 @@ use alloc::vec; use borsh::{self, from_slice}; #[cfg(feature = "schema")] -use borsh::{try_from_slice_with_schema, BorshSchema}; +use borsh::{schema_container_of, try_from_slice_with_schema}; #[cfg(feature = "schema")] #[test] fn test_to_vec() { let value = 42u8; - let seriazeble = (::schema_container(), value); + + let seriazeble = (schema_container_of::(), value); let serialized = borsh::to_vec(&seriazeble).unwrap(); #[cfg(feature = "std")] println!("serialized: {:?}", serialized); diff --git a/borsh/tests/test_schema_enums.rs b/borsh/tests/test_schema_enums.rs index fce8a09cd..4e902bcc7 100644 --- a/borsh/tests/test_schema_enums.rs +++ b/borsh/tests/test_schema_enums.rs @@ -17,7 +17,7 @@ use alloc::{ }; use borsh::schema::*; -use borsh::schema_helpers::{try_from_slice_with_schema, try_to_vec_with_schema}; +use borsh::{try_from_slice_with_schema, try_to_vec_with_schema}; macro_rules! map( () => { BTreeMap::new() }; diff --git a/borsh/tests/test_schema_primitives.rs b/borsh/tests/test_schema_primitives.rs index ff32b103e..756f503c7 100644 --- a/borsh/tests/test_schema_primitives.rs +++ b/borsh/tests/test_schema_primitives.rs @@ -7,11 +7,12 @@ extern crate alloc; #[cfg(not(feature = "std"))] use alloc::string::ToString; -use borsh::schema::*; +use borsh::{schema::*, schema_container_of}; #[test] fn isize_schema() { - let schema = isize::schema_container(); + let schema = schema_container_of::(); + assert_eq!( schema, BorshSchemaContainer::new("i64".to_string(), Default::default()) @@ -20,7 +21,8 @@ fn isize_schema() { #[test] fn usize_schema() { - let schema = usize::schema_container(); + let schema = schema_container_of::(); + assert_eq!( schema, BorshSchemaContainer::new("u64".to_string(), Default::default()) diff --git a/borsh/tests/test_schema_with.rs b/borsh/tests/test_schema_with.rs index bef815330..11550c9de 100644 --- a/borsh/tests/test_schema_with.rs +++ b/borsh/tests/test_schema_with.rs @@ -64,7 +64,7 @@ mod third_party_impl { ]); let definition = borsh::schema::Definition::Struct { fields }; let no_recursion_flag = definitions.get(&declaration::()).is_none(); - <() as BorshSchema>::add_definition(declaration::(), definition, definitions); + borsh::schema::add_definition(declaration::(), definition, definitions); if no_recursion_flag { as borsh::BorshSchema>::add_definitions_recursively(definitions); }