Crates.io | inplace |
lib.rs | inplace |
version | 0.0.5 |
source | src |
created_at | 2020-02-16 00:36:12.144667 |
updated_at | 2020-08-27 22:27:27.55313 |
description | A container that allows you temporarily take ownership of the stored value. |
homepage | |
repository | https://github.com/A1-Triard/inplace |
max_upload_size | |
id | 209614 |
size | 19,439 |
USE replace_with
INSTEAD!
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.