Skip to content

corrode/bumpref

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

.bump() method for Arc and Rc that signals cheap refcount cloning

Topics

Resources

License

Stars

Watchers

Forks

Languages