After reading Niko’s excellent article about reference counting, leaks and scoped threads I like the closure-based scoped threads the best.
However, in the section about the proposed Leak trait being somewhat contagious I got this somewhat unrelated idea how to lower the risk of introducing leak-related bugs: Extend type parameter constrains syntax with |
(or) operator and define Rc
as struct Rc<T: !Drop | (Drop + Leak)>
.
So types that implement Drop
but do not opt-in to Leak
would not be possible in Rc
as they are likely to introduce bugs in case of cycles.
Pros: type system will catch some more errors.
Cons: Type that implements Drop
but not Leak
cannot be used with Rc
even in cases where cycles cannot happen. Maybe this could be improved in the future by automatically implemented trait Acyclic
for types that do not form cycles in type composition graph so Rc
would be struct Rc<T: Acyclic | !Drop | (Drop + Weak)>
.
Alternatively, if specializations are possible the Rc
could be defined as struct Rc<T : Leakable>
and Leakable
as
trait Leakable<T : !Drop> {}
trait Leakable<T : Drop + Leak> {}