These are chat archives for rust-lang/rust

23rd
Nov 2018
laurent bernabé
@loloof64
Nov 23 2018 00:30
Hi @1tgr thank you for your help : it seems to me that what I tried to do is not allowed. The board model that I have in Rust side is not meant to have its methods called from JS directly, but rather I have to build a function inside the Rust Rpc that call the board method for me, and that I launch an Rpc command from the Javascript side to the method of the Rust Rpc of interest. This way it worked. The two sources files I've put a link to just above are the result of this success.
Kelly Thomas Kline
@kellytk
Nov 23 2018 02:06
Can https://pastebin.com/jP39N6QN be fixed without line 2 for id in self.users.keys() having cloned() added to it? This function is very hot and the allocation cuts ~20% from overall app performance. Ideally the lifetime of id could follow it into the Future and cooperate with the borrow checker
Tim Robinson
@1tgr
Nov 23 2018 07:22
What's the type of self.users?
It would help to put id.to_string() outside the future, I think that's why id is being borrowed longer than it needs to
trsh
@trsh
Nov 23 2018 09:16
Can I somhave tell macro to, for example, use std::cmp;
Without adding it to code, that calls the macro?
Tim Robinson
@1tgr
Nov 23 2018 10:03
Like your macro generates code that uses cmp, and you want to make sure the file that uses the macro has use std::cmp; at the top?
You can use std::cmp; within a single function, it doesn't have to apply to the whole file
You can have the macro generate a mod with use at the top
Often I do it by writing std::cmp::whatever inside the macro everywhere it's needed
Kelly Thomas Kline
@kellytk
Nov 23 2018 10:25
Thanks @1tgr
David O'Connor
@David-OConnor
Nov 23 2018 16:38

Hey dudes - is it possible to pass types to functions? Eg: a struct S is defined in a module using an API. A fn f that acts upon instances of S is defined in the same module. This module invokes a fn call in a library, in order to pass these ... How can I properly set up the parameters to accept arbitrary S and f?

I've tried several approaches involving closures, and generics, with no success.

Tim Robinson
@1tgr
Nov 23 2018 16:45
Can you write pseudocode for what you want to achieve?
Are you writing the module, or the library?
David O'Connor
@David-OConnor
Nov 23 2018 16:46
The lib... standby
David O'Connor
@David-OConnor
Nov 23 2018 16:53
pub mod ApiLib {
    pub fn run<S>(s: S, f: &Fn(S) -> S) {
        // ...
    }
}

pub mod UserMod {
    pub struct S {
        val: i8
    }

    pub fn f(s: S) -> S {
        S {val: s.val + 1}
    }

    fn main () {
        let s = S{val: 0};
        ApiLib::run<S>(s, &f)
    }  
}
The syntax is ApiLib::run::<S>(s, &f), but just ApiLib::run(s, &f) is enough - the compiler knows what you want to do, given the types
David O'Connor
@David-OConnor
Nov 23 2018 17:13
Thanks! Having disconnect between my example and actual proj. Will post back here once sorted
David O'Connor
@David-OConnor
Nov 23 2018 18:36
@1tgr Thank you very much - your solution worked. My problem was the missing :: syntax between run and <s>, as you pointed out
David O'Connor
@David-OConnor
Nov 23 2018 23:30

Another Q: How would y'all approach iterating over a nested structure? I'm trying a recursive approach, but am running into issues with lifetimes and borrowing. Example:

fn iter_els(result: Vec<&El>) -> Vec<&El> {
    // Iterate over all elements from a tree, starting at the top-level element.
    let mut modified_result = result;

    for el in result {
        for child in &el.children {
            modified_result.push(&child.clone());
        }
        result.extend(iter_els(modified_result));
    }
    modified_result
}

Playground

Of note, an alternative solution of storing nodes in a Hashmap with keys of ids, and children as Vecs of ids seems like a workaround.