ov
The 'ov' crate provides a collection of traits that allow you to chain off of anything. Each trait has a single method with the same name as the trait (but in snake case).
Over, OverRef, and OverMut
are each of self, &self, and &mut self, and the callback receives that same value.
They are implemented for all types.
OverDeref and OverDerefMut are implemented
for types which have Deref and DerefMut implementations. They both borrow the receiver,
and pass a reference of the Deref::target to the callback.
Usage
You may either wildcard import it, or import specific traits. New items will not be added in semver compatible versions.
use *;
or
use Over;
Examples
A common use for this is with unary enum or struct constructors. These would otherwise be multiple statements with variables, or nested parenthesis.
use *;
use Error;
use ;
assert_eq!;
// A helper is needed as `.over(Box::new)` always creates a Box<IoError>
The .over_mut method can be used to perform some mutations on an arbitrary value,
including a field of a struct.
use *;
let mut s = S ;
s.field.over_mut;
assert_eq!;
The over_deref and over_deref_mut methods can be useful if you want to use a function
that takes e.g. &str and you have a String.
let s = Stringfrom;
// Note: this would fail if `my_str` were `String` or `&String`
let len = s.over_deref;
// Can be shortened to this
let len = s.over_deref;
assert_eq!;
Another case for this is extracting the value from a mutable reference. For this, we
can use std::mem::take or std::mem::replace. In the following example we have
to use replace because Command doesn't impl Default.
use Command;
let command = new
.arg // returns &mut Command
.arg // returns &mut Command
.over; // returns Command
;
License: MIT