tempref 0.1.0

This crate provides a type whose value remains unchanged even when accessed through a mutable reference.
Documentation
  • Coverage
  • 100%
    42 out of 42 items documented3 out of 38 items with examples
  • Size
  • Source code size: 33.02 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 3.31 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 13s Average build duration of successful builds.
  • all releases: 12s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • yua134

TempRef

Crates.io Docs.rs Downloads

overview

This crate provides a type whose value remains unchanged even when accessed through a mutable reference.

features

  • Automatically reset when the mutable reference is dropped
  • Works in both single-threaded and multi-threaded contexts
  • no_std compatible (only the unsync module)

feature flags

Module Characteristics Feature Flags
unsync !Sync, !Send type supports no_std default, all, no_std, unsync
mutex Sync, Send type using std::sync::Mutex default, all, mutex
rwlock Sync, Send type using std::sync::RwLock default, all, rwlock

usage

use tempref::unsync::Temp;

let data = vec![0;128];
let workspace = Temp::new(data, |d| {d.fill(0);});

assert_eq!(*workspace.borrow(), vec![0;128]);

{
    // vec.clone() is unnecessary, so repeated allocations are avoided (as long as it’s not reallocated).
    // This helps keep your program lightweight.
    let mut guard = workspace.borrow_mut();
    guard.fill(1);
    assert_eq!(*guard, vec![1;128]);
} // d.fill(0) is called here.

assert_eq!(*workspace.borrow(), vec![0;128]);

crate info