Skip to content

Comments

refactor(core): Convert BoxModule from type alias to newtype struct#11978

Merged
hardfist merged 5 commits intoweb-infra-dev:mainfrom
kdy1:refactor/box-module
Oct 23, 2025
Merged

refactor(core): Convert BoxModule from type alias to newtype struct#11978
hardfist merged 5 commits intoweb-infra-dev:mainfrom
kdy1:refactor/box-module

Conversation

@kdy1
Copy link
Contributor

@kdy1 kdy1 commented Oct 22, 2025

I tried AutoDev for refactoring because I got a request from a core rspack team member.

See kdy1#2 for the full work, and my prompt was

@crates/rspack_core/src/module.rs

Refactor BoxModule into struct BoxModule(Box)

with Composite Task enabled (Automatic task decomposition / prompt enhancing).

I removed autodev.yml, which is added because it's required to run AutoDev


Summary

This PR refactors BoxModule from a simple type alias to a newtype struct pattern for improved type safety and better encapsulation while maintaining all existing functionality.

Objective

Refactor BoxModule in crates/rspack_core/src/module.rs from a type alias to a newtype struct pattern. This change improves type safety and provides better encapsulation while maintaining all existing functionality.

Current Implementation

Previously, BoxModule was defined as:

pub type BoxModule = Box<dyn Module>;

New Implementation

Now BoxModule is a newtype struct:

#[cacheable(with=AsInner)]
#[repr(transparent)]
pub struct BoxModule(Box<dyn Module>);

Changes Made

Core Module (crates/rspack_core/src/module.rs)

  • Converted BoxModule from type alias to newtype struct
  • Implemented essential traits:
    • Deref/DerefMut: Allow transparent access to Box<dyn Module> methods
    • From<Box>: Enable ergonomic construction
    • AsRef/AsMut: Provide reference conversions
    • Debug: Forward to inner type's Debug implementation
    • Identifiable: Forward to inner module's identifier method
    • AsInnerConverter: Enable cacheable serialization support
  • Added BoxModule::new() constructor method
  • Updated ModuleExt::boxed() to return BoxModule

Compilation & Module Management

  • Updated BuildTask and AddTask to use BoxModule
  • Fixed type conversions across module factories
  • Updated module graph operations for proper deref coercion

Plugin Updates (19 files changed)

  • rspack_plugin_devtool: Fixed borrow checker issues
  • rspack_plugin_dll: Updated DLL and delegated module factories
  • rspack_plugin_extract_css: Updated CSS module factory
  • rspack_plugin_javascript: Fixed ESM dependency checks
  • rspack_plugin_lazy_compilation: Updated proxy module creation
  • rspack_plugin_mf: Updated container and sharing module factories
  • rspack_plugin_split_chunks: Fixed module reference handling

Benefits

  • Type Safety: Prevents accidental mixing with other boxed types
  • Better Encapsulation: Private inner field with clear API boundary
  • Zero-Cost Abstraction: #[repr(transparent)] ensures no runtime overhead
  • Improved Maintainability: Single, well-defined interface for module boxing

Compatibility

  • All existing code continues to work due to Deref coercion
  • The .boxed() helper provides ergonomic module creation
  • Cacheable serialization works seamlessly

Testing

  • cargo check --workspace passes without errors
  • cargo test --workspace (pending)
  • All type conversions compile correctly
  • Deref coercion works as expected

References

🤖 Generated with Claude Code

Co-Authored-By: Claude noreply@anthropic.com

kdy1 and others added 5 commits October 22, 2025 19:39
## 🤖 Installing Claude Code GitHub App

This PR adds a GitHub Actions workflow that enables Claude Code
integration in our repository.

### What is Claude Code?

