0xMiden/miden-vm

Explore nutype for small checked newtypes

Open

#3,259 创建于 2026年6月16日

在 GitHub 查看
 (3 评论) (0 反应) (0 负责人)Rust (303 fork)github user discovery
good first issue

仓库指标

Star
 (753 star)
PR 合并指标
 (PR 指标待抓取)

描述

Several small types in this repo repeat the same pattern. They have a private wrapper, a checked constructor, a small error type, and serde code that must call the same checks. The nutype crate may remove some of that code while keeping invalid values out after serde deserialization.

I would start with ItemIndex in crates/assembly-syntax/src/ast/item/index.rs. It only checks that a usize fits in u16, then exposes a few simple traits and Display. This is a small test case before touching parser or VM types.

StackInputs and StackOutputs in core/src/program/stack/inputs.rs and core/src/program/stack/outputs.rs are also strong candidates. Their checked path only rejects more than MIN_STACK_DEPTH values, then pads with zeroes. Please keep the current public shape, Copy behavior, and binary encoding unless the refactor proves that changing them is harmless.

Kernel in core/src/program/kernel.rs is a useful second pass. It sorts procedure hashes, rejects duplicates, and caps the length at MAX_NUM_PROCEDURES. nutype can express the length check and a custom validator, but the sort is a domain rule and should stay explicit.

SectionId in crates/mast-package/src/package/section.rs is another small string case. It checks the first character, checks the allowed characters, supports constants, and has transparent serde. This looks close to the nutype string use case.

Ident, ProcedureName, and PathBuf are worth checking, but they should not be the first step. Ident carries a source span and interning behavior. ProcedureName has quote parsing rules. PathBuf has canonical form, component joining, and path rewrite logic. A refactor should only move the small checked string field into nutype if the parser and display behavior stay the same.

The custom Miden binary serde code should stay manual unless a small bridge can call the generated checked constructor. The nutype serde support covers serde, not the repo specific Serializable and Deserializable traits.

I would skip EventName, PackageId, TargetType, Linkage, MastNodeId, and the indexed vector types for now. Some have no validation rule today. Some are enums. Some need context from a forest or container, which does not fit a standalone newtype check.

A good first PR would move only ItemIndex or SectionId. Keep public behavior and the binary format unchanged. Stop there if the diff is not smaller or clearer.

贡献者指南