XRef and XRefMut
When you want to return some data that might be either
owned or borrowed, it is common to return a Cow<T>.
However, Cow<T> has the following limitations:
Thas to beToOwned, and- if the data you want to borrow is behind a
RefCell, you can't.
XRef<T> is like Cow<T>, except addresses the above
limitations with the following differences:
Tdoes not have to beToOwned, (this means that now the type contained inBorrowedandOwnedhave to contain the exact matching types), andXRef<T>has a third variantRefwhich holds astd::cell::Ref<T>, for cases when the value is borrowed from aRefCell.
XRefMut<T> is like XRef<T> except that
- all references are borrowed mutably (i.e.
XRef::Borrowedholds a&mut Tinstead of&T,XRef::Refholds aRefMut<T>instead of aRef<T>), XRefMut<T>implementsDerefMut<T>, to allow borrowing data mutably without cloning.