That won't work because Self needs a reference that lives as long as — well, the anonymous '_ lifetime declared.
The needed change here is to change X not to store a reference, but a copy of whatever data it needs. The definition of the Parse trait does not allow Self to borrow from the input.
$ cargo check
Checking foo v0.1.0 (/home/jean/tmp/foo)
error: lifetime may not live long enough
--> src/main.rs:26:9
|
25 | fn parse(input : &Stream) -> Self {
| ----- ---- return type is X<'2>
| |
| has type `&Stream<'1>`
26 | Self(&*input.current)
| ^^^^^^^^^^^^^^^^^^^^^ associated function was supposed to return data with lifetime `'2` but it is returning data with lifetime `'1`
error: could not compile `foo` (bin "foo") due to previous error
The way I understand it, the problem is essentially unsolvable. The Parse trait requires a function of signature
fn parse(input : &Stream) -> Self;
or with desugared lifetimes:
fn<'a, 'b> parse(input: &'a Stream<'b>) -> Self;
which requires you to be able to get a Self from a Stream with any lifetime for the inner data. You can't do that for X<'a> with a fixed lifetime 'a.
In reality, I'm talking about the Parse trait from the syn crate. Here, input is a &ParseBuffer, and current is a Cursor. Cursor does not implement Copy.
Okay, it does, but I still need a reference to the underlying buffer, I don't want to copy whatever Cursor is pointing to because that would defeat the purpose of using a reference/Cursor in the first place, which is: (1) to save memory by not having duplicate memory, (2) to save time by not copying that memory.
In syn, you use parsing to create ASTs through the Parse trait. However, for some parts of the input, I don't want to generate an entire syntax tree, I just want to check if that part of the TokenStream is indeed valid for what type I expect there to be. I want to store the cursor to those Tokens so that I can copy those parts of the input to the output of my macro, while the more complicated parts that I do want to change, are processed manually.
I want to store the cursor to those Tokens so that I can copy those parts of the input to the output of my macro
The definition of Parse prohibits that. Because the lifetime of the ParseStream<'_> is on the parse function, not the trait, you do not receive any guarantee that the data lasts longer than that single function call, and so borrowing it longer is not possible.