← all work

open source · Rust

crementpublished

All four increment/decrement forms for Rust.

n++, ++n, n--, --n with zero unsafe and zero runtime overhead beyond a single += / -=.

context

crement adds all four C-style increment/decrement variants to Rust via declarative macros. Add crement = "0.1" and use crement!(n++), or the named aliases post_inc!, pre_inc!, post_dec!, pre_dec!.

the interesting part

  • Postfix forms capture the old value via Clone::clone; the compiler erases this to a register move for any Copy type, so there is no real overhead
  • Internal temporaries use Span::mixed_site() hygiene: invisible to surrounding code, no shadowing
  • The ++ / -- tokens are distinguished from separate operators by their Joint spacing in the token stream, matching C semantics
  • Clean compile errors when a type is neither Copy nor Clone

outcome

Stable Rust has no specialization, so a single blanket impl<T: Clone> covers both paths and lets LLVM optimize. MSRV 1.71, MIT, published on crates.io. A small surface done exactly right.

A second published crate: small, sharp, and correct down to token spacing and macro hygiene.

next project Brainiac →