Trait devela::code::Also

source ·
pub trait Also: Sized {
    // Provided methods
    fn also_mut<F: FnOnce(&mut Self)>(self, f: F) -> Self { ... }
    fn also_ref<F: FnOnce(&Self)>(self, f: F) -> Self { ... }
}
Expand description

Represents a type that you can apply arbitrary functions to.

Useful for when a method doesn’t return the receiver but you want to apply several of them to the object.

It assumes that each function in the chain modifies the value by exclusive reference and returns the modified value.

§Examples

use devela::code::Also;

let v = vec![3, 2, 1, 5]
    .also_mut(|v| v.sort())
    .also_ref(|v| assert_eq![v, &[1, 2, 3, 5]])
    .also_mut(|v| v.push(7));
assert_eq![v, vec![1, 2, 3, 5, 7]];

Provided Methods§

source

fn also_mut<F: FnOnce(&mut Self)>(self, f: F) -> Self

Applies a function which takes the parameter by exclusive reference, and then returns the (possibly) modified owned value.

Similar to apply, but instead of returning self directly from f, since it has a different signature, returns it indirectly.

source

fn also_ref<F: FnOnce(&Self)>(self, f: F) -> Self

Applies a function which takes the parameter by shared reference, and then returns the (possibly) modified owned value.

Similar to apply, but instead of returning self directly from f, since it has a different signature, returns it indirectly.

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<T: Sized> Also for T