Skip to content

Conversation

Boshen
Copy link
Member

@Boshen Boshen commented Sep 30, 2025

Summary

This PR adds support for creating index types backed by NonMaxU32 from the nonmax crate, enabling memory-efficient Option<Index> representations.

Changes

  • ✨ Add nonmax as optional dependency
  • 🔧 Extend define_index_type! macro to accept type paths in addition to identifiers
  • ✨ Add new define_nonmax_index_type! macro for NonMaxU32-backed indices
  • ✅ Add comprehensive tests for NonMaxU32 functionality
  • 📝 Update documentation with NonMaxU32 usage examples

Benefits

  • Memory efficient: Option<NonMaxIndex> is the same size as NonMaxIndex (4 bytes)
  • Type safe: Cannot mix indices from different domains
  • Full API compatibility: Supports all standard index operations
  • Zero runtime overhead: All operations are inlined

Usage

// Enable the feature in Cargo.toml:
// oxc_index = { version = "3.1", features = ["nonmax"] }

use oxc_index::{IndexVec, define_nonmax_index_type};

define_nonmax_index_type! {
    pub struct StringId;
}

fn main() {
    let mut strings: IndexVec<StringId, String> = oxc_index::index_vec![];
    let id = strings.push("hello".to_string());
    println!("Added at index: {}", id.index());
    
    // Option<StringId> has the same size as StringId (4 bytes)
    assert_eq!(
        std::mem::size_of::<Option<StringId>>(),
        std::mem::size_of::<StringId>()
    );
}

Testing

All tests pass:

  • ✅ 37 tests pass with --all-features
  • ✅ 35 tests pass without features
  • ✅ Documentation builds successfully
  • ✅ Library compiles with --no-default-features

Resolves the request to make the macro work with NonMaxU32 from the nonmax crate.

🤖 Generated with Claude Code

This commit adds support for creating index types backed by NonMaxU32
from the nonmax crate, enabling memory-efficient Option<Index>
representations.

## Changes

- Add `nonmax` as optional dependency
- Extend `define_index_type!` macro to accept type paths in addition to identifiers
- Add new `define_nonmax_index_type!` macro for NonMaxU32-backed indices
- Add comprehensive tests for NonMaxU32 functionality
- Update documentation with NonMaxU32 usage examples

## Benefits

- Memory efficient: Option<NonMaxIndex> is same size as NonMaxIndex (4 bytes)
- Type safe: Cannot mix indices from different domains
- Full API compatibility: Supports all standard index operations
- Zero runtime overhead: All operations are inlined

## Usage

```rust
oxc_index::define_nonmax_index_type! {
    pub struct MyIndex;
}

let mut vec: IndexVec<MyIndex, String> = index_vec![];
let id = vec.push("hello".to_string());
```

Enable the feature in Cargo.toml:
```toml
oxc_index = { version = "3.1", features = ["nonmax"] }
```

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

Co-Authored-By: Claude <noreply@anthropic.com>
@Boshen Boshen merged commit 4d69844 into main Sep 30, 2025
4 checks passed
@Boshen Boshen deleted the feat/nonmax-support branch September 30, 2025 04:49
@Boshen Boshen mentioned this pull request Sep 30, 2025
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