Crate own_or_borrow

Crate own_or_borrow 

Source
Expand description

§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.

§no_std vs. std

To use the crate in a no_std context, disable the std feature.

§Crate features

  • std - Enables std; disabling enters no_std mode.
  • defmt - Enables deferred formatting support via the defmt crate.

§Examples

You can create an OwnOrBorrow from an owned value:

use own_or_borrow::OwnOrBorrow;

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;

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

Enums§

OwnOrBorrow
A type that provides either an owned value or RefCell borrowed reference to a value.
Reference
A reference to borrowed or owned data.
ReferenceMut
A mutable reference to borrowed or owned data.
TryIntoError
Errors from TryInto traits and related.