vector - Is there a more friendly RefCell-like object? -


i'm looking class vec<refcell<t>>, in ultimate owner & allocator of of data, yet different pieces of array can be mutably borrowed multiple parties indefinitely.

i emphasize indefinitely because of course pieces of vec<t> can mutably borrowed multiple parties, doing involves making split can resolved after parties done borrowing.

vec<refcell<t>> seems world of danger , many ugly if statements checking borrow_state, seems unstable. if wrong, kablammo! panic! not lending library like. in lending library, if ask book isn't there, tell "oh, it's checked out." nobody dies in explosion.

so write code this:

let mut = lendinglibrary::new(); a.push(foo{x:10}); a.push(foo{x:11}); let b1 = a.get(0); // <-- b1 option<refmut<foo>> let b2 = a.get(1); // <-- b2 option<refmut<foo>>  // 0th element has been borrowed, so... let b3 = a.get(0); // <-- b3 option::none  

does such thing exist? or there canonical way kind of behavior? kind of "friendly refcell"?

if answer happens yes, there threadsafe variant?

refcell not designed long-lived borrows. typical use case in function, you'll borrow refcell (either mutably or immutably), work value, release borrow before returning. i'm curious know how you're hoping recover borrowed refcell in single-threaded context.

the thread-safe equivalent refcell rwlock. has try_read , try_write functions not block or panic if incompatible lock still acquired (on thread, including current thread). contrarily refcell, makes sense retry later if locking rwlock fails, since thread might happen have locked @ same time.

if end using write or try_write, , never read or try_read, should use simpler mutex instead.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -