tempref

Crates.iotempref
lib.rstempref
version0.3.0
created_at2025-09-28 05:00:25.858629+00
updated_at2025-10-10 07:39:06.226776+00
descriptionThis crate provides a type whose value remains unchanged even when accessed through a mutable reference.
homepage
repositoryhttps://github.com/yua134/tempref
max_upload_size
id1857981
size46,504
yua (yua134)

documentation

README

TempRef

Crates.io Docs.rs CI 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)
  • no dependencies

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]);

{
    let mut guard = workspace.borrow_mut();
    guard.pop();
    assert_eq!(*guard, vec![0;127]);
} // The length is not reset because the closure only resets the payload.
assert_eq!(*workspace.borrow(), vec![0;127]);

crate info

Commit count: 0

cargo fmt