Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

static handler brings problems #10

Closed
ptxmac opened this issue Jul 8, 2016 · 6 comments
Closed

static handler brings problems #10

ptxmac opened this issue Jul 8, 2016 · 6 comments

Comments

@ptxmac
Copy link

ptxmac commented Jul 8, 2016

The CoAPServer::handle takes a 'static handler.

I'm a bit new to rust, but I believe this means we can only use top level methods as handler, and then all state needed for the server will need to be stored in global variables?

As far as I understand it, this will also make the routing tool in #4 impossible.

Please correct me if I'm wrong!

@Covertness
Copy link
Owner

@ptxmac I think this will be changed in #4 . The handler would be an instance of a struct that could store some variables. Just like this example.

@ptxmac
Copy link
Author

ptxmac commented Jul 11, 2016

I've looked a bit more on my problem. It looks like 'static isn't the problem. I would like to use a closure as a handler so I can capture some local state, but closures doesn't satisfy Copy.

I looked at the handle trait in Iron: https://github.com/iron/iron/blob/master/src/middleware/mod.rs#L58 and they managed to do it without Copy which allows this example to work: https://github.com/iron/iron/blob/master/examples/redirect.rs

@Covertness
Copy link
Owner

Copy isn't necessary in #4

@jamesmunns
Copy link
Collaborator

Yeah, I'm struggling with lifetimes as well. I've kind of hit a wall where I need to clone my "router" struct every time a function is dispatched (so it can be moved into the handler closure), or it needs to have static lifetime. I need to study how the iron handler works a little better.

I've thought about using box'd closures, but I haven't used those yet. You can see what I have here: https://github.com/Covertness/coap-rs/compare/master...jamesmunns:coap-router?expand=1 - but like I said, I hit a bit of a wall when I try to integrate the router (it works well on its own, but trying to get it to work interchangably with a 'static function handler is more difficult than I thought.

I'm open to any thoughts or suggestions.

@jamesmunns
Copy link
Collaborator

I believe this will help in the future: rust-lang/rust#34511

In the future, I would suggest that we use boxed closures rather than static handlers.

@Covertness
Copy link
Owner

handler has removed static #47

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants