VectorGuard

Struct VectorGuard 

Source
pub struct VectorGuard<'a>(/* private fields */);
Expand description

Guards against panics during unsafe rope mutation.

VectorGuard is returned from Rope::as_mut_vector. It implements DerefMut<Target=Vector<u8>>. If a VectorGuard is dropped by panicking, it will clear the vector to prevent observation of a rope containing invalid UTF-8 after catching the panic.

Methods from Deref<Target = Vector<u8>>§

Source

pub fn len(&self) -> usize

Get the length of a vector.

Time: O(1)

§Examples
assert_eq!(5, vector![1, 2, 3, 4, 5].len());
Source

pub fn is_empty(&self) -> bool

Test whether a vector is empty.

Time: O(1)

§Examples
let vec = vector!["Joe", "Mike", "Robert"];
assert_eq!(false, vec.is_empty());
assert_eq!(true, Vector::<i32>::new().is_empty());
Source

pub fn is_inline(&self) -> bool

Test whether a vector is currently inlined.

Vectors small enough that their contents could be stored entirely inside the space of std::mem::size_of::<Vector<A>>() bytes are stored inline on the stack instead of allocating any chunks. This method returns true if this vector is currently inlined, or false if it currently has chunks allocated on the heap.

This may be useful in conjunction with ptr_eq(), which checks if two vectors’ heap allocations are the same, and thus will never return true for inlined vectors.

Time: O(1)

Source

pub fn ptr_eq(&self, other: &Vector<A>) -> bool

Test whether two vectors refer to the same content in memory.

This uses the following rules to determine equality:

  • If the two sides are references to the same vector, return true.
  • If the two sides are single chunk vectors pointing to the same chunk, return true.
  • If the two sides are full trees pointing to the same chunks, return true.

This would return true if you’re comparing a vector to itself, or if you’re comparing a vector to a fresh clone of itself. The exception to this is if you’ve cloned an inline array (ie. an array with so few elements they can fit inside the space a Vector allocates for its pointers, so there are no heap allocations to compare).

Time: O(1)

Source

pub fn iter(&self) -> Iter<'_, A>

Get an iterator over a vector.

Time: O(1)

Source

pub fn iter_mut(&mut self) -> IterMut<'_, A>

Get a mutable iterator over a vector.

Time: O(1)

Source

pub fn leaves(&self) -> Chunks<'_, A>

Get an iterator over the leaf nodes of a vector.

This returns an iterator over the Chunks at the leaves of the RRB tree. These are useful for efficient parallelisation of work on the vector, but should not be used for basic iteration.

Time: O(1)

Source

pub fn leaves_mut(&mut self) -> ChunksMut<'_, A>

Get a mutable iterator over the leaf nodes of a vector. This returns an iterator over the Chunks at the leaves of the RRB tree. These are useful for efficient parallelisation of work on the vector, but should not be used for basic iteration.

Time: O(1)

Source

pub fn focus(&self) -> Focus<'_, A>

Construct a Focus for a vector.

Time: O(1)

Source

pub fn focus_mut(&mut self) -> FocusMut<'_, A>

Construct a FocusMut for a vector.

Time: O(1)

Source

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

Get a reference to the value at index index in a vector.

Returns None if the index is out of bounds.

Time: O(log n)

§Examples
let vec = vector!["Joe", "Mike", "Robert"];
assert_eq!(Some(&"Robert"), vec.get(2));
assert_eq!(None, vec.get(5));
Source

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

Get a mutable reference to the value at index index in a vector.

Returns None if the index is out of bounds.

Time: O(log n)

§Examples
let mut vec = vector!["Joe", "Mike", "Robert"];
{
    let robert = vec.get_mut(2).unwrap();
    assert_eq!(&mut "Robert", robert);
    *robert = "Bjarne";
}
assert_eq!(vector!["Joe", "Mike", "Bjarne"], vec);
Source

pub fn front(&self) -> Option<&A>

Get the first element of a vector.

If the vector is empty, None is returned.

Time: O(log n)

Source

pub fn front_mut(&mut self) -> Option<&mut A>

Get a mutable reference to the first element of a vector.

If the vector is empty, None is returned.

Time: O(log n)

Source

pub fn head(&self) -> Option<&A>

Get the first element of a vector.

If the vector is empty, None is returned.

