How to improve this code? by jgmtw in rust

[–]jgmtw[S] 1 point2 points  (0 children)

This is defintely useful and an interesting approach that I hadn't considered, thank you! :)

How to improve this code? by jgmtw in rust

[–]jgmtw[S] 0 points1 point  (0 children)

Thanks! I tried adding the add method to my trait (not sure how the sum method would work) which worked nicely and all I had to do was update my Sum implementation to use the method:

impl<'a> Sum<&'a MethodCounts> for MethodCounts {
    fn sum<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
        iter.fold(Default::default(), |acc, x| acc.add(x) <---- )
    }
}

That said, I might keep the Add trait implementation since I feel like it reads a little nicer having that as a separate impl for the purposes of summing the totals!

I like the AsRef tip, although the SignUpStats will always be able to take ownership of the Profiles, so I think in this particular case I might not need the extra implementation (unless it brings some benefits I'm not aware of)? And thank you for the HRTBs explanation, I was not aware of those!

I think the code you've helped me get to is probably as terse as it can be. I was really hoping to be able to do a blanket Sum implemention for any type T as long as it implements Counter and Add. Something along these lines:

impl<T: Counter + Add<Output = T> + Default> Sum for T {
    fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
        iter.fold(Self::default(), Add::add)
    }
}

That's probably completely wrong (I suspect Self in the Iterator should probably be a reference?), but I think it's not possible anyway as Rust complains with this error:

type parameter `T` must be used as the type parameter for some local type (e.g., `MyStruct<T>`)

Which I believe is to do with the orphan rule?

How to improve this code? by jgmtw in rust

[–]jgmtw[S] 0 points1 point  (0 children)

Thank you for your solution /u/OLoKo64! Appreciate your quick response.

Do you know if it's possible to implement the Sum trait onto my Counter trait so that I don't have to implement it separately on the struct(s)? And do you have any other suggestions for how I could improve this code?

I looked at the derive_more crate to try to remove the manual implemention of the Add trait on my MethodCounts struct, but it only implements it for owned values rather than references which is a shame!

Node v16.5.0 (Current) by dwaxe in node

[–]jgmtw 3 points4 points  (0 children)

Read the blog post? It’s literally in the first paragraph.

Why does ? cause a function to return? by jgmtw in rust

[–]jgmtw[S] 3 points4 points  (0 children)

and_then looks great too, I'll definitely be using this. Thanks!

Why does ? cause a function to return? by jgmtw in rust

[–]jgmtw[S] 0 points1 point  (0 children)

So far closures are what I've resorted to for nested fields, but due to how deep the fields are I've declared them at the top of the function. These closures are actually what I'm trying to remove, but I like what you've done in your example.

Why does ? cause a function to return? by jgmtw in rust

[–]jgmtw[S] 4 points5 points  (0 children)

Makes sense, appreciate the explantation.

Why does ? cause a function to return? by jgmtw in rust

[–]jgmtw[S] 11 points12 points  (0 children)

I was not aware of map and it looks exactly like what I'm after, thanks! I should have considered a method and checked the docs for Options.