Use cases for Borrow counted types?


#1

I just published a small crate to crates.io which provide the Bc<T> type. This type only count the number of time the underlying value has been mutably borrowed.

My initial motivation for building this is in the README, which I quote here:

If you want to cache an expensive computation result, you need to have
information about wether the parameters of the computation have changed or
not. You can use a hash for that, but this have two shortcomings:

  • You need to have values which implement the Hash trait. Some useful
    types like f64 do not;
  • You need to have a cheap way to compute the hash.

If computing the hash is harder or more expensive than doing the
computation, you are doomed. Or you can use Bc which will give you
information about the number of borrow since the last computation. If this
number have changed, then it is very likely that the value have changed,
and that you need to redo your computation.

That’s why I only count mutables borrow. But now I wonder if there could be any other use case for borrow counted types, either counting only immutables borrow or counting both mutables and immutables borrow.

Do you have any insight on this question ?

Cheers,
Luthaf


#2

FYI, it’s possible to mutate a value with out mutably borrowing it (e.g. using a Cell, RefCell, UnsafeCell, Mutex, etc.).


#3

Yes, I will add this to the limitations. But I do not think that people wanting to count the mutables borrow will use types you can mutate without a mutable borrow ^^