From ffad86bd8a230d47acf3afdfd0101fa82813b757 Mon Sep 17 00:00:00 2001 From: dzmitry-lahoda Date: Wed, 15 May 2024 22:06:47 +0100 Subject: [PATCH 1/5] assert variant too suprising --- borsh/Cargo.toml | 2 +- borsh/src/schema.rs | 38 ++++++++++++++++++++++++++++++++++++++ borsh/src/ser/mod.rs | 5 ++--- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/borsh/Cargo.toml b/borsh/Cargo.toml index 73f1d8991..178fbcdf1 100644 --- a/borsh/Cargo.toml +++ b/borsh/Cargo.toml @@ -45,7 +45,7 @@ features = ["derive", "unstable__schema", "rc"] targets = ["x86_64-unknown-linux-gnu"] [features] -default = ["std"] +default = ["std", "unstable__schema"] derive = ["borsh-derive"] unstable__schema = ["derive", "borsh-derive/schema"] std = [] diff --git a/borsh/src/schema.rs b/borsh/src/schema.rs index 9988737c9..e6cac0782 100644 --- a/borsh/src/schema.rs +++ b/borsh/src/schema.rs @@ -715,6 +715,44 @@ pub mod hashes { format!(r#"HashMap<{}, {}>"#, K::declaration(), V::declaration()) } } + + impl BorshSchema for HashMap + where + K: BorshSchema, + V: BorshSchema, + S: BorshSchema + { + fn add_definitions_recursively(definitions: &mut BTreeMap) { + S::add_definitions_recursively(definitions); + + // makes explicit that user must provide empty definition instead of not adding nothing + // nor here plug manually empty + let tuple_or_empty_struct = &definitions[S::declaration().as_str()]; + assert!( + matches!( + tuple_or_empty_struct, Definition::Struct { fields: crate::schema::Fields::Empty } + ) + || + matches!( + tuple_or_empty_struct, Definition::Tuple { elements } if elements.is_empty() + ), + "S is not serialized or deserialized, so its schema must be an empty tuple or struct" + ); + + let definition = Definition::Sequence { + length_width: Definition::DEFAULT_LENGTH_WIDTH, + length_range: Definition::DEFAULT_LENGTH_RANGE, + elements: <(K, V)>::declaration(), + }; + add_definition(Self::declaration(), definition, definitions); + <(K, V)>::add_definitions_recursively(definitions); + } + + fn declaration() -> Declaration { + format!(r#"HashMap<{}, {}, {}>"#, K::declaration(), V::declaration(), S::declaration()) + } + } + impl BorshSchema for HashSet where T: BorshSchema, diff --git a/borsh/src/ser/mod.rs b/borsh/src/ser/mod.rs index e14b6dcfb..5097f8920 100644 --- a/borsh/src/ser/mod.rs +++ b/borsh/src/ser/mod.rs @@ -384,9 +384,8 @@ pub mod hashes { u32::try_from(vec.len()) .map_err(|_| ErrorKind::InvalidData)? .serialize(writer)?; - for (key, value) in vec { - key.serialize(writer)?; - value.serialize(writer)?; + for kv in vec { + kv.serialize(writer)?; } Ok(()) } From ecb2bec81c2bcbd802db8f33d13b3de9831a0b76 Mon Sep 17 00:00:00 2001 From: dzmitry-lahoda Date: Wed, 15 May 2024 22:10:09 +0100 Subject: [PATCH 2/5] may be this right? --- borsh/src/schema.rs | 74 ++++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/borsh/src/schema.rs b/borsh/src/schema.rs index e6cac0782..38cccb5bb 100644 --- a/borsh/src/schema.rs +++ b/borsh/src/schema.rs @@ -696,7 +696,7 @@ pub mod hashes { #[cfg(not(feature = "std"))] use alloc::format; - impl BorshSchema for HashMap + impl BorshSchema for HashMap where K: BorshSchema, V: BorshSchema, @@ -716,42 +716,42 @@ pub mod hashes { } } - impl BorshSchema for HashMap - where - K: BorshSchema, - V: BorshSchema, - S: BorshSchema - { - fn add_definitions_recursively(definitions: &mut BTreeMap) { - S::add_definitions_recursively(definitions); - - // makes explicit that user must provide empty definition instead of not adding nothing - // nor here plug manually empty - let tuple_or_empty_struct = &definitions[S::declaration().as_str()]; - assert!( - matches!( - tuple_or_empty_struct, Definition::Struct { fields: crate::schema::Fields::Empty } - ) - || - matches!( - tuple_or_empty_struct, Definition::Tuple { elements } if elements.is_empty() - ), - "S is not serialized or deserialized, so its schema must be an empty tuple or struct" - ); - - let definition = Definition::Sequence { - length_width: Definition::DEFAULT_LENGTH_WIDTH, - length_range: Definition::DEFAULT_LENGTH_RANGE, - elements: <(K, V)>::declaration(), - }; - add_definition(Self::declaration(), definition, definitions); - <(K, V)>::add_definitions_recursively(definitions); - } - - fn declaration() -> Declaration { - format!(r#"HashMap<{}, {}, {}>"#, K::declaration(), V::declaration(), S::declaration()) - } - } + // impl BorshSchema for HashMap + // where + // K: BorshSchema, + // V: BorshSchema, + // S: BorshSchema + // { + // fn add_definitions_recursively(definitions: &mut BTreeMap) { + // S::add_definitions_recursively(definitions); + + // // makes explicit that user must provide empty definition instead of not adding nothing + // // nor here plug manually empty + // let tuple_or_empty_struct = &definitions[S::declaration().as_str()]; + // assert!( + // matches!( + // tuple_or_empty_struct, Definition::Struct { fields: crate::schema::Fields::Empty } + // ) + // || + // matches!( + // tuple_or_empty_struct, Definition::Tuple { elements } if elements.is_empty() + // ), + // "S is not serialized or deserialized, so its schema must be an empty tuple or struct" + // ); + + // let definition = Definition::Sequence { + // length_width: Definition::DEFAULT_LENGTH_WIDTH, + // length_range: Definition::DEFAULT_LENGTH_RANGE, + // elements: <(K, V)>::declaration(), + // }; + // add_definition(Self::declaration(), definition, definitions); + // <(K, V)>::add_definitions_recursively(definitions); + // } + + // fn declaration() -> Declaration { + // format!(r#"HashMap<{}, {}, {}>"#, K::declaration(), V::declaration(), S::declaration()) + // } + // } impl BorshSchema for HashSet where From 59d131f15de94913fd6dc6b2b692a8a42d248942 Mon Sep 17 00:00:00 2001 From: dzmitry-lahoda Date: Wed, 15 May 2024 22:11:59 +0100 Subject: [PATCH 3/5] and set --- borsh/src/schema.rs | 39 +-------------------------------------- 1 file changed, 1 insertion(+), 38 deletions(-) diff --git a/borsh/src/schema.rs b/borsh/src/schema.rs index 38cccb5bb..a976fb04d 100644 --- a/borsh/src/schema.rs +++ b/borsh/src/schema.rs @@ -716,44 +716,7 @@ pub mod hashes { } } - // impl BorshSchema for HashMap - // where - // K: BorshSchema, - // V: BorshSchema, - // S: BorshSchema - // { - // fn add_definitions_recursively(definitions: &mut BTreeMap) { - // S::add_definitions_recursively(definitions); - - // // makes explicit that user must provide empty definition instead of not adding nothing - // // nor here plug manually empty - // let tuple_or_empty_struct = &definitions[S::declaration().as_str()]; - // assert!( - // matches!( - // tuple_or_empty_struct, Definition::Struct { fields: crate::schema::Fields::Empty } - // ) - // || - // matches!( - // tuple_or_empty_struct, Definition::Tuple { elements } if elements.is_empty() - // ), - // "S is not serialized or deserialized, so its schema must be an empty tuple or struct" - // ); - - // let definition = Definition::Sequence { - // length_width: Definition::DEFAULT_LENGTH_WIDTH, - // length_range: Definition::DEFAULT_LENGTH_RANGE, - // elements: <(K, V)>::declaration(), - // }; - // add_definition(Self::declaration(), definition, definitions); - // <(K, V)>::add_definitions_recursively(definitions); - // } - - // fn declaration() -> Declaration { - // format!(r#"HashMap<{}, {}, {}>"#, K::declaration(), V::declaration(), S::declaration()) - // } - // } - - impl BorshSchema for HashSet + impl BorshSchema for HashSet where T: BorshSchema, { From baee979d083cc11b563436c7a47a17457c434963 Mon Sep 17 00:00:00 2001 From: dzmitry-lahoda Date: Wed, 15 May 2024 22:13:43 +0100 Subject: [PATCH 4/5] clean --- borsh/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/borsh/Cargo.toml b/borsh/Cargo.toml index 178fbcdf1..73f1d8991 100644 --- a/borsh/Cargo.toml +++ b/borsh/Cargo.toml @@ -45,7 +45,7 @@ features = ["derive", "unstable__schema", "rc"] targets = ["x86_64-unknown-linux-gnu"] [features] -default = ["std", "unstable__schema"] +default = ["std"] derive = ["borsh-derive"] unstable__schema = ["derive", "borsh-derive/schema"] std = [] From 33705a7300aeaa71aaa93a10a1e4e58ae0061d4c Mon Sep 17 00:00:00 2001 From: dzmitry-lahoda Date: Wed, 15 May 2024 22:17:03 +0100 Subject: [PATCH 5/5] why --- borsh/src/schema.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/borsh/src/schema.rs b/borsh/src/schema.rs index a976fb04d..5068144b0 100644 --- a/borsh/src/schema.rs +++ b/borsh/src/schema.rs @@ -696,6 +696,10 @@ pub mod hashes { #[cfg(not(feature = "std"))] use alloc::format; + // S is not serialized, so we ignore it in schema too + // forcing S to be BorshSchema forces to define Definition + // which must be empty, but if not - it will fail + // so better to ignore it impl BorshSchema for HashMap where K: BorshSchema,