diff --git a/Cargo.toml b/Cargo.toml index 6ae96e6..1aadc78 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ members = [ "scale-decode-derive", "testing/no_std", ] +resolver = "2" [workspace.package] version = "0.9.0" diff --git a/scale-decode/src/error/mod.rs b/scale-decode/src/error/mod.rs index f8672fc..78af5ea 100644 --- a/scale-decode/src/error/mod.rs +++ b/scale-decode/src/error/mod.rs @@ -103,6 +103,13 @@ impl From for Error { } } +impl From for Error { + fn from(err: codec::Error) -> Error { + let err: DecodeError = err.into(); + Error::new(err.into()) + } +} + /// The underlying nature of the error. #[derive(Debug, derive_more::From, derive_more::Display)] pub enum ErrorKind { diff --git a/scale-decode/src/impls/mod.rs b/scale-decode/src/impls/mod.rs index 73941f5..0c1bc90 100644 --- a/scale-decode/src/impls/mod.rs +++ b/scale-decode/src/impls/mod.rs @@ -262,7 +262,6 @@ macro_rules! impl_decode_seq_via_collect { impl <$generic> Visitor for BasicVisitor<$ty<$generic>> where $generic: IntoVisitor, - Error: From<<$generic::Visitor as Visitor>::Error>, $( $($where)* )? { type Value<'scale, 'info> = $ty<$generic>; @@ -306,11 +305,7 @@ macro_rules! array_method_impl { Ok(arr) }}; } -impl Visitor for BasicVisitor<[T; N]> -where - T: IntoVisitor, - Error: From<::Error>, -{ +impl Visitor for BasicVisitor<[T; N]> { type Value<'scale, 'info> = [T; N]; type Error = Error; @@ -331,22 +326,14 @@ where visit_single_field_composite_tuple_impls!(); } -impl IntoVisitor for [T; N] -where - T: IntoVisitor, - Error: From<::Error>, -{ +impl IntoVisitor for [T; N] { type Visitor = BasicVisitor<[T; N]>; fn into_visitor() -> Self::Visitor { BasicVisitor { _marker: core::marker::PhantomData } } } -impl Visitor for BasicVisitor> -where - T: IntoVisitor, - Error: From<::Error>, -{ +impl Visitor for BasicVisitor> { type Error = Error; type Value<'scale, 'info> = BTreeMap; @@ -375,11 +362,7 @@ where } impl_into_visitor!(BTreeMap); -impl Visitor for BasicVisitor> -where - T: IntoVisitor, - Error: From<::Error>, -{ +impl Visitor for BasicVisitor> { type Error = Error; type Value<'scale, 'info> = Option; @@ -409,13 +392,7 @@ where } impl_into_visitor!(Option); -impl Visitor for BasicVisitor> -where - T: IntoVisitor, - Error: From<::Error>, - E: IntoVisitor, - Error: From<::Error>, -{ +impl Visitor for BasicVisitor> { type Error = Error; type Value<'scale, 'info> = Result; @@ -595,7 +572,6 @@ macro_rules! impl_decode_tuple { impl < $($t),* > Visitor for BasicVisitor<($($t,)*)> where $( $t: IntoVisitor, - Error: From<<$t::Visitor as Visitor>::Error>, )* { type Value<'scale, 'info> = ($($t,)*); @@ -623,7 +599,7 @@ macro_rules! impl_decode_tuple { // We can turn this tuple into a visitor which knows how to decode it: impl < $($t),* > IntoVisitor for ($($t,)*) - where $( $t: IntoVisitor, Error: From<<$t::Visitor as Visitor>::Error>, )* + where $( $t: IntoVisitor, )* { type Visitor = BasicVisitor<($($t,)*)>; fn into_visitor() -> Self::Visitor { @@ -633,7 +609,7 @@ macro_rules! impl_decode_tuple { // We can decode given a list of fields (just delegate to the visitor impl: impl < $($t),* > DecodeAsFields for ($($t,)*) - where $( $t: IntoVisitor, Error: From<<$t::Visitor as Visitor>::Error>, )* + where $( $t: IntoVisitor, )* { fn decode_as_fields<'info>(input: &mut &[u8], fields: &mut dyn FieldIter<'info>, types: &'info scale_info::PortableRegistry) -> Result { let mut composite = crate::visitor::types::Composite::new(input, crate::EMPTY_SCALE_INFO_PATH, fields, types, false); @@ -678,7 +654,6 @@ fn decode_items_using<'a, 'scale, 'info, D: DecodeItemIterator<'scale, 'info>, T ) -> impl Iterator> + 'a where T: IntoVisitor, - Error: From<::Error>, D: DecodeItemIterator<'scale, 'info>, { let mut idx = 0; @@ -712,7 +687,7 @@ mod test { fn assert_encode_decode_to_with(a: &A, b: &B) where A: Encode, - B: DecodeAsType + PartialEq + core::fmt::Debug, + B: IntoVisitor + PartialEq + core::fmt::Debug, T: scale_info::TypeInfo + 'static, { let (type_id, types) = make_type::(); @@ -726,7 +701,7 @@ mod test { fn assert_encode_decode_to(a: &A, b: &B) where A: Encode + scale_info::TypeInfo + 'static, - B: DecodeAsType + PartialEq + core::fmt::Debug, + B: IntoVisitor + PartialEq + core::fmt::Debug, { assert_encode_decode_to_with::(a, b); } diff --git a/scale-decode/src/lib.rs b/scale-decode/src/lib.rs index 549da48..a7c0308 100644 --- a/scale-decode/src/lib.rs +++ b/scale-decode/src/lib.rs @@ -263,8 +263,13 @@ pub trait FieldIter<'a>: Iterator> {} impl<'a, T> FieldIter<'a> for T where T: Iterator> {} /// This trait can be implemented on any type that has an associated [`Visitor`] responsible for decoding -/// SCALE encoded bytes to it. Anything that implements this trait gets a [`DecodeAsType`] implementation -/// for free. +/// SCALE encoded bytes to it whose error type is [`Error`]. Anything that implements this trait gets a +/// [`DecodeAsType`] implementation for free. +// Dev note: This used to allow for any Error type that could be converted into `scale_decode::Error`. +// The problem with this is that the `DecodeAsType` trait became tricky to use in some contexts, because it +// didn't automatically imply so much. Realistically, being stricter here shouldn't matter too much; derive +// impls all use `scale_decode::Error` anyway, and manual impls can just manually convert into the error +// rather than rely on auto conversion, if they care about also being able to impl `DecodeAsType`. pub trait IntoVisitor { /// The visitor type used to decode SCALE encoded bytes to `Self`. type Visitor: for<'scale, 'info> visitor::Visitor = Self, Error = Error>;