This is an alias for the front method.

Time: O(log n)

Source

pub fn back(&self) -> Option<&A>

Get the last element of a vector.

If the vector is empty, None is returned.

Time: O(log n)

Source

pub fn back_mut(&mut self) -> Option<&mut A>

Get a mutable reference to the last element of a vector.

If the vector is empty, None is returned.

Time: O(log n)

Source

pub fn last(&self) -> Option<&A>

Get the last element of a vector.

If the vector is empty, None is returned.

This is an alias for the back method.

Time: O(log n)

Source

pub fn index_of(&self, value: &A) -> Option<usize>
where A: PartialEq,

Get the index of a given element in the vector.

Searches the vector for the first occurrence of a given value, and returns the index of the value if it’s there. Otherwise, it returns None.

Time: O(n)

§Examples
let mut vec = vector![1, 2, 3, 4, 5];
assert_eq!(Some(2), vec.index_of(&3));
assert_eq!(None, vec.index_of(&31337));
Source

pub fn contains(&self, value: &A) -> bool
where A: PartialEq,

Test if a given element is in the vector.

Searches the vector for the first occurrence of a given value, and returns true if it’s there. If it’s nowhere to be found in the vector, it returns false.

Time: O(n)

§Examples
let mut vec = vector![1, 2, 3, 4, 5];
assert_eq!(true, vec.contains(&3));
assert_eq!(false, vec.contains(&31337));
Source

pub fn clear(&mut self)

Discard all elements from the vector.

This leaves you with an empty vector, and all elements that were previously inside it are dropped.

Time: O(n)

Source

pub fn binary_search_by<F>(&self, f: F) -> Result<usize, usize>
where F: FnMut(&A) -> Ordering,

Binary search a sorted vector for a given element using a comparator function.

Assumes the vector has already been sorted using the same comparator function, eg. by using sort_by.

If the value is found, it returns Ok(index) where index is the index of the element. If the value isn’t found, it returns Err(index) where index is the index at which the element would need to be inserted to maintain sorted order.

Time: O(log n)

Binary search a sorted vector for a given element.

If the value is found, it returns Ok(index) where index is the index of the element. If the value isn’t found, it returns Err(index) where index is the index at which the element would need to be inserted to maintain sorted order.

Time: O(log n)

Source

pub fn binary_search_by_key<B, F>(&self, b: &B, f: F) -> Result<usize, usize>
where F: FnMut(&A) -> B, B: Ord,

Binary search a sorted vector for a given element with a key extract function.

Assumes the vector has already been sorted using the same key extract function, eg. by using sort_by_key.

If the value is found, it returns Ok(index) where index is the index of the element. If the value isn’t found, it returns Err(index) where index is the index at which the element would need to be inserted to maintain sorted order.

Time: O(log n)

Source

pub fn update(&self, index: usize, value: A) -> Vector<A>

Create a new vector with the value at index index updated.

Panics if the index is out of bounds.

Time: O(log n)

§Examples
let mut vec = vector![1, 2, 3];
assert_eq!(vector![1, 5, 3], vec.update(1, 5));
Source

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

Update the value at index index in a vector.

Returns the previous value at the index.

Panics if the index is out of bounds.

Time: O(log n)

Source

pub fn swap(&mut self, i: usize, j: usize)

Swap the elements at indices i and j.

Time: O(log n)

Source

pub fn push_front(&mut self, value: A)

Push a value to the front of a vector.

Time: O(1)*

§Examples
let mut vec = vector![5, 6, 7];
vec.push_front(4);
assert_eq!(vector![4, 5, 6, 7], vec);
Source

pub fn push_back(&mut self, value: A)

Push a value to the back of a vector.

Time: O(1)*

§Examples
let mut vec = vector![1, 2, 3];
vec.push_back(4);
assert_eq!(vector![1, 2, 3, 4], vec);
Source

pub fn pop_front(&mut self) -> Option<A>

Remove the first element from a vector and return it.

Time: O(1)*

§Examples
let mut vec = vector![1, 2, 3];
assert_eq!(Some(1), vec.pop_front());
assert_eq!(vector![2, 3], vec);
Source

pub fn pop_back(&mut self) -> Option<A>

Remove the last element from a vector and return it.

Time: O(1)*

