#util-lib #hobby #no-dependency

no-std disuse

The way to notify the implementation which return value is disuse

2 releases

Uses new Rust 2021

0.0.2 Mar 4, 2022
0.0.1 Dec 25, 2021

#494 in Rust patterns

Download history 22/week @ 2022-06-03 8/week @ 2022-06-10 14/week @ 2022-06-17 15/week @ 2022-06-24 10/week @ 2022-07-01 11/week @ 2022-07-08 12/week @ 2022-07-15 15/week @ 2022-07-22 14/week @ 2022-07-29 26/week @ 2022-08-05 20/week @ 2022-08-12 17/week @ 2022-08-19 10/week @ 2022-08-26 25/week @ 2022-09-02 26/week @ 2022-09-09 14/week @ 2022-09-16

81 downloads per month
Used in 3 crates (via const-array-attrs)

MIT/Apache

4KB

Latest Release pipeline status coverage report

Description

The way to notify the implementation which return value is disuse.

Usage

When an algorithm can calculate multiple useful values simultaneously, you will write like the following code.

pub fn some_algorithm(x: f64) -> (f64, f64, f64) {
    let mut r1 = 0f64; let mut r2 = 0f64; let mut r3 = 0f64;

    for _ in 0..1000 { // ... some heavy calculations here ...
        r1 += x;
        r2 += x * 2.0;
        r3 += x * 3.0;
    }
    (r1, r2, r3)
}

// But your users are not always need all return values.
// Yes, we can ignore the return values, but calculations done.

let (a, _, _) = some_algorithm(4.0);

Above example code can be rewritten using Disuse as follows,

use disuse::Disuse;

pub fn some_algorithm<R1, R2, R3>(x: f64) -> (R1, R2, R3)
where
    R1: From<f64>, R2: From<f64>, R3: From<f64>
{
   let mut r1 = 0f64; let mut r2 = 0f64; let mut r3 = 0f64;

   for _ in 0..1000 { // ... heavy calculations here ...
       r1 += x;
       r2 += x * 2.0; // When user call this function like below,
       r3 += x * 3.0; // we can expect the compiler eliminate this two line, right?
   }
   (r1.into(), r2.into(), r3.into())
}

let (a, _, _): (f64, Disuse, Disuse) = some_algorithm(4.0);

It's better than before version.

(But return type should implement Clone trait.)

If the unit type (()(unit)) implemented From trait like as follows,

impl<T> From<T> for () {
    fn from(_: T) -> () { () }
}

above example code could be written more smart.

pub fn some_algorithm<R1, R2, R3>(x: f64) -> (R1, R2, R3)
where
    R1: From<f64>, R2: From<f64>, R3: From<f64>
{
   let mut r1 = 0f64; let mut r2 = 0f64; let mut r3 = 0f64;

   for _ in 0..1000 { // ... heavy calculations here ...
       r1 += x;
       r2 += x * 2.0; // When user call this function like below,
       r3 += x * 3.0; // we can expect the compiler eliminate this two line, right?
   }
   (r1.into(), r2.into(), r3.into())
}

let (a, _, _): (f64, (), ()) = some_algorithm(4.0);

It's just unrealizable dream...

No runtime deps