bumpref 0.1.0

Explicit .bump() method for Arc and Rc that signals cheap refcount cloning
Documentation
  • Coverage
  • 100%
    3 out of 3 items documented2 out of 3 items with examples
  • Size
  • Source code size: 18.02 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 671.36 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 24s Average build duration of successful builds.
  • all releases: 24s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • corrode/bumpref
    3 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • mre

bumpref

A lightweight Rust crate that provides a bump() method for reference-counted pointers. This is an explicit alias for clone() that makes it clear the operation is cheap (just incrementing a reference count).

Usage

Add this to your Cargo.toml:

[dependencies]
bumpref = "0.1"

Then use it in your code:

extern crate bumpref;

use bumpref::Bump;
use std::sync::Arc;
use std::rc::Rc;

fn main() {
    // For Arc
    let arc = Arc::new(42);
    let arc_bumped = arc.bump(); // same as Arc::clone(&arc)

    // For Rc
    let rc = Rc::new("hello");
    let rc_bumped = rc.bump();

    // Also works with Weak references
    let weak = Arc::downgrade(&arc);
    let weak_bumped = weak.bump();
}

Supported Types

  • Arc<T> and std::sync::Weak<T>
  • Rc<T> and std::rc::Weak<T>

All implementations work with T: ?Sized, so they support trait objects and slices.

Why?

When code contains many .clone() calls on reference-counted types, it's not immediately obvious whether you're doing an expensive deep clone or just bumping a refcount. Using .bump() makes the intent explicit and signals to readers that the operation is O(1).

Inspiration

This came out of a conversation with Richard Feldman on the 'Rust in Production' podcast about how Zed uses reference-counted pointers extensively in their codebase and how Richard was initially concerned that cloning datastructures might be expensive until learning that all those clones were just bumping reference counts. The interview is here.