Owning-ref-rs: Movable bundle of owner and reference


Motivated by some insight I had after an IRC discussion last night. This should help in some situations where you’d want to combine static borrow checking with dynamic ownership transfers.

Provided no one proofs it unsafe, of course. :slight_smile:

(See also on reddit)


The only problem I can see with this is that writing methods using this will be a pain since you can’t have arbitrary self types.

Also, you could probably stand to give a clearer example; it took me a few passes to realise what the core use of this is. It kinda gets lost in inferred types and maps (wait, there are iterators involved here?) Maybe a simple headline example like:

// Create an array owned by a Box.
let arr = Box::new([1, 2, 3, 4]) as Box<[i32]>;

// Transfer into a BoxRef.
let arr: BoxRef<[i32]> = BoxRef::new(arr);
assert_eq!(&*arr, &[1, 2, 3, 4]);

// We can slice the array without losing ownership or changing type.
let arr: BoxRef<[i32]> = arr.map(|arr| &arr[1..3]);
assert_eq!(&*arr, &[2, 3]);

// Also works for Arc, Rc, String and Vec!

Cool library! :slight_smile:


Thanks! The docs where kinda rushed, so I totally understand them being confusing. :slight_smile: Your example is good, I’ll use that in the readme.