§Examples
let mut vec = vector![1, 2, 3];
assert_eq!(Some(3), vec.pop_back());
assert_eq!(vector![1, 2], vec);
Source

pub fn append(&mut self, other: Vector<A>)

Append the vector other to the end of the current vector.

Time: O(log n)

§Examples
let mut vec = vector![1, 2, 3];
vec.append(vector![7, 8, 9]);
assert_eq!(vector![1, 2, 3, 7, 8, 9], vec);
Source

pub fn retain<F>(&mut self, f: F)
where F: FnMut(&A) -> bool,

Retain only the elements specified by the predicate.

Remove all elements for which the provided function f returns false from the vector.

Time: O(n)

Source

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

Split a vector at a given index.

Split a vector at a given index, leaving the left hand side in the current vector and returning a new vector containing the right hand side.

Time: O(log n)

§Examples
let mut left = vector![1, 2, 3, 7, 8, 9];
let right = left.split_off(3);
assert_eq!(vector![1, 2, 3], left);
assert_eq!(vector![7, 8, 9], right);
Source

pub fn skip(&self, count: usize) -> Vector<A>

Construct a vector with count elements removed from the start of the current vector.

Time: O(log n)

Source

pub fn take(&self, count: usize) -> Vector<A>

Construct a vector of the first count elements from the current vector.

Time: O(log n)

Source

pub fn truncate(&mut self, len: usize)

Truncate a vector to the given size.

Discards all elements in the vector beyond the given length.

Panics if the new length is greater than the current length.

Time: O(log n)

Source

pub fn slice<R>(&mut self, range: R) -> Vector<A>
where R: RangeBounds<usize>,

Extract a slice from a vector.

Remove the elements from start_index until end_index in the current vector and return the removed slice as a new vector.

Time: O(log n)

Source

pub fn insert(&mut self, index: usize, value: A)

Insert an element into a vector.

Insert an element at position index, shifting all elements after it to the right.

§Performance Note

While push_front and push_back are heavily optimised operations, insert in the middle of a vector requires a split, a push, and an append. Thus, if you want to insert many elements at the same location, instead of inserting them one by one, you should rather create a new vector containing the elements to insert, split the vector at the insertion point, and append the left hand, the new vector and the right hand in order.

Time: O(log n)

Source

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

Remove an element from a vector.

Remove the element from position ‘index’, shifting all elements after it to the left, and return the removed element.

§Performance Note

While pop_front and pop_back are heavily optimised operations, remove in the middle of a vector requires a split, a pop, and an append. Thus, if you want to remove many elements from the same location, instead of removeing them one by one, it is much better to use slice.

Time: O(log n)

Source

pub fn insert_ord(&mut self, item: A)
where A: Ord,

Insert an element into a sorted vector.

Insert an element into a vector in sorted order, assuming the vector is already in sorted order.

Time: O(log n)

§Examples
let mut vec = vector![1, 2, 3, 7, 8, 9];
vec.insert_ord(5);
assert_eq!(vector![1, 2, 3, 5, 7, 8, 9], vec);
Source

pub fn sort(&mut self)
where A: Ord,

Sort a vector.

Time: O(n log n)

§Examples
let mut vec = vector![3, 2, 5, 4, 1];
vec.sort();
assert_eq!(vector![1, 2, 3, 4, 5], vec);
Source

pub fn sort_by<F>(&mut self, cmp: F)
where F: Fn(&A, &A) -> Ordering,

Sort a vector using a comparator function.

Time: O(n log n)

§Examples
let mut vec = vector![3, 2, 5, 4, 1];
vec.sort_by(|left, right| left.cmp(right));
assert_eq!(vector![1, 2, 3, 4, 5], vec);

Trait Implementations§

Source§

impl<'a> Debug for VectorGuard<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Deref for VectorGuard<'a>

Source§

type Target = Vector<u8>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<'a> DerefMut for VectorGuard<'a>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<'a> Drop for VectorGuard<'a>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for VectorGuard<'a>

§

impl<'a> RefUnwindSafe for VectorGuard<'a>

§

impl<'a> Send for VectorGuard<'a>

§

impl<'a> Sync for VectorGuard<'a>

§

impl<'a> Unpin for VectorGuard<'a>

§

impl<'a> !UnwindSafe for VectorGuard<'a>

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

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.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V