From eef7e4c04a30864eb0ad604ffc2b4152f68bcd59 Mon Sep 17 00:00:00 2001 From: James Wilson Date: Thu, 9 Oct 2025 10:33:28 +0100 Subject: [PATCH 1/3] impl DecodeAsFields for Value<()> --- src/scale_impls/decode.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/scale_impls/decode.rs b/src/scale_impls/decode.rs index a0fdeb1..fa3d533 100644 --- a/src/scale_impls/decode.rs +++ b/src/scale_impls/decode.rs @@ -87,7 +87,6 @@ macro_rules! to_unnamed_composite { }}; } -// We can't implement this on `Value` because we have no TypeId to assign to the value. impl scale_decode::DecodeAsFields for Composite<()> { fn decode_as_fields<'resolver, R: TypeResolver>( input: &mut &[u8], @@ -111,6 +110,17 @@ impl scale_decode::DecodeAsFields for Composite<()> { } } +impl scale_decode::DecodeAsFields for Value<()> { + fn decode_as_fields<'resolver, R: TypeResolver>( + input: &mut &[u8], + fields: &mut dyn FieldIter<'resolver, R::TypeId>, + types: &'resolver R, + ) -> Result { + let composite = Composite::<()>::decode_as_fields(input, fields, types)?; + Ok(Value { value: ValueDef::Composite(composite), context: () }) + } +} + impl scale_decode::IntoVisitor for Value<()> { // Note: the DefaultMapper just removes all type ids here. type AnyVisitor = From e8f3a976704d7004845fad1ad7edd816061930d6 Mon Sep 17 00:00:00 2001 From: James Wilson Date: Thu, 9 Oct 2025 10:49:37 +0100 Subject: [PATCH 2/3] Patch-bump version and improve value macro docs a little --- CHANGELOG.md | 6 ++++++ Cargo.toml | 2 +- src/macros.rs | 58 ++++++++++++++++++++++++++++++++++++++++----------- 3 files changed, 53 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c5f070a..533f146 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ The format is based on [Keep a Changelog]. [Keep a Changelog]: http://keepachangelog.com/en/1.0.0/ +# 0.18.1 (2025-10-09) + +A small patch release to impl `DecodeAsFields` for `scale_value::Value<()>`, allowing the `Value` type to be used in more contexts upstream. + +Also elaborate on the `value!` macro docs a little. + ## 0.18.0 (2024-11-15) This release makes scale-value entirely no_std which is now using core::error::Error instead of std::error::Error as it was using before behind diff --git a/Cargo.toml b/Cargo.toml index 29a2e6b..7479cb4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "scale-value" -version = "0.18.0" +version = "0.18.1" authors = ["Parity Technologies "] edition = "2021" diff --git a/src/macros.rs b/src/macros.rs index 92a1d4b..493392b 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -13,30 +13,64 @@ // See the License for the specific language governing permissions and // limitations under the License. -/// Construct a `scale_value::Value` +/// Construct a `scale_value::Value` using syntax similar to the `serde_jaon::json!` macro. /// -/// -/// Supports unnamed and named composites and variants: +/// Construct values representing structs (SCALE "composite" types with named fields): +/// /// ``` /// use scale_value::value; -/// -/// let val = value!({ -/// name: "localhost", -/// address: V4(127, 0, 0, 1), -/// payload: { -/// bytes: (255, 3, 4, 9), -/// method: ("Post", 3000), -/// }, +/// +/// let struct_value = value!({ +/// name: "foo", +/// value: 123, +/// sub_fields: { +/// foo: true, +/// bar: false +/// } /// }); -/// /// ``` +/// +/// Construct values representing tuples (SCALE composite types with unnamed fields): +/// +/// ``` +/// use scale_value::value; +/// +/// let tuple_value = value!{ +/// ("foo", 123, (true, false)) +/// }; +/// ``` +/// +/// Construct enum variants by prefixing the variant name to either of the above constructions: +/// +/// ``` +/// use scale_value::value; +/// +/// let variant_with_unnamed_fields = value!{ +/// VariantName("foo", 123) +/// }; +/// +/// let variant_with_named_fields = value!{ +/// VariantName { +/// name: "foo", +/// value: 123, +/// sub_fields: { +/// foo: true, +/// bar: false, +/// other_variant: AnotherVariant(true, 1,2,3) +/// } +/// } +/// }; +/// ``` +/// /// Values can be nested in each other: +/// /// ``` /// use scale_value::value; /// /// let data_value = value!((1, v1(1, 2), 3)); /// let val = value!(POST { data: data_value }); /// ``` +/// /// Trailing commas are optional. #[macro_export(local_inner_macros)] macro_rules! value { From 54d9b0ab3baa831855159ad8b0c866039af2376e Mon Sep 17 00:00:00 2001 From: James Wilson Date: Thu, 9 Oct 2025 10:50:53 +0100 Subject: [PATCH 3/3] fmt --- src/macros.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index 493392b..3145e4b 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -16,10 +16,10 @@ /// Construct a `scale_value::Value` using syntax similar to the `serde_jaon::json!` macro. /// /// Construct values representing structs (SCALE "composite" types with named fields): -/// +/// /// ``` /// use scale_value::value; -/// +/// /// let struct_value = value!({ /// name: "foo", /// value: 123, @@ -29,26 +29,26 @@ /// } /// }); /// ``` -/// +/// /// Construct values representing tuples (SCALE composite types with unnamed fields): -/// +/// /// ``` /// use scale_value::value; -/// +/// /// let tuple_value = value!{ /// ("foo", 123, (true, false)) /// }; /// ``` -/// +/// /// Construct enum variants by prefixing the variant name to either of the above constructions: -/// +/// /// ``` /// use scale_value::value; -/// +/// /// let variant_with_unnamed_fields = value!{ /// VariantName("foo", 123) /// }; -/// +/// /// let variant_with_named_fields = value!{ /// VariantName { /// name: "foo", @@ -61,16 +61,16 @@ /// } /// }; /// ``` -/// +/// /// Values can be nested in each other: -/// +/// /// ``` /// use scale_value::value; /// /// let data_value = value!((1, v1(1, 2), 3)); /// let val = value!(POST { data: data_value }); /// ``` -/// +/// /// Trailing commas are optional. #[macro_export(local_inner_macros)] macro_rules! value {