#Phantom borrowing happen when multiple time on a view even if dropping

18 messages · Page 1 of 1 (latest)

sharp mortar
#

Hello,
I am facing an issue where I have an Entry API that return a view over some storage, to do so it takes a mutable borrow and return a structure holding that borrow, when that structure goes out of scope the borrow should disapear and thus let me reborrow, however this is not the case. here is the smallest code snippet I could make that show the issue. Please do note that the internal api and lifetime are structured like that for a reason and I can not use GAT (else it would be way easier).
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=7a453666f4e796f4abfeeb266b26410a

And yes the obvious issue is due to this :
fn entry(&'a mut self, key: K) -> Entry<'a, Self, K, V>; which should really be fn entry(&mut self, key: K) -> Entry<'_, Self, K, V>; and even better fn entry(&mut self, key: K) -> Entry<'_, K, V>; but sadly this would mean a complete rework of my dispatch system to the storage.

clever edge
#

when you write things like rust impl<'a, K, V> Map<'a, K, V> pub fn entry(&'a mut self, key: K) -> Entry<'a, K::MapStorage, K, V> { ... } } you are saying "this entry borrows the map for the entire lifetime of the map"

#

when you mutably borrow something for its entire lifetime that means it may never be borrowed again

#

To better understand this, your first step should be to assign sensible names to these lifetimes. 'a is not a good name; try things like 'key, 'map, 'slf etc

sharp mortar
#

that much I knew, sadly my issue more lies in my structure as my Entry is not really friendly lifetime wise

#

btw 'a here only means 'value and nothing more

#

ok by removing my lifetime requirement on my Entry such as my storage had the same lifetime as the value, it now works

#
pub enum EntryNew<'value, S, K, V> where S: MapStorage<'value, K, V>

pub enum EntryOld<'value, S:'value, K, V> where S: MapStorage<'value, K, V>
clever edge
sharp mortar
#

Ok now let's complexify and add back some of my original trait in the mix and try to return some data

#

now I don't borrow my entry for 'value but my associated type does

#

which cause another slew of issue

clever edge
#

gats is not really my strong suit sorry

iron terrace
#

yeah, I am trying to go gat less

iron terrace