Skip to content
\n

The reported error is this:

\n
// error[E0597]: `bytes` does not live long enough\nfn print_header(bytes: Vec<u8>) -> Result<()> {\n//              ----- binding `bytes` declared here\n    let hdr = Header::ref_from_bytes(&bytes)?;\n//            -----------------------^^^^^^-\n//            |                      |\n//            |                      borrowed value does not live long enough\n//            argument requires that `bytes` is borrowed for `'static`\n    println!(\"{:#?}\", hdr);\n    Ok(())\n}\n//- `bytes` dropped here while still borrowed
\n

I found that rather puzzling, but I am far from experienced with rust, so that's not unusual.
\nRequiring the value to be parsed to be 'static seems like it would make the library extremely difficult to use in a real-world application.

\n

If you were to change the function definition to expect a &[u8] instead of Vec<u8> the error changes:

\n
// error[E0521]: borrowed data escapes outside of function\nfn print_header(bytes: &[u8]) -> Result<()> {\n//              -----  - let's call the lifetime of this reference `'1`\n//              |\n//              `bytes` is a reference that is only valid in the function body\n    let hdr = Header::ref_from_bytes(&bytes)?;\n//            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n//            |\n//            `bytes` escapes the function body here\n//            argument requires that `'1` must outlive `'static`\n    println!(\"{:#?}\", hdr);
\n

This error is more useful. How does bytes escape the function body there?

\n

Of course, it is the ? operator. anyhow requires error types to be 'static and zerocopy includes the reference to be parsed in the error value. So the compiler forces the input bytes to be 'static in case of the Err() outcome.

\n

I believe this is very similar to (if not the same as) the issue described here: #2262

\n

So the problem can be resolved by switching the line:

\n
let hdr = Header::ref_from_bytes(&bytes)?;
\n

to either

\n
let hdr = Header::ref_from_bytes(&bytes).unwrap();
\n

or

\n
let hdr = Header::ref_from_bytes(&bytes)\n    .map_err(|_| format_err!(\"ref_from_bytes failed\"))?;
\n

And in that linked discussion, in addition to other changes, the user also switches ? to .unwrap(). They don't mention use of anyhow specifically in that discussion however.

\n

I'm not really sure what my question is.
\nIs this issue something that an experienced rust developer would immediately recognize and fix? Or is this awkward ergonomics that might be worth looking into?

\n

I have no problem just mapping the error to something else, but thought I'd raise the issue just in case there was a better solution.

","upvoteCount":1,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"

It's anyhow that imposes the 'static requirement, since — under the hood — anyhow Boxes up (and type erases) the error value. The requirement specifically comes from this conversion, which is implicitly invoked by ?.

\n

The correct solution is, yes, to map zerocopy's error to something else that meets these requirements. We're lightly looking into providing an API that would make this easier, but it's not on our near-term roadmap right now.

","upvoteCount":1,"url":"https://github.com/google/zerocopy/discussions/2498#discussioncomment-12845335"}}}
Discussion options

You must be logged in to vote

It's anyhow that imposes the 'static requirement, since — under the hood — anyhow Boxes up (and type erases) the error value. The requirement specifically comes from this conversion, which is implicitly invoked by ?.

The correct solution is, yes, to map zerocopy's error to something else that meets these requirements. We're lightly looking into providing an API that would make this easier, but it's not on our near-term roadmap right now.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@kit299
Comment options

Answer selected by kit299
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants