#update #cell #generic

no-std update_cell

A Cell<Option<T>> that you can update

1 unstable release

0.1.0 Jul 21, 2023

#2953 in Rust patterns

Download history 1/week @ 2024-03-26 15/week @ 2024-04-02 7/week @ 2024-04-09 30/week @ 2024-04-16 8/week @ 2024-04-23 2/week @ 2024-05-21 29/week @ 2024-05-28 52/week @ 2024-06-04 22/week @ 2024-06-11 10/week @ 2024-06-18

91 downloads per month

MPL-2.0 license

5KB
54 lines

update_cell

A Cell<Option<T>> that you can update

Why would I need this?

Cell::update is currently experimental. And it only supports types that are Copy. So if you want to store and modify something that is neither Copy nor has a Default (eg. a builder), this crate might be useful.

Usage

Add this to your Cargo.toml:

[dependencies]
update_cell = "0.1"

And if you have a struct, you can put it inside and modify it:

use update_cell::UpdateCell;

struct MySuperFancyStruct {
    inner: bool
}

impl MySuperFancyStruct {
    fn new() -> Self {
        Self { inner: false }
    }

    fn toggle(mut self) -> Self {
        self.inner = !self.inner;
        self
    }
}

let mut cell = UpdateCell::new(MySuperFancyStruct::new());
cell.update(|s| s.toggle());

License: MPL-2.0


lib.rs:

A Cell<Option<T>> that you can update

Why would I need this?

Cell::update is currently experimental. And it only supports types that are Copy. So if you want to store and modify something that is neither Copy nor has a Default (eg. a builder), this crate might be useful.

Usage

Add this to your Cargo.toml:

[dependencies]
update_cell = "0.1"

And if you have a struct, you can put it inside and modify it:

use update_cell::UpdateCell;

struct MySuperFancyStruct {
    inner: bool
}

impl MySuperFancyStruct {
    fn new() -> Self {
        Self { inner: false }
    }

    fn toggle(mut self) -> Self {
        self.inner = !self.inner;
        self
    }
}

let mut cell = UpdateCell::new(MySuperFancyStruct::new());
cell.update(|s| s.toggle());

No runtime deps