These are chat archives for rust-lang/rust

25th
Mar 2017
Gwen Lofman
@glfmn
Mar 25 2017 14:20

I have a simple function that I originally wrote as:

fn solver(l1: f64, l2: f64, x: f64,y: f64) -> f64 {
    let c: f64 = (l1.powi(2) + l2.powi(2) - (x.powi(2) + y.powi(2))) / (2f64*l1*l2);
    // Clamp c to handle edge cases
    match c {
        _ if c >  1f64 =>  1f64,
        _ if c < -1f64 => -1f64,
        _              =>  c,
    }.acos().to_degrees()
}

Is it possible to simplify it to something like:

fn solver(l1: f64, l2: f64, x: f64,y: f64) -> f64 {
    // Clamp c to handle edge cases
    match c @ (l1.powi(2) + l2.powi(2) - (x.powi(2) + y.powi(2))) / (2f64*l1*l2) {
        _ if c >  1f64 =>  1f64,
        _ if c < -1f64 => -1f64,
        _              =>  c,
    }.acos().to_degrees()
}

I get a type mismatch with the compiler: expected f64, found (). What about the match statement causes the type mismatch? Is it that var @ pattern syntax only works inside the match braces?

Denis Lisov
@tanriol
Mar 25 2017 14:38
fn solver(l1: f64, l2: f64, x: f64,y: f64) -> f64 {
    // Clamp c to handle edge cases
    match (l1.powi(2) + l2.powi(2) - (x.powi(2) + y.powi(2))) / (2f64*l1*l2) {
        c if c >  1f64 =>  1f64,
        c if c < -1f64 => -1f64,
        c              =>  c,
    }.acos().to_degrees()
}
Gwen Lofman
@glfmn
Mar 25 2017 14:38
Thanks!
Denis Lisov
@tanriol
Mar 25 2017 14:43
...or even
fn solver(l1: f64, l2: f64, x: f64,y: f64) -> f64 {
    // Clamp c to handle edge cases
    let c = (l1.powi(2) + l2.powi(2) - (x.powi(2) + y.powi(2))) / (2f64*l1*l2);
    c.max(-1).min(1).acos().to_degrees()
}
Gwen Lofman
@glfmn
Mar 25 2017 14:43
Is the latter more idomatic?
Denis Lisov
@tanriol
Mar 25 2017 14:48
Not sure, it seems slightly confusing to me due to the method names.
Gwen Lofman
@glfmn
Mar 25 2017 14:52
    ((l1.powi(2) + l2.powi(2) - (x.powi(2) + y.powi(2))) / (2f64*l1*l2))
        .min(1.0).max(-1.0) // Clamp to handle edge cases
        .acos().to_degrees()
makes it more readable.
Joonas Koivunen
@koivunej
Mar 25 2017 16:38
I'd like to define a trait Pool<Something> which would have ability to fn lease<'a>(&'a self) -> Self::LeaseGuard + 'a (to lease something for less than it's own lifetime, and then be returned back on drop) but I'm not having any luck, as the + 'a is not allowed in the return type, but at the same time I do not think this should be trait level lifetime either. any ideas or am I completly off-track?
Jacob Kiesel
@Xaeroxe
Mar 25 2017 18:43
I want to add a clamp function to int_impl, uint_impl and float_impl in Rustc. I've downloaded the rustc source code and was writing out my addition to it for a PR when I saw the "stable" attributes for the other functions and it occurred to me I might not be doing this right. Would adding a function to the primitive types (i8, u8, f32, i32, u32) require creating an RFC and having it approved prior to making the PR?
Jacob Kiesel
@Xaeroxe
Mar 25 2017 19:06
I guess a better question might be: For a new PR that hasn't been approved yet what should be in the stable attribute?
Jacob Kiesel
@Xaeroxe
Mar 25 2017 19:25
Xaeroxe/rust@a3bbc84 it's a pretty tiny change, this commit encompasses the entirety of what I'd be sending in my PR.
Aleksey Kladov
@matklad
Mar 25 2017 19:29
@Xaeroxe I think discussing it on the Internals forum might be a good idea: https://internals.rust-lang.org/
Jacob Kiesel
@Xaeroxe
Mar 25 2017 19:39
Thanks!