offer-cell

Crates.iooffer-cell
lib.rsoffer-cell
version0.1.2
sourcesrc
created_at2024-08-05 19:46:44.117482
updated_at2024-08-06 18:33:13.818021
descriptionA rust library that defines a pattern for providing a reference to stored data, and optionally transferring ownership of that data.
homepagehttps://github.com/rhedgeco/offer-cell
repositoryhttps://github.com/rhedgeco/offer-cell
max_upload_size
id1326444
size7,445
Ryan Hedgecock (rhedgeco)

documentation

README

Offer Cell

A rust library that defines a pattern for providing a reference to stored data, and optionally transferring ownership of that data.

Usage

Initialization

// a cell may be created
let cell = OfferCell::new(42);

// or initialzed as empty
let empty = OfferCell::empty();

Accessing Data

// access the item as a reference
match cell.item() {
    Some(value) = (), // do something with the value
    None => (), // returns none if there is no item
}

// access the item as a mutable reference
match cell.item_mut() {
    Some(value) = (), // do something with the value
    None => (), // returns none if there is no item
}

Offering Data

What sets this apart, is the data within the cell can be "offered"

// if the cell contains an item, it can be offered
let offered = cell.offer() {
    Some(offered) => offered,
    None => return,
};

// the offered item implements Deref and DerefMut
assert_eq!(offered.deref(), &42);

// if nothing else is done with the offered item,
// the data will stay in the cell for later

// alternatively the offering can be consumed
// this leaves nothing in the cell, and takes ownership of the data
let data = offered.take();
Commit count: 0

cargo fmt