paritytech / paritytech/parity-scale-codec

Maybe bounds for recursive types can be improved

Open
#217 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Rust
Stars
287
Forks
103
Avg merge
6d 13h
Merged PRs (30d)
1

Description

This is a potential improvement but it doesn't completely solve the issue for recursive types so not sure it matters, I'm OK to close.

the following example doesn't compiles:

#[test]
fn recursive_variant_3_encode_works() {
	#[derive(Default)]
	struct NonEncodableType;

	#[derive(Default, Encode)]
	struct Recursive<A, B, N> {
		other: Vec<(A, B, Recursive<A, B, N>)>,
		n: core::marker::PhantomData<N>,
	}

	let val: Recursive<u32, i32, NonEncodableType> = Recursive::default();
	val.encode();
}

this is because the bounds are:

A: Encode,
B: Encode,
N: Encode,
core::marker::PhantomData<N>: Encode,

for recursive types like Vec<(A, B, Recursive<A, B, N>)>, I think we could only bound all used generics but not those inside Self types: thus A and B here.

(I tested and bounding only A, B, and phantom make the test works, though I haven't tried substrate codebase)

Though anyway this is just best effort, ideally we should bound Vec<(A, B, Recursive<A, B, N>)>: Encode but this is not possible.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the recursive_variant_3_encode_works example from the issue and inspect the derive-generated bounds for Recursive. Determine whether recursive field types can avoid requiring Encode for N; done means the example compiles while retaining correct bounds for A, B, and the relevant PhantomData type.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.