tap icon indicating copy to clipboard operation
tap copied to clipboard

A version of pipe that gets a self and returns nothing consuming object

Open ta3pks opened this issue 1 year ago • 1 comments

Often when dealing with locks or something I tend to just do lock.tap_mut(...) which gets a &mut self however this triggers must use warnings.

Alternatively i use pipe to solve this issue however by design it is for mapping something to something else so this would be abusing pipe.

I suggest adding a method lets say with which would consume the value and return nothing

lock.with(|mut lock|{...}) I can create a pr if maintainers approve the naming and the idea

PS. Thanks for this amazing library which became basically a utility for all my rust projects

ta3pks avatar Mar 15 '23 08:03 ta3pks

I've done something like this in my own projects and I called it "ignore". Basically the whole trait is like:

trait Ignored: Sized {
    fn ignore(self) {}
}
impl<T> Ignored for T{}

Alternatively, if you wanted to do some side effect, you could also implement another trait like:

trait Effect: Sized {
    fn effect<F>(self, f: F)
    where F: FnOnce(Self)
    {
        f(self);
    }
}
impl<T> Effect for T{}

th3oth3rjak3 avatar Apr 17 '24 02:04 th3oth3rjak3