#replace #mem #state #change #modify

nightly no-std inplace

A container that allows you temporarily take ownership of the stored value

4 releases

0.0.4 Feb 16, 2020
0.0.3 Feb 16, 2020
0.0.2 Feb 16, 2020
0.0.1 Feb 16, 2020

#171 in No standard library

Download history 8/week @ 2020-03-14 12/week @ 2020-03-21 4/week @ 2020-03-28 9/week @ 2020-04-04 24/week @ 2020-04-11 4/week @ 2020-04-18 3/week @ 2020-04-25 1/week @ 2020-05-02 4/week @ 2020-05-23 13/week @ 2020-05-30 4/week @ 2020-06-13 4/week @ 2020-06-27

67 downloads per month

MIT/Apache

8KB
123 lines

travis

inplace

A container that allows you temporarily take ownership of the stored value.

Sometimes you can find yourself in situation when you have mutable reference to some value a, and function kind of a -> a, and you want mutate referenced value with this function. So, you can think, you need some empowered version of mem:replace function, which could be named as inplace and should look like

fn inplace<T>(a: &mut T, f: impl FnOnce(T) -> T);

But such function does not exist! And even further: it cannot exist. So in such situation, you need to change some part of your code: either part, providing mutable reference, either mutating function. The second way is conventional: normally, mutable reference is the only adequate way to express temporary owning, thus instead of FnOnce(T) -> T, it should be FnOnce(&mut T). But in some situations it can be better to stay with FnOnce(T) -> T, and change another part, namely &mut T.

This crate provides special type wrap Inplace<T>, which have same size and memory representation as source type T, but have desired inplace function. So, changing &mut T to &mut Inplace<T> solves the problem.

No runtime deps