Skip to content

refactor: serialize CSS through PrinterTrait - #17

Merged
intellild merged 1 commit into
codex/rstack-printer-writefrom
codex/rstack-to-css-printer-trait
Nov 9, 2026
Merged

intellild merged 1 commit into
codex/rstack-printer-writefrom
codex/rstack-to-css-printer-trait

Conversation

@intellild

@intellild intellild commented Nov 4, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Introduce the sealed PrinterTrait abstraction with scoped access to printer internals.
  • Change ToCss and related serialization helpers to accept PrinterTrait rather than one concrete Printer<W> type.
  • Keep the .map_err(Into::into) to ? cleanup in the prerequisite printer PR, so this diff focuses on the trait migration.
  • Preserve the existing parcel_sourcemap API in this layer.
  • Reuse the existing scoped context helpers for nesting and @scope. Keep mapping feature gates and serializer state transitions aligned with the base branch.

Example

The same serializer now works with any internal printer variant implementing PrinterTrait:

fn to_css<P: PrinterTrait>(&self, dest: &mut P) -> Result<(), PrinterError> {
  dest.write_str("example")?;
  Ok(())
}

This is the prerequisite for adding a source-map-generic printer in the final PR without duplicating every ToCss implementation.

Validation

  • cargo check --locked --workspace --all-features
  • cargo test --locked -p rspack-lightningcss --all-features (184 passed)
  • cargo check --locked -p rspack-lightningcss --no-default-features --features visitor --example custom_at_rule --test test_custom_parser

Stack

  1. Fix bundled source-map indices
  2. Use std::fmt::Write for printer output
  3. This PR
  4. Introduce the custom_sourcemap feature flag
  5. Custom source maps through generic PrinterOptions

by OpenAI Codex

Copilot AI balanced review requested due to automatic review settings November 4, 2026 06:52

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-04T06:55:47.669437Z c6503a0 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@codspeed

codspeed Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 8 untouched benchmarks
⏩ 8 skipped benchmarks1


Comparing codex/rstack-to-css-printer-trait (62d0547) with codex/rstack-printer-write (f077386)

Open in CodSpeed

Footnotes

  1. 8 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c6503a0d3a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/printer.rs Outdated
Comment on lines +427 to +428
fn state_mut(&mut self) -> &mut PrinterState<'_, '_> {
unsafe { std::mem::transmute::<&mut PrinterState<'a, 'c>, &mut PrinterState<'_, '_>>(&mut self.state) }

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Prevent lifetime-erased printer state from escaping

A safe custom ToCss implementation can call std::mem::swap(dest.state_mut(), other.state_mut()) with another printer because both returned states have lifetimes shortened to the temporary borrows. This allows borrowed CSS-module data, or the stack-backed nesting context, to move into a printer that outlives the original serialization. Subsequent serialization through that printer can then dereference dangling references or context pointers. Sealing PrinterTrait does not prevent callers from using its public methods this way. Keep mutable state inaccessible to external implementations, or expose operations that preserve its actual lifetimes rather than transmuting a mutable reference.

Useful? React with 👍 / 👎.

@intellild
intellild added this pull request to stack #19 November 4, 2026 06:57
@intellild
intellild force-pushed the codex/rstack-to-css-printer-trait branch from c6503a0 to 88b1907 Compare November 4, 2026 07:06
@intellild
intellild force-pushed the codex/rstack-to-css-printer-trait branch from 88b1907 to 2ed94ad Compare November 4, 2026 07:45
@intellild
intellild removed this pull request from stack #19 November 4, 2026 09:03
@intellild
intellild added this pull request to stack #21 November 4, 2026 09:04
@intellild
intellild removed this pull request from stack #21 November 4, 2026 09:34
@intellild
intellild force-pushed the codex/rstack-to-css-printer-trait branch from 2ed94ad to 62d0547 Compare November 4, 2026 09:35
@intellild
intellild added this pull request to stack #24 November 5, 2026 02:22
@intellild
intellild merged commit b5d575f into master Nov 9, 2026
7 checks passed
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.

2 participants