#refcell #no-alloc

no-std own_or_borrow

Unified references to owned or RefCell borrowed data

1 unstable release

0.1.0 Jun 30, 2024

#1744 in Embedded development

EUPL-1.2

15KB
317 lines

Own or borrow your data.

GitHub Workflow Status codecov

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

~155KB