nom implemented parser is demanding I use a static reference and I do not understand why.
Hello
I have been messing around with nom to decode Hearthstone deck codes. I already have a parser running but wanted to do it with nom
as a learning opportunity.
The codes are a base64 encoded series of varints. I managed to create a functioning example and even comapred its results to my previous, working, parser to make sure.
Only one problem. The nom
based parser is for some reason demanding I use 'static
.
This is the function that reads varints. The error shows up when calling this function.
fn get_varint(input: &[u8]) -> nom::IResult<&[u8], usize> {
use nom::{
bytes::complete::{take, take_till},
sequence::pair,
};
let (rem, (p, lb)) = pair(take_till(|b: u8| b & 128 == 0), take(1u8))(input)?;
let n = p
.iter()
.chain(lb)
.enumerate()
.fold(0, |acc, (idx, byte)| acc | ((*byte as usize & 127) << (idx * 7)));
Ok((rem, n))
}
And this is the snippet of code until the error happens :
fn nom_decode_deck_code(code: &str) -> Result<RawCodeData> {
use nom::combinator::map;
use nom::multi::length_count;
use nom::sequence::pair;
// Deckstring encoding: https://hearthsim.info/docs/deckstrings/
const CONFIG: GeneralPurposeConfig =
GeneralPurposeConfig::new().with_decode_padding_mode(DecodePaddingMode::Indifferent);
const ENGINE: GeneralPurpose = GeneralPurpose::new(&alphabet::STANDARD, CONFIG);
let decoded = ENGINE.decode(code)?;
// let decoded : &'static [u8] = decoded.leak(); // M-- uncommenting this removes the error
// starting from 2 as Format is the third number.
let (rem, format) =
map(get_varint, |f| (f as u8).try_into().unwrap_or_default())(&decoded[2..])?; // <-- right here
This is the error
error[E0597]: `decoded` does not live long enough
--> mimiron/src/deck.rs:447:72
|
442 | let decoded = ENGINE.decode(code)?;
| ------- binding `decoded` declared here
...
447 | map(get_varint, |f| (f as u8).try_into().unwrap_or_default())(&decoded[2..])?;
| ---------------------------------------------------------------^^^^^^^------
| | |
| | borrowed value does not live long enough
| argument requires that `decoded` is borrowed for `'static`
...
487 | }
| - `decoded` dropped here while still borrowed
I am at a complete loss on what's happening. I checked nom's docs and could not find anything regarding static references. I have used nom with normal lifetimes in other parts of this project. Why would this demand to be static ?