| Crates.io | rco-cell |
| lib.rs | rco-cell |
| version | 0.1.0 |
| created_at | 2024-03-02 00:12:01.170124+00 |
| updated_at | 2024-03-02 00:12:01.170124+00 |
| description | Wrapper for Rc |
| homepage | https://github.com/AlexanderSchuetz97/rco-cell |
| repository | |
| max_upload_size | |
| id | 1159332 |
| size | 47,596 |
Wrapper for Rc<RefCell<Option<T>>> and its weak variant.
Includes various utilities for common operations usually performed on such a datastructure.
pub fn main() {
let cell : RcOCell<u8> = RcOCell::from_value(1u8);
//The usual RefCell stuff...
let borrowed : Ref<u8> = cell.borrow();
//...
drop(borrowed);
let borrowed_mut : RefMut<u8> = cell.borrow_mut();
//...
drop(borrowed_mut);
}
Following functions are provided as part of RcoCell and WeakRcoCell:
In general all functions prefixed "try" will not panic. All functions without this prefix may panic depending on what they do.
borrow and borrow_mut
RefCelltry_borrow and try_borrow_mut
RefCell but error type is an enum.Those calls will either panic or fail if the cell is empty. The normal rust borrowing rules apply: only 1 mutable borrow or n normal borrows. If the borrowing rules are violated at runtime then either panic or failure occurs.
set and try_set
replace and try_replace
clear and try_clear
get_and_clear and try_get_and_clear
compute and try_compute
compute_if_present and try_compute_if_present
compute_if_absent
swap and try_swap
RefCell::swapget_and_clone and try_get_and_clone
if_present_mut, if_present, try_if_present and try_if_present_mut
map, map_mut, try_map, try_map_mut
Fn(&T) -> X or Fn(&mut T) -> X
Option<X> or Result<Option<X>, RcOCellError>T can convert to RcOCell<T> via intoRcOCell<Vec<T>> can convert to Vec<T> via try_intoRcOCell<T> can convert to Rc<RefCell<Option<T>>> via intoRc<RefCell<Option<T>>> can convert to RcOCell<T> via intoRcOCell<T> can convert to WeakRcOCell<T> via into
downgrade method also exists just like Rc::downgradeWeakRcOCell<T> can convert to RcOCell<T> via try_into
upgrade method also exists just like Rc::upgradeRcOCell<T> can convert to Option<T> via try_intoRcOCell<T> can convert to Result<Option<T>, RcOCellError> via intoRefCell borrow methods can convert to RcOCellError via into or the ? operator.from_value and from
From<T> traitfrom_option
new
downgrade and upgrade
clone
Rc::clone.pub fn main() {
let cell : RcOCell<u8> = RcOCell::from_value(1u8); //RcOCell::new() creates a empty cell.
//Remove the value from the cell
let old_value : Option<u8> = cell.clear(); //old value would be 1u8.
let borrow_result : Result<u8, RcOCellError> = cell.try_borrow();
if borrow_result.is_err() {
//Would be error because cell is empty.
//If you want to handle the RcOCellError error, it's an enum.
//Can be handled like this:
match borrow_result.unwrap_err() {
RcOCellError::NoValue => {println!("No value present")},
RcOCellError::BorrowError(_) => {}, //Won't happen in this case
RcOCellError::Dropped => {}, //Won't happen in this case
}
}
//Now cell has value 2. old_value is None.
let old_value : u8 = cell.set(2u8);
let borrowed : Ref<u8> = cell.borrow();
//set would panic, because the value is still borrowed, try_set will fail with RcOCellError::BorrowError
let try_set_result : Result<u8, RcOCellError> = cell.try_set(4u8);
drop(borrowed);
//Now it will work!
let try_set_result : Result<u8, RcOCellError> = cell.try_set(4u8);
//2u8 is the old value, 4u8 is now in the cell
let old_value : u8 = try_set_result.unwrap();
let current_value : u8 = cell.get_and_clone(); //Only works for all T that implement Clone trait, in this case 4u8
let current_value : u8 = cell.get_and_clear(); //Cell is empty again after this call. current_value 4u8
//Another call to get_and_clear would panic as cell is empty.
let get_result : Result<u8, RcOCellError> = cell.try_get_and_clear();
//Result is once again RcOCellError::NoValue Error
}