[Claude Code](https://claude.com/claude-code) is an AI coding agent that
can help with:
- Bug fixes and improvements  
- Documentation updates
- Implementing new features
- Code reviews and suggestions
- Writing tests
- And more!

### How it works

Once this PR is merged, we'll be able to interact with Claude by
mentioning @claude in a pull request or issue comment.
Once the workflow is triggered, Claude will analyze the comment and
surrounding context, and execute on the request in a GitHub action.

### Important Notes

- **This workflow won't take effect until this PR is merged**
- **@claude mentions won't work until after the merge is complete**
- The workflow runs automatically whenever Claude is mentioned in PR or
issue comments
- Claude gets access to the entire PR or issue context including files,
diffs, and previous comments

### Security

- Our Anthropic API key is securely stored as a GitHub Actions secret
- Only users with write access to the repository can trigger the
workflow
- All Claude runs are stored in the GitHub Actions run history
- Claude's default tools are limited to reading/writing files and
interacting with our repo by creating comments, branches, and commits.
- We can add more allowed tools by adding them to the workflow file
like:

```
allowed_tools: Bash(npm install),Bash(npm run build),Bash(npm run lint),Bash(npm run test)
```

There's more information in the [Claude Code action
repo](https://github.com/anthropics/claude-code-action).

After merging this PR, let's try mentioning @claude in a comment on any
PR to get started!
## Summary
This PR refactors `BoxModule` from a simple type alias to a newtype
struct pattern for improved type safety and better encapsulation while
maintaining all existing functionality.

## Objective
Refactor `BoxModule` in `crates/rspack_core/src/module.rs` from a type
alias to a newtype struct pattern. This change improves type safety and
provides better encapsulation while maintaining all existing
functionality.

## Current Implementation
Previously, `BoxModule` was defined as:
```rust
pub type BoxModule = Box<dyn Module>;
```

## New Implementation
Now `BoxModule` is a newtype struct:
```rust
#[cacheable(with=AsInner)]
#[repr(transparent)]
pub struct BoxModule(Box<dyn Module>);
```

## Changes Made

### Core Module (`crates/rspack_core/src/module.rs`)
- Converted `BoxModule` from type alias to newtype struct
- Implemented essential traits:
- **Deref/DerefMut**: Allow transparent access to `Box<dyn Module>`
methods
  - **From<Box<dyn Module>>**: Enable ergonomic construction
  - **AsRef/AsMut**: Provide reference conversions
  - **Debug**: Forward to inner type's Debug implementation
  - **Identifiable**: Forward to inner module's identifier method
  - **AsInnerConverter**: Enable cacheable serialization support
- Added `BoxModule::new()` constructor method
- Updated `ModuleExt::boxed()` to return `BoxModule`

### Compilation & Module Management
- Updated `BuildTask` and `AddTask` to use `BoxModule`
- Fixed type conversions across module factories
- Updated module graph operations for proper deref coercion

### Plugin Updates (19 files changed)
- **rspack_plugin_devtool**: Fixed borrow checker issues
- **rspack_plugin_dll**: Updated DLL and delegated module factories
- **rspack_plugin_extract_css**: Updated CSS module factory
- **rspack_plugin_javascript**: Fixed ESM dependency checks
- **rspack_plugin_lazy_compilation**: Updated proxy module creation
- **rspack_plugin_mf**: Updated container and sharing module factories
- **rspack_plugin_split_chunks**: Fixed module reference handling

## Benefits
- **Type Safety**: Prevents accidental mixing with other boxed types
- **Better Encapsulation**: Private inner field with clear API boundary
- **Zero-Cost Abstraction**: `#[repr(transparent)]` ensures no runtime
overhead
- **Improved Maintainability**: Single, well-defined interface for
module boxing

## Compatibility
- All existing code continues to work due to `Deref` coercion
- The `.boxed()` helper provides ergonomic module creation
- Cacheable serialization works seamlessly

## Testing
- [x] `cargo check --workspace` passes without errors
- [ ] `cargo test --workspace` (pending)
- [x] All type conversions compile correctly
- [x] Deref coercion works as expected

## References
- Rust newtype pattern:
https://rust-unofficial.github.io/patterns/patterns/behavioural/newtype.html
- Rust Book - Advanced Types:
https://doc.rust-lang.org/book/ch19-04-advanced-types.html
- Effective Rust - Newtype Pattern:
https://www.lurklurk.org/effective-rust/newtype.html

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

---------

Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com>
Co-authored-by: Claude <noreply@anthropic.com>
@netlify
Copy link

netlify bot commented Oct 22, 2025

Deploy Preview for rspack canceled.

Built without sensitive environment variables

Name Link
🔨 Latest commit 91af1e2
🔍 Latest deploy log https://app.netlify.com/projects/rspack/deploys/68f8fb99ff8d330008d2d9ac

@hardfist
Copy link
Contributor

@codex review

@chatgpt-codex-connector
Copy link

Codex Review: Didn't find any major issues. Swish!

ℹ️ 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".

@kdy1 kdy1 marked this pull request as ready for review October 22, 2025 16:04
@kdy1 kdy1 requested review from JSerFeng and ahabhgk as code owners October 22, 2025 16:04
@codspeed-hq
Copy link

codspeed-hq bot commented Oct 22, 2025

CodSpeed Performance Report

Merging #11978 will not alter performance

Comparing kdy1:refactor/box-module (91af1e2) with main (e28e40e)

Summary

✅ 17 untouched

@hardfist hardfist merged commit d3b6760 into web-infra-dev:main Oct 23, 2025
47 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants