tokio-rs/slab

Is get2_unchecked_mut redundant?

Ralith opened this issue · 3 comments

It's unclear to me why this should ever be preferred over calling get_unchecked twice. Can we avoid maintaining an extra interface here?

hawkw commented

get_unchecked_mut mutably borrows the Slab, so the interface doesn't permit mutably borrowing a second item while one is already borrowed. get2_unchecked_mut can be used to mutate two entries at a time (such as mem::swapping them).

Fair enough!

hawkw commented

I notice the docs for get2_mut explain the use-case for it:

slab/src/lib.rs

Lines 693 to 709 in 3340fdc

/// This function can be used to get two mutable references out of one slab,
/// so that you can manipulate both of them at the same time, eg. swap them.
///
/// # Examples
///
/// ```
/// # use slab::*;
/// use std::mem;
///
/// let mut slab = Slab::new();
/// let key1 = slab.insert(1);
/// let key2 = slab.insert(2);
/// let (value1, value2) = slab.get2_mut(key1, key2).unwrap();
/// mem::swap(value1, value2);
/// assert_eq!(slab[key1], 2);
/// assert_eq!(slab[key2], 1);
/// ```

but the docs for get2_unchecked_mut don't, really:

slab/src/lib.rs

Lines 783 to 787 in 3340fdc

/// Return two mutable references to the values associated with the two
/// given keys simultaneously without performing bounds checking and safety
/// condition checking.
///
/// This function should be used with care.

Maybe we could make this a little clearer by having the doc comment for get2_unchecked_mut explain the intended use-case, or adding a "This is a version of [get2_mut] that doesn't perform bounds and safety condition checks" or something?