tap icon indicating copy to clipboard operation
tap copied to clipboard

Tap that return result

Open feois opened this issue 1 year ago • 1 comments

Add something like

trait TapResult: Tap {
    fn tap_result<E>(self, f: impl FnOnce(&Self) -> std::result::Result<(), E>) -> std::result::Result<Self, E> {
        f(&self).map(|()| self)
    }
    
    fn tap_result_mut<E>(mut self, f: impl FnOnce(&mut Self) -> std::result::Result<(), E>) -> std::result::Result<Self, E> {
        f(&mut self).map(|()| self)
    }
}

impl<T: Tap> TapResult for T {}

Usecase:

// bar returns Result<(), Error>
let foo = Foo::new().tap_result_mut(|foo| foo.bar())?;

// as opposed to

let mut foo = Foo::new();

foo.bar()?;

let foo = foo;

feois avatar Oct 25 '23 03:10 feois

That’s Pipe.

flying-sheep avatar Feb 23 '24 09:02 flying-sheep