Skip to content

Optimize vec<T> for ToProxy - #20

Merged
chenyan2002 merged 2 commits into
mainfrom
specialization
Oct 5, 2026
Merged

chenyan2002 merged 2 commits into
mainfrom
specialization

Conversation

@chenyan2002

@chenyan2002 chenyan2002 commented Oct 4, 2026 •

Copy link
Copy Markdown
Owner

Fix #3. Implements a Rust version of Leibniz equality

  • ToProxy gets const IS_ID: bool and fn cast<F: TypeCtor>(x: F::Apply<Self>) -> Result<F::Apply<Self::Output>, F::Apply<Self>>. By default, cast returns Err(x), handing the value back so the caller can iterate
  • Identity impls set IS_ID = true and return Ok(x): String, the primitives, and pass-through structs/enums/flags. That type-checks only because Output == Self in those impls.
  • TypeCtor { type Apply<X>; } encodes a "type with a hole" using a generic associated type. The constructors are Id, InVec, InOption, InResultOk, InResultErr, and InTuple{n}_{i} for each tuple position.
  • Vec, Option, Result and tuples pass cast down to their element types, adding one constructor layer each. Result and tuples check IS_ID first, so a cast can never fail halfway through.
  • Vec::to_proxy tries T::cast::<InVec<Id>>(self) first and only iterates if that fails. Once compiled, the identity case just returns the Vec it was given.

As a result, any nesting of Vec, Option, Result and tuples over identity types now converts with no per-element work, e.g. Vec<Vec<u8>>, Vec<(String, Vec<u8>)> or Vec<Result<Option<…>, ()>>. Types that really need converting go through the old per-element path, unchanged.

Requires Rust ≥ 1.65 (GATs)

@chenyan2002
chenyan2002 merged commit 419b4c9 into main Oct 5, 2026
2 checks passed
@chenyan2002
chenyan2002 deleted the specialization branch October 5, 2026 00:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid iterating Vec<T> when ToProxy::Output == Vec<T>

1 participant