Skip to content

Instantly share code, notes, and snippets.

@m4rw3r
Created August 18, 2015 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m4rw3r/6d1ca498f8e4abc24dbd to your computer and use it in GitHub Desktop.
Save m4rw3r/6d1ca498f8e4abc24dbd to your computer and use it in GitHub Desktop.
Difference between manually threading state and boxed closures in a piece of code using a parser-combinator.
diff --git a/examples/http_parser.rs b/examples/http_parser.rs
index 5c31b35..196763e 100644
--- a/examples/http_parser.rs
+++ b/examples/http_parser.rs
@@ -41,17 +41,17 @@ fn is_not_space(c: u8) -> bool { c != b' ' }
fn is_end_of_line(c: u8) -> bool { c == b'\r' || c == b'\n' }
fn is_http_version(c: u8) -> bool { c >= b'0' && c <= b'9' || c == b'.' }
-fn end_of_line<'a>(p: Empty<'a, u8>) -> Parser<'a, u8, u8, Error<u8>> {
- or(mdo!{p,
+fn end_of_line<'a>() -> Parser<'a, 'a, u8, u8, Error<u8>> {
+ or(mdo!{
char(b'\r');
char(b'\n');
ret u8, Error<u8>: b'\r'
- }, |p| char(p, b'\n'))
+ }, char(b'\n'))
}
-fn http_version<'a>(p: Empty<'a, u8>) -> Parser<'a, u8, &'a [u8], Error<u8>> {
- mdo!{p,
+fn http_version<'a>() -> Parser<'a, 'a, u8, &'a [u8], Error<u8>> {
+ mdo!{
string(b"HTTP/");
let version = take_while1(is_http_version);
@@ -59,13 +59,13 @@ fn http_version<'a>(p: Empty<'a, u8>) -> Parser<'a, u8, &'a [u8], Error<u8>> {
}
}
-fn request_line<'a>(p: Empty<'a, u8>) -> Parser<'a, u8, Request<'a>, Error<u8>> {
- mdo!{p,
+fn request_line<'a>() -> Parser<'a, 'a, u8, Request<'a>, Error<u8>> {
+ mdo!{
let method = take_while1(is_token);
take_while1(is_space);
let uri = take_while1(is_not_space);
take_while1(is_space);
- let version = http_version;
+ let version = http_version();
ret Request {
method: method,
@@ -75,18 +75,18 @@ fn request_line<'a>(p: Empty<'a, u8>) -> Parser<'a, u8, Request<'a>, Error<u8>>
}
}
-fn message_header_line<'a>(p: Empty<'a, u8>) -> Parser<'a, u8, &'a [u8], Error<u8>> {
- mdo!{p,
- take_while1(is_horizontal_space);
+fn message_header_line<'a>() -> Parser<'a, 'a, u8, &'a [u8], Error<u8>> {
+ mdo!{
+ take_while1(is_horizontal_space);
let line = take_till(is_end_of_line);
- end_of_line;
+ end_of_line();
ret line
}
}
-fn message_header<'a>(p: Empty<'a, u8>) -> Parser<'a, u8, Header, Error<u8>> {
- mdo!{p,
+fn message_header<'a>() -> Parser<'a, 'a, u8, Header<'a>, Error<u8>> {
+ mdo!{
let name = take_while1(is_token);
char(b':');
let lines = many1(message_header_line);
@@ -98,12 +98,12 @@ fn message_header<'a>(p: Empty<'a, u8>) -> Parser<'a, u8, Header, Error<u8>> {
}
}
-fn request<'a>(p: Empty<'a, u8>) -> Parser<'a, u8, (Request, Vec<Header>), Error<u8>> {
- mdo!{p,
- let r = request_line;
- end_of_line;
+fn request<'a>() -> Parser<'a, 'a, u8, (Request<'a>, Vec<Header<'a>>), Error<u8>> {
+ mdo!{
+ let r = request_line();
+ end_of_line();
let h = many(message_header);
- end_of_line;
+ end_of_line();
ret (r, h)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment