1

I'm a bit lost with a struct I have :

pub struct Blah {
    pub efd: mio::unix::EventedFd,
}

When compiling that I get expected lifetime parameter for the EventedFd even though it isn't a reference. I tried wrapping it in a Box but the error stays the same. I could add a lifetime parameter but I feel like I'm missing something important, I don't get why I'd need it right there. I actually have another Box in that same struct which works perfectly fine without a lifetime parameter, so I assume it has something to do with the type of EventedFd.

I just want to store that EventedFd in there, like this (I tried with and without the Box, as mentioned) :

let efd = Box::new(EventedFd(&fd.as_raw_fd()));
Blah {efd}
hellow
  • 12,430
  • 7
  • 56
  • 79
Ulrar
  • 895
  • 8
  • 17
  • 4
    _"even though it isn't a reference"_ But it does contain a lifetime parameter, as presented in the documentation of [`EventedFd`](https://docs.rs/mio/0.6.16/mio/unix/struct.EventedFd.html). – E_net4 Apr 24 '19 at 15:06
  • From the examples in the docs linked by E_net4, it looks like `EventedFd` is not meant to be stored. Just store a `RawFd` and create a temporary `EventedFd` when needed. – rodrigo Apr 24 '19 at 18:03
  • @rodrigo *"it looks like EventedFd is not meant to be stored"* Why do you think that? You can store evertything everywhere as long as you respect the lifetimes. – hellow Apr 25 '19 at 05:35
  • @Ulrar please **always** include the **complete** error message. You can do that best by using `none` as language specifier (look at https://stackoverflow.com/q/55830219 to see what I mean). – hellow Apr 25 '19 at 05:37
  • I get it, EventedFd takes a reference, so whatever I do I'm stuck with whatever I'm passing to it, there's no way to have it take ownership of the fd. Alright then, I will store the fd and construct EventedFd on the fly, thanks ! – Ulrar Apr 25 '19 at 07:24
  • @hellow: Sure you can, but look at the examples in the documentation. This type is used as `EventedFd(&self.fd).register(...)`. That is, an object is created on the stack to call a utility function and then immediately dropped. They keep the `RawFd` and construct a temporary `EventedFd` when needed. It has zero cost construction, after all, and its sole purpose is to implement `Evented`. – rodrigo Apr 25 '19 at 07:25
  • 1
    I found [Value doesn't live long enough when put in struct](https://stackoverflow.com/q/42538463/3650362) [and](https://stackoverflow.com/q/39127218/3650362) [these](https://stackoverflow.com/q/50955374/3650362) [five](https://stackoverflow.com/q/28949524/3650362) [other](https://stackoverflow.com/q/53719894/3650362) [questions](https://stackoverflow.com/q/43712228/3650362) all exhibit minor variants on the same problem. I'm beginning to think that lifetime elision for non-references was always a bad idea. – trent Apr 25 '19 at 11:49

0 Answers0