WrapIt
wrapit is a small Rust library that wraps any variable using interior mutability to allow mutation through shared references.
This crate provides a generic Wrapper type for single-threaded use, which allows:
- Mutable access to a value through a shared reference.
- Convenient cloning and sharing via Arc.
- Read, reset, and functional mapping of the inner value.
Note: This crate uses RefCell for interior mutability. It is not thread-safe. Concurrent access across threads will not compile.
Installation
You can install wrapit either from Crates.io or directly from the Git repository.
Installing from Crates.io
- Open your
Cargo.tomlfile. - Add
wrapitto your dependencies:
[]
= "0.1.0"
OR do;
from your project folder or directory.
Installing directly from Git
- Open your
Cargo.tomlfile. - Add
wrapitas a dependency using the Git repository URL:
[]
= { = "https://github.com/2teez/wrapit.git" }
- In your Rust code, import the wrapper:
use Wrapper;
Description
wraptit - Simple wraps any type and using interior mutability makes using the variable like in javascript or python without specifying mut to the wrapper variable, however using the function reset and get, the values in the variable could be reset and gotten.
const langs = ;
langs.;
console.log;
However in rust, you will have to use the mut keyword with the variable like so:
let mut langs = vec!;
langs.push;
println!;
using wrapit, does this:
use Wrapper;
let langs = vec!; // immutable
let wrapper = new;
// changed the variable content without the `mut` keyword
wrapper.reset;
println!;
wrait Methods
- new
fn new(data: T) -> Self
- Creates a new Wrapper containing data.
- Allocates the value inside RefCell and wraps it in Arc.
- No runtime panic.
Example:
let wrapper = new;
- reset
fn reset(&self, ndata: T) -> &Self
- Replaces the inner value with ndata.
- Returns a reference to self for method chaining.
- Panics at runtime if any active borrow exists (e.g., if .borrow() has been called and not dropped).
Example:
wrapper.reset;
- get
fn get(&self) -> T
- Returns a clone of the inner value.
- Uses immutable borrow (borrow()) internally.
- Panics if a mutable borrow is active.
Example:
let value = wrapper.get;
- borrow
fn borrow(&self) -> std::cell::Ref<'_, T>
- Returns a Ref, a smart pointer for immutable access.
- Automatically releases the borrow when dropped.
- Panics if a mutable borrow is active.
Example:
let value_ref = wrapper.borrow;
println!;
- map
fn map<F, U>(&self, f: F) -> U where F: FnOnce(&T) -> U,
- Applies a function f to the inner value.
- Returns the function’s result.
- Panics if a mutable borrow exists while calling this method.
Example:
let len = wrapper.map;
Summary of Runtime Panics
| Method | Cause of Panic |
|---|---|
| reset() | Active immutable borrow exists |
| get() | Active mutable borrow exists |
| borrow() | Active mutable borrow exists |
| map() | Active mutable borrow exists |
- These panics are enforced at runtime by RefCell.
Usage Example
use Wrapper;
let w = new;
// Read value
assert_eq!;
// Mutate value
w.reset;
assert_eq!;
// Borrow inner value
let val = w.borrow;
println!;
// Map function
let len = w.map;
assert_eq!;
Notes
- This Wrapper is not thread-safe (YET). The thread-safe branch would be added in later version.
- All borrows are checked at runtime using RefCell.
- Use .borrow() and .borrow_mut() carefully to avoid panics.
- Cloning the Wrapper shares the same underlying value.