Enum im_rc::vector::FocusMut

source ·
pub enum FocusMut<'a, A>where
    A: 'a,
{ // some variants omitted }
Expand description

A mutable version of Focus.

See Focus for more details.

You can only build one FocusMut at a time for a vector, effectively keeping a lock on the vector until you’re done with the focus, which relies on the structure of the vector not changing while it exists.

let mut vec = Vector::from_iter(0..1000);
let focus1 = vec.focus_mut();
// Fails here because you already have a focus
let focus2 = vec.focus_mut();

On the other hand, you can split that one focus into multiple sub-foci, which is safe because they can’t overlap:

let mut vec = Vector::from_iter(0..1000);
let focus = vec.focus_mut();
let (left, right) = focus.split_at(500);

These sub-foci also work as a lock on the vector, even if the focus they were created from goes out of scope.

let mut vec = Vector::from_iter(0..1000);
let (left, right) = {
    let focus = vec.focus_mut();
    focus.split_at(500)
};
// `left` and `right` are still in scope even if `focus` isn't, so we can't
// create another focus:
let focus2 = vec.focus_mut();

Implementations§

Construct a FocusMut for a Vector.

Get the length of the focused Vector.

Test if the focused Vector is empty.

Get a reference to the value at a given index.

Get a mutable reference to the value at a given index.

Get a reference to the value at a given index.

Panics if the index is out of bounds.

Get a mutable reference to the value at a given index.

Panics if the index is out of bounds.

Update the value at a given index.

Returns None if the index is out of bounds, or the replaced value otherwise.

Swap the values at two given indices.

Panics if either index is out of bounds.

If the indices are equal, this function returns without doing anything.

Lookup two indices simultaneously and run a function over them.

Useful because the borrow checker won’t let you have more than one mutable reference into the same data structure at any given time.

Panics if either index is out of bounds, or if they are the same index.

Examples
let mut vec = vector![1, 2, 3, 4, 5];
vec.focus_mut().pair(1, 3, |a, b| *a += *b);
assert_eq!(vector![1, 6, 3, 4, 5], vec);

Lookup three indices simultaneously and run a function over them.

Useful because the borrow checker won’t let you have more than one mutable reference into the same data structure at any given time.

Panics if any index is out of bounds, or if any indices are equal.

Examples
let mut vec = vector![1, 2, 3, 4, 5];
vec.focus_mut().triplet(0, 2, 4, |a, b, c| *a += *b + *c);
assert_eq!(vector![9, 2, 3, 4, 5], vec);

Get the chunk for the given index.

This gives you a reference to the leaf node that contains the index, along with its start and end indices.

Narrow the focus onto a subslice of the vector.

FocusMut::narrow(range) has the same effect as &slice[range], without actually modifying the underlying vector.

Panics if the range isn’t fully inside the current focus.

Examples
let mut vec = Vector::from_iter(0..1000);
let narrowed = vec.focus_mut().narrow(100..200);
let narrowed_vec = narrowed.unmut().into_iter().cloned().collect();
assert_eq!(Vector::from_iter(100..200), narrowed_vec);

Split the focus into two.

Given an index index, consume the focus and produce two new foci, the left onto indices 0..index, and the right onto indices index..N where N is the length of the current focus.

Panics if the index is out of bounds.

This is the moral equivalent of slice::split_at, in that it leaves the underlying data structure unchanged, unlike Vector::split_at.

Examples
let mut vec = Vector::from_iter(0..1000);
{
    let (left, right) = vec.focus_mut().split_at(500);
    for ptr in left {
        *ptr += 100;
    }
    for ptr in right {
        *ptr -= 100;
    }
}
let expected = Vector::from_iter(100..600)
             + Vector::from_iter(400..900);
assert_eq!(expected, vec);

Convert a FocusMut into a Focus.

Trait Implementations§

Converts this type into the (usually inferred) input type.
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.