Enum druid::im::vector::FocusMut

source ·
pub enum FocusMut<'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 in 2015 edition because you're creating
// two mutable references to the same thing.
let focus2 = vec.focus_mut();
// Fails here in 2018 edition because creating focus2
// made focus1's lifetime go out of scope.
assert_eq!(Some(&0), focus1.get(0));

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

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

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();
assert_eq!(Some(&0), left.get(0));

Implementations§

source§

impl<'a, A> FocusMut<'a, A>where A: Clone + 'a,

source

pub fn new(vector: &'a mut Vector<A>) -> FocusMut<'a, A>

Construct a FocusMut for a Vector.

source

pub fn len(&self) -> usize

Get the length of the focused Vector.

source

pub fn is_empty(&self) -> bool

Test if the focused Vector is empty.

source

pub fn get(&mut self, index: usize) -> Option<&A>

Get a reference to the value at a given index.

source

pub fn get_mut(&mut self, index: usize) -> Option<&mut A>

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

source

pub fn index(&mut self, index: usize) -> &A

Get a reference to the value at a given index.

Panics if the index is out of bounds.

source

pub fn index_mut(&mut self, index: usize) -> &mut A

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

Panics if the index is out of bounds.

source

pub fn set(&mut self, index: usize, value: A) -> Option<A>

Update the value at a given index.

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

source

pub fn swap(&mut self, a: usize, b: usize)

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.

source

pub fn pair<F, B>(&mut self, a: usize, b: usize, f: F) -> Bwhere F: FnMut(&mut A, &mut A) -> B,

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);
source

pub fn triplet<F, B>(&mut self, a: usize, b: usize, c: usize, f: F) -> Bwhere F: FnMut(&mut A, &mut A, &mut A) -> B,

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);
source

pub fn chunk_at(&mut self, index: usize) -> (Range<usize>, &mut [A])

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.

source

pub fn narrow<R>(self, range: R) -> FocusMut<'a, A>where R: RangeBounds<usize>,

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);
source

pub fn split_at(self, index: usize) -> (FocusMut<'a, A>, FocusMut<'a, A>)

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);
source

pub fn unmut(self) -> Focus<'a, A>

Convert a FocusMut into a Focus.

Trait Implementations§

source§

impl<'a, A> From<FocusMut<'a, A>> for Focus<'a, A>where A: Clone + 'a,

source§

fn from(f: FocusMut<'a, A>) -> Focus<'a, A>

Converts to this type from the input type.
source§

impl<'a, A> IntoIterator for FocusMut<'a, A>where A: Clone + 'a,

§

type Item = &'a mut A

The type of the elements being iterated over.
§

type IntoIter = IterMut<'a, A>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> <FocusMut<'a, A> as IntoIterator>::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<'a, A> RefUnwindSafe for FocusMut<'a, A>where A: RefUnwindSafe,

§

impl<'a, A> Send for FocusMut<'a, A>where A: Send + Sync,

§

impl<'a, A> Sync for FocusMut<'a, A>where A: Send + Sync,

§

impl<'a, A> Unpin for FocusMut<'a, A>where A: Unpin,

§

impl<'a, A> !UnwindSafe for FocusMut<'a, A>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

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

§

impl<T> RoundFrom<T> for T

§

fn round_from(x: T) -> T

Performs the conversion.
§

impl<T, U> RoundInto<U> for Twhere U: RoundFrom<T>,

§

fn round_into(self) -> U

Performs the conversion.
source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more