Trait libera::devela::prelude::Also

source ·
pub trait Also: Sized {
    // Provided methods
    fn also_mut<F>(self, f: F) -> Self
       where F: FnOnce(&mut Self) { ... }
    fn also_ref<F>(self, f: F) -> Self
       where F: FnOnce(&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.

use devela::ops::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>(self, f: F) -> Selfwhere F: FnOnce(&mut 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>(self, f: F) -> Selfwhere F: FnOnce(&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.

Implementors§

source§

impl<T> Also for T