-
-
Save stevedonovan/7e3a6d8c8921e3eff16c4b11ab82b8d7 to your computer and use it in GitHub Desktop.
@stevedonovan I think the implementation is unsound. The unsafe {self.as_ptr().as_ref().unwrap()}
part gives you what is inside the RefCell
but does not prevent anyone from calling borrow_mut
while the deref is still in scope.
You are completely correct, alas.
Some way to fix it?
I don't think that Deref
can be fixed safely. It basically says that the handed-out reference has same lifetime as shared pointer, which is insufficient. Ref<T>
gives you the right guarantee. A pity, really - can still say borrow
everywhere (or make it shorter ref
:) )
It cannot be fixed currently. Deref has associated type Target
which cannot be polymorphic (forall t'
), because of a few reasons, including lack of HKT. I don't think it will be possible even if GATs land, as forall t'
is not what we want to tell there, really. What we want to express is that this 't
in Target
is the same as t
on &'t self
. In order to express that, we would either need to change the definition of Deref
(not going to happen), or we would need to have some special type checking rules. I do feel that the type system around lifetimes is not flexible as it should be, so hopefully, it could be expressible in the future.
This is great! And so readable!