These are chat archives for rust-lang/rust

6th
Feb 2017
Sasha Hilton
@sashahilton00
Feb 06 2017 00:27
Evening, back again :) I was hoping someone could provide some clarity as to how one uses state provided by one part of a rust application from another part of the rust application. I am trying to use the ws-rs websockets library. The main.rs file calls setup_websockets() in websockets.rs which starts the websockets server. What I am trying to do is to use websockets::Server::broadcast_message() from another part of the application (lets call it sendinfo.rs). My question is, is it possible to use the out variable as provided by setup_websockets() in the function broadcast_message() when called from sendinfo.rs? From what I can gather one would clone it for use in sendinfo.rs somehow, though I am completely lost as to where one goes from here :(
struct Server {
    out: Sender,
}

impl Server {
    pub fn broadcast_message(msg: Message) - > Result < () > {
        //self is a mutable reference to the Server struct containing the out variable provided by setup_websockets(). I am trying to work out what one must replace self.out.broadcast(msg) with in order that one could call broadcast_message() from a separate part of the application without needing to setup Server {} again?

        self.out.broadcast(msg)
    }
}

pub fn setup_websockets() {
    listen("127.0.0.1:3012", |out| {
        Server {
            out: out,
        }
    }).unwrap();
}
Mow Dron
@6mw6
Feb 06 2017 07:38
Hello, everyone!
I am a newbie)
There is book learning Rust except for The Rust Programming Language?
Mow Dron
@6mw6
Feb 06 2017 07:41
@ozkriff Thank you, man!
Clément
@claisne
Feb 06 2017 10:43
Hi, I am currently playing with rust and I stumbled into that issue:
players is an hashmap
for player in players.values_mut() { for other in players.values_mut() { // code } }
which result in cannot borrow self.players as mutable more than once at a time
@claisne,
What do you want to do with these players?
Felix S Klock II
@pnkfelix
Feb 06 2017 10:46
@claisne one work-around, if you actually require mutable access to distinct players: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.split_at_mut
Clément
@claisne
Feb 06 2017 10:46
check if some condition is met between two players, and then update the first one
well i only require mutability to player not other
Felix S Klock II
@pnkfelix
Feb 06 2017 10:46
@claisne namely, iterate over the indices, splitting the vector into disjoint sections before borrowing the element(s) that you need to look at
Clément
@claisne
Feb 06 2017 10:47
humm I'll look into it thanks!
Nate
@phaux
Feb 06 2017 12:51
Guys. I just had a crazy idea. How about we implement >> for Fn so that f1>>f2 is the same as |x|f2(f1(x)) (like in haskell)
Denis Lisov
@tanriol
Feb 06 2017 12:54
How is this going to work for functions having more or less than one argument?
Nate
@phaux
Feb 06 2017 12:56
if f2 takes more than 1 arg then f1 would have to return a tuple I guess
also you could do something like f1 >> (|a,b| a) >> f2
Denis Lisov
@tanriol
Feb 06 2017 13:01
Feels to me it would be more rustic as a combinator f1.then(f2)
Nate
@phaux
Feb 06 2017 14:03
#![feature(unboxed_closures)]

trait Then<'a, A, B, C> {
    fn then<F>(self, other: F) -> Box<Fn<(A,), Output=C> + 'a>
        where F: Fn<(B,), Output=C> + 'a;
}

impl<'a, T, A, B, C> Then<'a, A, B, C> for T where T: Fn<(A,), Output=B> + 'a {
    fn then<F>(self, other: F) -> Box<Fn<(A,), Output=C> + 'a>
        where F: Fn<(B,), Output=C> + 'a
    {
        Box::new(move |x| other(self(x)))
    }
}

fn main() {

    let f = (|x| x as f64)
        .then(|x| x * x)
        .then(f64::sqrt);

    println!("{:?}", f(42));

}
this almost works modulo some borrowing issues

now how cool it would be to just write

fn main() {
    let f = (|x| x as f64) >> (|x| x * x) >> f64::sqrt;
    ...

etc

Denis Lisov
@tanriol
Feb 06 2017 14:13
And then you find out that x is a string, so you need to parse it, get a Result and then suddenly you cannot anymore use this syntax?
Nate
@phaux
Feb 06 2017 14:15
… >> str::parse >> Result::unwrap >> …
Denis Lisov
@tanriol
Feb 06 2017 14:16
No, no, no unwrap please. I need my soft to work, not panic at the worst moment possible.
And these are all functions, so you cannot early return from inside one of them.
Nate
@phaux
Feb 06 2017 14:20

then you're supposed to do something like

x.parse().map(f1 >> f2 >> f3)

or use unwrap_or_default. the possibilities are endless

Denis Lisov
@tanriol
Feb 06 2017 14:21

That's exactly why I'd prefer to do

x.parse()
    .and_then(f1)
    .and_then(f2)
    .and_then(f3)

to avoid switching from combinators to inline operator and back all the time.

(yes, most functions I have to deal with return Result)
x.parse()
    .map(f1)
    .map(f2)
    .map(f3)
for your example
Denis Lisov
@tanriol
Feb 06 2017 14:28
Also, "Rust Result map" or "Rust function then" are much more friendly to search engines than... "Rust function double right bracket"? "Rust function shift right"? I don't know even how to put it in the search field :-)
Nate
@phaux
Feb 06 2017 14:30
it could be overloaded to allow f1 returning Result<T> and f2 taking T and it would carry the error possibly using the Carrier trait. I think haskells >>= operator does similar thing
Denis Lisov
@tanriol
Feb 06 2017 14:31
Yeah, that's exactly the previous point: how do I google for the >>= operator?
Nate
@phaux
Feb 06 2017 14:32
why would you have to?
it would be documented if it was implemented
Denis Lisov
@tanriol
Feb 06 2017 14:33
Because I don't know Haskell. How can I quickly find the section of documentation where it's described?
And a person new to Rust will have problems finding it too.
Also, you may want to look at an internals discussion
Nate
@phaux
Feb 06 2017 14:37
there's that thing called syntax index in the book which lists all the operators 🙄
Ashley Mannix
@KodrAus
Feb 06 2017 21:48
A function
composition operator would be kinda cool...