1 unstable release
0.1.0 | Jun 30, 2024 |
---|
#1664 in Embedded development
15KB
317 lines
Own or borrow your data.
This crate provides the OwnOrBorrow
type that wraps either owned data or a RefCell
borrowed reference to it. Think Cow
for borrowing.
Examples
You can create an OwnOrBorrow
from an owned value:
use own_or_borrow::OwnOrBorrow;
fn example() {
let mut value = OwnOrBorrow::own(42);
assert_eq!(value.borrow().as_ref(), &42);
assert_eq!(value.borrow_mut().as_mut(), &mut 42);
}
You can create an OwnOrBorrow
from a RefCell
and treat it the same way:
use own_or_borrow::OwnOrBorrow;
use core::cell::RefCell;
fn example() {
let refcell = RefCell::new(42);
let mut value = OwnOrBorrow::from(refcell);
assert_eq!(value.borrow().as_ref(), &42);
assert_eq!(value.borrow_mut().as_mut(), &mut 42);
}
Dependencies
~160KB