Live data from Hacker News

Pencil: A Microframework Inspired by Flask for Rust

fengsp.github.io

51–58 of 58 posts

Re: Pencil: A Microframework Inspired by Flask for Rust

#51

Anyone know of any benchmarks? This seems to be built on top of hyper. I remember checking out hyper a couple months ago and being disappointed with its performance. Last I checked it was using synchronous IO, and was performing about an order of magnitude worse than equivalent Go. That could certainly change, but I'm hesitant to use Rust for an HTTP server like I would with Go until I see better performance.

Hyper's async IO is being actively worked on or done IIRC.

Re: Pencil: A Microframework Inspired by Flask for Rust

#52
https://github.com/fengsp/pencil/blob/master/examples/hello/...

    app.get("/", "hello", hello);
    app.get("/user/", "user", user);
    ....
This reminds me of Pyramid more than Flask though, just my personal observation. The code looks pretty readable even from a non-Rust programmer.

Re: Pencil: A Microframework Inspired by Flask for Rust

#53
post #21

Earlier quoted context omitted.

I know very little Rust, but I was actually looking at how this could be done in Rust some time ago, but I didn't get anything working. All the routes (e.g. /foobar and accompanying function) needs to be collected by the "router" somehow which begs for a global route-container that the route macro (like Flasks route decorator) would need to add routes to, similar to how it is done is Flask, and, well, yeah, I couldn'…

There are a few different ways one could achieve this. The first options would be to generate a struct: struct RouteUser { route: &'static str, f: fn(_: &mut Request) -> PencilResult } Then given #[route("/user")] fn user(_: &mut Request) -> PencilResult {} You would hijack the `user` function to return an instance of the struct. fn user() -> RouteUser { // The actual function the user wrote: fn user(_: &mut Request)…

That seems like a good approach, although one thing is missing: when you use the route-decorator in Flask, the route is registered to the app object "via magic", whereas in your example, you still have to register it manually (`app.route(user())`). Now, I am partial to liking registering manually, like in your example and in Pencil itself, since that's clearer than having a lot of routes spread around in a whole lot of files that are magically registered, but just for the kicks, would it be possible to not having to manually register the route to the app object?

Re: Pencil: A Microframework Inspired by Flask for Rust

#54
post #16

Great start. Instead of numeric codes, perhaps use an enum for all the acceptable status codes? As Jane Street Says, make illegal state unrepresentable.

This is a good slogan, but there's some subtleties when it comes to HTTP status codes. That is, there are the defined ones, but any number is legit, so you end up with an enum with a member that's basically "anything we don't know about", so it's not as clear-cut as it is in other situations.

Good point, this is where Rust's flexible enums are an advantage relative to C-style. If you'll forgive the Haskell syntax, you can have something like:

    data StatusCoode
      = Ok200
      | Missing404
      | Error500
      | OtherStatusCode Int
To rephrase the slogan a bit - make an illegal state a bit more difficult to represent or at least the risk of an illegal state clear to the programmer.

Re: Pencil: A Microframework Inspired by Flask for Rust

#55
post #54

Earlier quoted context omitted.

This is a good slogan, but there's some subtleties when it comes to HTTP status codes. That is, there are the defined ones, but any number is legit, so you end up with an enum with a member that's basically "anything we don't know about", so it's not as clear-cut as it is in other situations.

Good point, this is where Rust's flexible enums are an advantage relative to C-style. If you'll forgive the Haskell syntax, you can have something like: data StatusCoode = Ok200 | Missing404 | Error500 | OtherStatusCode Int To rephrase the slogan a bit - make an illegal state a bit more difficult to represent or at least the risk of an illegal state clear to the programmer.

Exactly: https://github.com/hyperium/hyper/blob/master/src/status.rs#...

Re: Pencil: A Microframework Inspired by Flask for Rust

#56
post #54

Earlier quoted context omitted.

Good point, this is where Rust's flexible enums are an advantage relative to C-style. If you'll forgive the Haskell syntax, you can have something like: data StatusCoode = Ok200 | Missing404 | Error500 | OtherStatusCode Int To rephrase the slogan a bit - make an illegal state a bit more difficult to represent or at least the risk of an illegal state clear to the programmer.

Exactly: https://github.com/hyperium/hyper/blob/master/src/status.rs#...

My bad.. I was just basing my comment off the readme. In general, I love what you guys are doing and my team is looking closely at Rust and Leaf.

Re: Pencil: A Microframework Inspired by Flask for Rust

#57
post #56

Earlier quoted context omitted.

Exactly: https://github.com/hyperium/hyper/blob/master/src/status.rs#...

My bad.. I was just basing my comment off the readme. In general, I love what you guys are doing and my team is looking closely at Rust and Leaf.

No worries! I just meant to confirm your suspicions :)

Re: Pencil: A Microframework Inspired by Flask for Rust

#58
post #28

The magic of Flask is that that thing is full of state and globals you just import and they work, like request, app, session.

That is also, I think, one of flask's biggest weaknesses. It does make everything simpler though. I'm just not very fond of magic globals. It also doesn't fit very well into REST and HTTP. On the other hand, I love flask for just about everything else.
Post reply on HN