#traits #argument #performance #generic #pattern

no-std take_ref

TakeRef, TakeSlice, and TakeString traits enable treating references/slices and values interchangeably. They can be treated as references/slices. And you can take ownership of the value within with a clone performed only as needed.

2 unstable releases

0.2.0 Sep 15, 2020
0.1.0 Sep 14, 2020

#2959 in Rust patterns

MPL-2.0 license

11KB
81 lines

take_ref

take_ref provides TakeRef, TakeSlice, and TakeString traits which enable treating references/slices and values interchangeably. They can be treated as references/slices. And you can take ownership of the value within with a clone performed only as needed.

An Example

The example take_slice can take in both &[i64] and Vec<i64> instances and refer to them multiple times before finally deciding that it needs to take ownership of a Vec<i64>. At that point if it is a &[i64] a Vec<i64> is constructed from the &[i64]. On the other hand, if it is a Vec<i64> ownership is transferred directly without overhead.

use take_ref::TakeSlice;

fn take_slice(value: impl TakeSlice<i64>) {
    value.as_slice();
    value.as_slice();
    value.take();
}

lib.rs:

take_ref

take_ref provides the TakeRef, TakeSlice, and TakeString traits to enable treating values and their reference/slice types interchangeably until the moment taking ownership is required. If taking ownership is required sometimes but not always, this can eliminate the need to always copy data from reference types.

No runtime deps

Features