Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Coding Guidelines

This section describes coding and collaboration conventions for the Asterinas project. These guidelines aim to keep code clear, consistent, maintainable, correct, and efficient.

For the underlying philosophy, principles, and quality criteria for individual guidelines, see How Guidelines Are Written.

The guidelines are organized into the following pages:

  • General Guidelines — Language-agnostic guidance for naming, comments, layout, formatting, and API design.
  • Rust Guidelines — Rust-specific guidance for naming, language items, and cross-cutting topics.
  • Git Guidelines — Commit hygiene and pull-request conventions.
  • Testing Guidelines — Test behavior, assertions, regression policy, and cleanup.
  • Assembly Guidelines.S and global_asm! conventions for sectioning, function metadata, labels, and alignment.

The guidelines represent the desired state of the codebase. If you find conflicting cases in the codebase, you are welcome to fix the code to match the guidelines.

Index

CategoryGuidelineShort Name
GeneralBe descriptivedescriptive-names
GeneralBe accurateaccurate-names
GeneralEncode units and important attributes in namesencode-units
GeneralUse assertion-style boolean namesbool-names
GeneralPrefer semantic line breakssemantic-line-breaks
GeneralExplain why, not whatexplain-why
GeneralDocument design decisionsdesign-decisions
GeneralCite specifications and algorithm sourcescite-sources
GeneralOne concept per fileone-concept-per-file
GeneralOrganize code for top-down readingtop-down-reading
GeneralGroup statements into logical paragraphslogical-paragraphs
GeneralFormat error messages consistentlyerror-message-format
GeneralStick to familiar conventionsfamiliar-conventions
GeneralHide implementation detailshide-impl-details
GeneralValidate at boundaries, trust internallyvalidate-at-boundaries
RustFollow Rust CamelCase and acronym capitalizationcamel-case-acronyms
RustEnd closure variables with _fnclosure-fn-suffix
RustIntroduce explaining variablesexplain-variables
RustUse block expressions to scope temporary stateblock-expressions
RustUse checked or saturating arithmeticchecked-arithmetic
RustMinimize nestingminimize-nesting
RustKeep functions small and focusedsmall-functions
RustAvoid boolean argumentsno-bool-args
RustUse types to enforce invariantsrust-type-invariants
RustPrefer enum over trait objects for closed setsenum-over-dyn
RustEncapsulate fields behind gettersgetter-encapsulation
RustFollow RFC 1574 summary line conventionsrfc1574-summary
RustEnd sentence comments with punctuationcomment-punctuation
RustWrap identifiers in backticksbacktick-identifiers
RustDo not disclose implementation details in doc commentsno-impl-in-docs
RustAdd module-level documentation for major componentsmodule-docs
RustJustify every use of unsafejustify-unsafe-use
RustDocument safety conditionsdocument-safety-conds
RustDeny unsafe code in kernel/deny-unsafe-kernel
RustReason about safety at the module boundarymodule-boundary-safety
RustDefault to the narrowest visibilitynarrow-visibility
RustUse workspace dependenciesworkspace-deps
RustSuppress lints at the narrowest scopenarrow-lint-suppression
RustUse #[expect(dead_code)] with restraintexpect-dead-code
RustPrefer functions over macrosmacros-as-last-resort
RustEstablish and enforce a consistent lock orderlock-ordering
RustNever do I/O or blocking operations while holding a spinlockno-io-under-spinlock
RustDo not use atomics casuallycareful-atomics
RustCritical sections must not be split across lock boundariesatomic-critical-sections
RustUse debug_assert for correctness-only checksdebug-assert
RustPropagate errors with ?propagate-errors
RustUse log crate macros exclusivelylog-crate-only
RustChoose appropriate log levelslog-levels
RustUse RAII for all resource acquisition and releaseraii
RustAvoid O(n) algorithms on hot pathsno-linear-hot-paths
RustMinimize unnecessary copies and allocationsminimize-copies
RustNo premature optimization without evidenceno-premature-optimization
GitWrite imperative, descriptive subject linesimperative-subject
GitOne logical change per commitatomic-commits
GitSeparate refactoring from featuresrefactor-then-feature
GitKeep pull requests focusedfocused-prs
TestingAdd regression tests for every bug fixadd-regression-tests
TestingTest user-visible behavior, not internalstest-visible-behavior
TestingUse assertion macros, not manual inspectionuse-assertions
TestingClean up resources after every testtest-cleanup
AssemblyUse the correct section directiveasm-section-directives
AssemblyPlace code-width directives after the section definitionasm-code-width
AssemblyPlace attributes directly before the functionasm-function-attributes
AssemblyAdd .type and .size for Rust-callable functionsasm-type-and-size
AssemblyUse unique label prefixes to avoid name clashesasm-label-prefixes
AssemblyPrefer .balign over .alignasm-prefer-balign