I found that trying to use Wrapper after it was borrowed by Transform::transform was impossible.
That is:
let mut wrapper = Wrapper::new();
// this line is fine by itself
transform.transform(&mut wrapper);
// but this line fails to compile due to the above line holding onto the reference forever.
println!("{:?}", wrapper.requests);
This is very strange since after transform.transform(&mut wrapper) the reference to the wrapper should be dropped and we should be able to use wrapper again.
However it turns out this is expected behavior.
I found this article helpful in explaining why exactly this was occurring.
https://tfpk.github.io/lifetimekata/chapter_4.html
The article suggests using different lifetimes but we also have situations where we need the lifetimes to be the same.
But I found a solution which I have implemented in this PR:
We need to define that the lifetime of the contents of the wrapper outlives the lifetime wrapper itself.
To achieve this we introduce a 'shorter and 'longer lifetime in Transform::transform and some of the functions that call it.
The 'longer: 'shorter syntax declares that 'longer will live longer than 'shorter (inclusive)
There is no need for self in Transform::transform to be assigned a lifetime
not strictly needed but makes sense.
Adding the 'longer and 'shorter lifetimes makes the function signature more confusing to read, but it actually relaxes the lifetime requirements instead of adding more.
Previously the two lifetimes had to be exactly the same.
Now they can be exactly the same or 'longer can outlive 'shorter.
I also updated the changelog since that was missed in #1724
Fixes some issues with https://github.com/shotover/shotover-proxy/pull/1724/files
I found that trying to use Wrapper after it was borrowed by
Transform::transform
was impossible. That is:This is very strange since after
transform.transform(&mut wrapper)
the reference to the wrapper should be dropped and we should be able to use wrapper again. However it turns out this is expected behavior. I found this article helpful in explaining why exactly this was occurring. https://tfpk.github.io/lifetimekata/chapter_4.htmlThe article suggests using different lifetimes but we also have situations where we need the lifetimes to be the same. But I found a solution which I have implemented in this PR:
Transform::transform
and some of the functions that call it.'longer: 'shorter
syntax declares that'longer
will live longer than'shorter
(inclusive)Transform::transform
to be assigned a lifetimeAdding the
'longer
and'shorter
lifetimes makes the function signature more confusing to read, but it actually relaxes the lifetime requirements instead of adding more. Previously the two lifetimes had to be exactly the same. Now they can be exactly the same or'longer
can outlive'shorter
.I also updated the changelog since that was missed in #1724
TL;DR
This PR should have no impact on functionality or performance but enables the way that we call
Transform::transform
to be more flexible, unblocking https://github.com/shotover/shotover-proxy/pull/1717