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

Not operator needs 'static #27

Closed
Cokemonkey11 opened this issue Mar 26, 2019 · 5 comments
Closed

Not operator needs 'static #27

Cokemonkey11 opened this issue Mar 26, 2019 · 5 comments

Comments

@Cokemonkey11
Copy link

Means if you want to parse a string sourced from IO, you need to use lazy_static. Not very convenient

    lazy_static! {
        pub static ref STDIN_BUF: String = {
            let mut buf = String::new();

            std::io::stdin().lock().read_to_string(&mut buf).expect(
                "Failed to read stdin!"
            );

            buf
        };
    }

    println!(
        "{}",
        serde_yaml::to_string(&my_parser().parse(STDIN_BUF.as_bytes())?)?
    );
@J-F-Liu
Copy link
Owner

J-F-Liu commented Mar 28, 2019

Use pom::parser::Parser instead of pom::Parser, there is lifetime parameter, e.g.

pub fn json<'a>() -> Parser<'a, u8, JsonValue> {
	space() * value() - end()
}

@Cokemonkey11
Copy link
Author

I'm having some problems with seq(&' static str) if I try that.

https://github.com/Cokemonkey11/wurstdoktor/tree/nostatic

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
  --> src\main.rs:92:7
   |
92 |     ).map(|(((d, (e, n)), p), r)| {
   |       ^^^
   |
note: first, the lifetime cannot outlive the lifetime 'a as defined on the function body at 75:13...
  --> src\main.rs:75:13
   |
75 | fn class_fn<'a>() -> Parser<'a, u8, WurstFunction> {
   |             ^^
   = note: ...so that the expression is assignable:
           expected pom::parser::Parser<'a, _, _>
              found pom::parser::Parser<'_, _, _>
   = note: but, the lifetime must be valid for the static lifetime...
note: ...so that the type `&[u8]` will meet its required lifetime bounds
  --> src\main.rs:79:13
   |
79 |             !seq(PRIVATE) *
   |             ^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
error: Could not compile `wurstdoktor`.

@J-F-Liu
Copy link
Owner

J-F-Liu commented Mar 28, 2019

I have no problem when doing this in examples/json.rs

static NULL: &[u8; 4] = b"null";

fn value<'a>() -> Parser<'a, u8, JsonValue> {
	( seq(NULL).map(|_|JsonValue::Null)

@Cokemonkey11
Copy link
Author

Cokemonkey11 commented Mar 29, 2019

You're right - I think the problem is actually related to ! operator:

@@ -52,7 +52,7 @@ fn object<'a>() -> Parser<'a, u8, HashMap<String, JsonValue>> {
 }

 fn value<'a>() -> Parser<'a, u8, JsonValue> {
-       ( seq(b"null").map(|_|JsonValue::Null)
+       ( !seq(b"*") * seq(b"null").map(|_|JsonValue::Null)

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
  --> examples\json.rs:63:4
   |
63 |     ) - space()
   |       ^
   |
note: first, the lifetime cannot outlive the lifetime 'a as defined on the function body at 55:10...
  --> examples\json.rs:55:10
   |
55 | fn value<'a>() -> Parser<'a, u8, JsonValue> {
   |          ^^
   = note: ...so that the expression is assignable:
           expected pom::parser::Parser<'a, _, _>
              found pom::parser::Parser<'_, _, _>
   = note: but, the lifetime must be valid for the static lifetime...
note: ...so that the type `&[u8]` will meet its required lifetime bounds
  --> examples\json.rs:56:4
   |
56 |     ( !seq(b"*") * seq(b"null").map(|_|JsonValue::Null)
   |       ^^^^^^^^^^

@Cokemonkey11 Cokemonkey11 changed the title Needs 'static lifetime to work nicely Not operator needs 'static Mar 29, 2019
@J-F-Liu
Copy link
Owner

J-F-Liu commented Mar 29, 2019

Yes, fixed.

@J-F-Liu J-F-Liu closed this as completed Mar 29, 2019
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

2 participants