Skip to content

Conversation

@eyalleshem
Copy link
Contributor

@eyalleshem eyalleshem commented Dec 1, 2025

his PR adds a lifetime parameter to the Token enum to enable zero-copy tokenization in the future.

Backward Compatibility:
To maintain backward compatibility, we renamed Token to BorrowedToken<'a> and introduced Token as a type alias for BorrowedToken<'static>. This allows existing consumers of the library to continue using Token without needing to handle
lifetimes throughout their code.

New API:
This PR also adds a tokenized_owned() method for use cases where consumers prefer to pay the cost of copying in exchange for owned tokens.

Current State:
This commit does not yet change the tokenizer's behavior—all string allocations remain in place. The goal of the following commits is to replace String with Cow<'a, str> in as many places as possible, leveraging the Borrowed variant to
achieve zero-copy tokenization where feasible.

  This change introduces a lifetime parameter 'a to BorrowedToken enum
  to prepare for zero-copy tokenization support. This is a foundational
  step toward reducing memory allocations during SQL parsing.

  Changes:
  - Added lifetime parameter to BorrowedToken<'a> enum
  - Added _Phantom(Cow<'a, str>) variant to carry the lifetime
  - Implemented Visit and VisitMut traits for Cow<'a, str> to support
    the visitor pattern with the new lifetime parameter
  - Fixed lifetime issues in visitor tests by using tokenized_owned()
    instead of tokenize() where owned tokens are required
  - Type alias Token = BorrowedToken<'static> maintains backward
    compatibility
@eyalleshem eyalleshem force-pushed the add_lifetime_to_token branch from 26f485d to 0f17b32 Compare December 18, 2025 15:55
@eyalleshem eyalleshem marked this pull request as ready for review December 18, 2025 18:10
@eyalleshem
Copy link
Contributor Author

@iffyio @alamb - Marking this one ready for review too :)

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.

1 participant