inplace

Crates.ioinplace
lib.rsinplace
version0.0.5
sourcesrc
created_at2020-02-16 00:36:12.144667
updated_at2020-08-27 22:27:27.55313
descriptionA container that allows you temporarily take ownership of the stored value.
homepage
repositoryhttps://github.com/A1-Triard/inplace
max_upload_size
id209614
size19,439
Warlock (A1-Triard)

documentation

https://docs.rs/inplace/

README

USE replace_with INSTEAD!

travis

inplace

USE replace_with INSTEAD!

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.

Commit count: 14

cargo fmt