These are chat archives for rust-lang/rust

24th
Oct 2017
Nanocryk
@Nanocryk_twitter
Oct 24 2017 09:13
Can I get some help with my code ? I have an error and can't manage to find similar issues.
#[derive(Debug)]
pub struct World {
    ids: HashSet<u32>,
    next_id: u32,
    listeners: Vec<Rc<RefCell<WorldListener>>>,
}

impl World {
    // ...

    pub fn remove_listener<T: WorldListener>(&mut self, listener: Rc<RefCell<T>>) 
    where
        T: WorldListener + 'static
    {
        self.listeners.retain(|e| Rc::ptr_eq(e, &listener));
    }

    // ...
}

/// Enable to listen to World events.
pub trait WorldListener: Debug {
    /// Called when an event is raised.
    ///
    /// Return `Some` list of WorldRequest to process, or `None`.
    fn listen_world(&mut self, event: WorldEvent) -> Option<Vec<WorldRequest>>;
}

and I get

 --> src\entity\world.rs:79:49
   |
79 |         self.listeners.retain(|e| Rc::ptr_eq(e, &listener));
   |                                                 ^^^^^^^^^ expected trait entity::world::WorldListener, found type parameter
   |
   = note: expected type `&std::rc::Rc<std::cell::RefCell<entity::world::WorldListener + 'static>>`
              found type `&std::rc::Rc<std::cell::RefCell<T>>`
   = help: here are some functions which might fulfill your needs:
           - .into_inner()

I previously tried to use Box but I had a similar issue.

Chaitanya Munukutla
@munukutla
Oct 24 2017 09:21
Hey guys.
Small issue.
I'm a bit new to Rust.
So bear with me?
fn get_stream() -> Incoming {
    let listener = TcpListener::bind("localhost:3000").unwrap();

    let stream = listener.incoming();

    stream
}
Nanocryk
@Nanocryk_twitter
Oct 24 2017 09:24
@munukutla what is the problem ?
Chaitanya Munukutla
@munukutla
Oct 24 2017 09:25
expected lifetime parameter for Incoming
this function's return type contains a borrowed value, but there is no value for it to be borrowed from
consider giving it a 'static lifetime
Nanocryk
@Nanocryk_twitter
Oct 24 2017 09:27
You create a listener in your function, you borrow the result of .incoming(), so I'll say that stream will outlive listener.
.incoming() gives an iterator over new connections. What are you trying to do here ?
Chaitanya Munukutla
@munukutla
Oct 24 2017 09:30
I want to use the stream variable from another module and process it.
111
use std::net::{TcpListener, Incoming};

fn main() {
    let stream = get_stream();
}

fn get_stream() -> Incoming {
    let listener = TcpListener::bind("localhost:3000").unwrap();

    let stream = listener.incoming();

    stream
}
That's the entire code.
I want to handle the processing in the main function, let's say.
How do I achieve that?
Nanocryk
@Nanocryk_twitter
Oct 24 2017 09:32
stream is "linked" to listener, so it can't outlive it.
use std::net::{TcpListener, Incoming};

fn main() {
    let listener = TcpListener::bind("localhost:3000").unwrap();

    for stream in listener.incoming() {
        // handle client ...
    }
}
Since stream is borrowed from listener, listener can't go out of scope. And that's logic, you can't handle connections if your listener don't exist anymore.
Chaitanya Munukutla
@munukutla
Oct 24 2017 09:36
Oh okay, thanks a lot @Nanocryk_twitter
Chaitanya Munukutla
@munukutla
Oct 24 2017 09:51
@Nanocryk_twitter , one more doubt.
So you mean to say that listener goes out of scope at the end of the for loop?
Nanocryk
@Nanocryk_twitter
Oct 24 2017 09:53
No
listener will go out of fn main() scope
Each stream will go out of the for scope.
Chaitanya Munukutla
@munukutla
Oct 24 2017 09:54
Yeah yeah, so the way I was attempting, I was trying to take stream beyond the scope of the listener, which is not allowed.
Are we on the same page?
Nanocryk
@Nanocryk_twitter
Oct 24 2017 11:24
That's it.
The same page ?
Jan-Oliver Kaiser
@Janno
Oct 24 2017 14:15
rustc tells me that "impl trait is experimental". what do I need to do to enable it?
Chaitanya Munukutla
@munukutla
Oct 24 2017 14:17
There would be a corresponding #[feature] you can add.
Jan-Oliver Kaiser
@Janno
Oct 24 2017 14:18
usually the error messages are pretty good about telling me which feature it is. but I've got no clue for this one. the obvious candidate "impl_trait" doesn't seem to work
Chaitanya Munukutla
@munukutla
Oct 24 2017 14:20
I read around that it's stable in nightly.
Jan-Oliver Kaiser
@Janno
Oct 24 2017 14:21
oh, nevermind. I think I messed up the syntax for that and now I realize I forgot to switch to nightly anyways so let me try to correct these two mistakes first :)
Chaitanya Munukutla
@munukutla
Oct 24 2017 14:21
May be you can have a local override to use the nightly rustc and check?
Yayy.
Jan-Oliver Kaiser
@Janno
Oct 24 2017 14:27
and now the error message suggest the reight feature: #![feature(conservative_impl_trait)]
sadly it is too conservative for my use case but that's a different problem. thanks for the help!
Chaitanya Munukutla
@munukutla
Oct 24 2017 14:28
Awesome. I saw that solution somewhere but was searching for something more ... "un-conservative"
Jan-Oliver Kaiser
@Janno
Oct 24 2017 14:29
I suppose that doesn't exist yet