{"action":"created","issue":{"url":"https://api.github.com/repos/fengsp/pencil/issues/22","repository_url":"https://api.github.com/repos/fengsp/pencil","labels_url":"https://api.github.com/repos/fengsp/pencil/issues/22/labels{/name}","comments_url":"https://api.github.com/repos/fengsp/pencil/issues/22/comments","events_url":"https://api.github.com/repos/fengsp/pencil/issues/22/events","html_url":"https://github.com/fengsp/pencil/issues/22","id":160575353,"number":22,"title":"How to reuse a db client and modularize handlers?","user":{"login":"scorphus","id":2112697,"avatar_url":"https://avatars.githubusercontent.com/u/2112697?v=3","gravatar_id":"","url":"https://api.github.com/users/scorphus","html_url":"https://github.com/scorphus","followers_url":"https://api.github.com/users/scorphus/followers","following_url":"https://api.github.com/users/scorphus/following{/other_user}","gists_url":"https://api.github.com/users/scorphus/gists{/gist_id}","starred_url":"https://api.github.com/users/scorphus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/scorphus/subscriptions","organizations_url":"https://api.github.com/users/scorphus/orgs","repos_url":"https://api.github.com/users/scorphus/repos","events_url":"https://api.github.com/users/scorphus/events{/privacy}","received_events_url":"https://api.github.com/users/scorphus/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-06-16T04:29:27Z","updated_at":"2016-06-17T15:53:11Z","closed_at":null,"body":"Hi! This is what I tried to do:\r\n\r\n```rust\r\nextern crate chill;\r\nextern crate pencil;\r\n\r\nuse pencil::Pencil;\r\nuse pencil::{Request, PencilResult, Response};\r\nuse pencil::method::Get;\r\nuse pencil::helpers;\r\n\r\nuse chill::Client;\r\n\r\n\r\nstruct Handlers {\r\n    db: Client,\r\n}\r\n\r\nimpl Handlers {\r\n    fn hello(&self, _: &mut Request) -> PencilResult {\r\n        Ok(Response::from(\"Hello World!\"))\r\n    }\r\n}\r\n\r\n\r\nfn main() {\r\n    let mut app = Pencil::new(\"/web/hello\");\r\n    let mut handlers = Handlers{db: Client::new(\"http://0.0.0.0:5984/\").unwrap()};\r\n    app.route(\"/\", &[Get], \"hello\", handlers.hello);\r\n    app.run(\"0.0.0.0:5000\");\r\n}\r\n```\r\nI can't pass `handlers.hello` as a `ViewFunc` to `app.route()`. How could I do what I just meant by this code? As you can see, I'm pretty new to Rust.\r\n\r\nThanks."},"comment":{"url":"https://api.github.com/repos/fengsp/pencil/issues/comments/226807373","html_url":"https://github.com/fengsp/pencil/issues/22#issuecomment-226807373","issue_url":"https://api.github.com/repos/fengsp/pencil/issues/22","id":226807373,"user":{"login":"scorphus","id":2112697,"avatar_url":"https://avatars.githubusercontent.com/u/2112697?v=3","gravatar_id":"","url":"https://api.github.com/users/scorphus","html_url":"https://github.com/scorphus","followers_url":"https://api.github.com/users/scorphus/followers","following_url":"https://api.github.com/users/scorphus/following{/other_user}","gists_url":"https://api.github.com/users/scorphus/gists{/gist_id}","starred_url":"https://api.github.com/users/scorphus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/scorphus/subscriptions","organizations_url":"https://api.github.com/users/scorphus/orgs","repos_url":"https://api.github.com/users/scorphus/repos","events_url":"https://api.github.com/users/scorphus/events{/privacy}","received_events_url":"https://api.github.com/users/scorphus/received_events","type":"User","site_admin":false},"created_at":"2016-06-17T15:53:11Z","updated_at":"2016-06-17T15:53:11Z","body":"After applying #14 – actually rebasing on top of master and solving a conflict – I managed to do this:\r\n\r\n```rust\r\nextern crate chill;\r\nextern crate pencil;\r\n\r\nuse pencil::Pencil;\r\nuse pencil::{Request, PencilResult, Response};\r\nuse pencil::method::Get;\r\nuse pencil::helpers;\r\n\r\nuse chill::Client;\r\n\r\n\r\nstruct Handlers {\r\n    db: Client,\r\n}\r\n\r\nimpl Handlers {\r\n    fn hello(&self, _: &mut Request) -> PencilResult {\r\n        Ok(Response::from(\"Hello World!\"))\r\n    }\r\n\r\n}\r\n\r\n\r\nfn main() {\r\n    let mut app = Pencil::new(\"/web/hello\");\r\n    let handlers = Handlers{db: Client::new(\"http://0.0.0.0:5984/\").unwrap()};\r\n    app.route(\"/\", &[Get], \"hello\", move |resp| handlers.hello(resp));\r\n    app.run(\"0.0.0.0:5000\");\r\n}\r\n```\r\n\r\nSo far so good. But no DB connections yet. So I tried:\r\n\r\n```rust\r\nextern crate chill;\r\nextern crate pencil;\r\n\r\nuse pencil::Pencil;\r\nuse pencil::{Request, PencilResult, Response};\r\nuse pencil::method::Get;\r\nuse pencil::helpers;\r\n\r\nuse chill::Client;\r\n\r\n\r\nstruct Handlers {\r\n    db: Client,\r\n}\r\n\r\nimpl Handlers {\r\n    fn hello(&self, _: &mut Request) -> PencilResult {\r\n        Ok(Response::from(\"Hello World!\"))\r\n    }\r\n\r\n    fn logo(&self, req: &mut Request) -> PencilResult {\r\n        println!(\"From {}\", req.remote_addr());\r\n        println!(\"Headers {}\", req.headers());\r\n        Ok(helpers::send_file(\"./static/logo.png\", \"image/png\".parse().unwrap(), false).unwrap())\r\n    }\r\n\r\n}\r\n\r\n\r\nfn main() {\r\n    let mut app = Pencil::new(\"/web/hello\");\r\n    let handlers = Handlers{db: Client::new(\"http://0.0.0.0:5984/\").unwrap()};\r\n    app.route(\"/\", &[Get], \"hello\", move |resp| handlers.hello(resp));\r\n    app.route(\"/logo.png\", &[Get], \"logo\", move |resp| handlers.logo(resp));\r\n    app.run(\"0.0.0.0:5000\");\r\n}\r\n```\r\nWhich obviously failed:\r\n```\r\n   Compiling handers v0.1.0 (file:///Users/pablo/Workspace/handers)\r\nsrc/main.rs:34:56: 34:64 error: capture of moved value: `handlers` [E0382]\r\nsrc/main.rs:34     app.route(\"/logo.png\", &[Get], \"logo\", move |resp| handlers.logo(resp));\r\n                                                                      ^~~~~~~~\r\nsrc/main.rs:33:37: 33:48 note: value moved (into closure) here\r\nsrc/main.rs:33     app.route(\"/\", &[Get], \"hello\", move |resp| handlers.hello(resp));\r\n                                                   ^~~~~~~~~~~\r\nsrc/main.rs:34:56: 34:64 help: run `rustc --explain E0382` to see a detailed explanation\r\nsrc/main.rs:34:56: 34:64 note: move occurs because `handlers` has type `Handlers`, which does not implement the `Copy` trait\r\nerror: aborting due to previous error\r\nerror: Could not compile `handers`.\r\n```\r\nAnd, finally, I gave up:\r\n```rust\r\nextern crate chill;\r\nextern crate pencil;\r\n\r\nuse pencil::Pencil;\r\nuse pencil::{Request, PencilResult, Response};\r\nuse pencil::method::Get;\r\nuse pencil::helpers;\r\n\r\nuse chill::Client;\r\n\r\n\r\n#[derive(Copy, Clone)]\r\nstruct Handlers {\r\n    db: Client,\r\n}\r\n\r\nimpl Handlers {\r\n    fn hello(&self, _: &mut Request) -> PencilResult {\r\n        Ok(Response::from(\"Hello World!\"))\r\n    }\r\n\r\n    fn logo(&self, req: &mut Request) -> PencilResult {\r\n        println!(\"From {}\", req.remote_addr());\r\n        println!(\"Headers {}\", req.headers());\r\n        Ok(helpers::send_file(\"./static/logo.png\", \"image/png\".parse().unwrap(), false).unwrap())\r\n    }\r\n\r\n}\r\n\r\n\r\nfn main() {\r\n    let mut app = Pencil::new(\"/web/hello\");\r\n    let handlers = Handlers{db: Client::new(\"http://0.0.0.0:5984/\").unwrap()};\r\n    app.route(\"/\", &[Get], \"hello\", move |resp| handlers.hello(resp));\r\n    app.route(\"/logo.png\", &[Get], \"logo\", move |resp| handlers.logo(resp));\r\n    app.run(\"0.0.0.0:5000\");\r\n}\r\n```\r\nBecause of:\r\n```\r\n   Compiling handlers v0.1.0 (file:///.../handlers)\r\nsrc/main.rs:12:10: 12:14 error: the trait `Copy` may not be implemented for this type; field `db` does not implement `Copy` [E0204]\r\nsrc/main.rs:12 #[derive(Copy, Clone)]\r\n                        ^~~~\r\nsrc/main.rs:12:10: 12:14 note: in this expansion of #[derive(Copy)] (defined in src/main.rs)\r\nsrc/main.rs:12:10: 12:14 help: run `rustc --explain E0204` to see a detailed explanation\r\nerror: aborting due to previous error\r\nerror: Could not compile `handlers`.\r\n```\r\n\r\n@fengsp: Can you please help me achieve what I meant by this code? Thank you!"}}
{"actor":{"display_login":"scorphus"}}